Device Software version management
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / deviceversion / DeviceVersionManagerImpl.java
diff --git a/common/src/main/java/org/commscope/tr069adapter/common/deviceversion/DeviceVersionManagerImpl.java b/common/src/main/java/org/commscope/tr069adapter/common/deviceversion/DeviceVersionManagerImpl.java
new file mode 100644 (file)
index 0000000..4b76ae4
--- /dev/null
@@ -0,0 +1,192 @@
+/*\r
+ * ============LICENSE_START========================================================================\r
+ * ONAP : tr-069-adapter\r
+ * =================================================================================================\r
+ * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
+ * =================================================================================================\r
+ * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
+ * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
+ * may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
+ * either express or implied. See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ===============LICENSE_END=======================================================================\r
+ */\r
+\r
+package org.commscope.tr069adapter.common.deviceversion;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map.Entry;\r
+import java.util.TreeMap;\r
+import java.util.stream.Collectors;\r
+import javax.annotation.PostConstruct;\r
+import org.springframework.stereotype.Component;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+\r
+@Component\r
+public class DeviceVersionManagerImpl implements DeviceVersionManager {\r
+\r
+  TreeMap<DeviceVersion, String> deviceVersionMap = new TreeMap<>();\r
+  TreeMap<String, ProfileDefinition> profileDefinitionMap = new TreeMap<>();\r
+\r
+  @PostConstruct\r
+  public void loadProfileConfiguration() throws IOException {\r
+    ObjectMapper objectMapper = new ObjectMapper();\r
+    String contents;\r
+    try (\r
+        InputStream inputStream =\r
+            getClass().getResourceAsStream("/profile-definition-mapping.json");\r
+        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {\r
+      contents = reader.lines().collect(Collectors.joining(System.lineSeparator()));\r
+    }\r
+\r
+    ProfileDefinitions versions;\r
+    try {\r
+      versions = objectMapper.readValue(contents, ProfileDefinitions.class);\r
+\r
+      List<ProfileDefinition> definitionList = versions.getProfileDefinition();\r
+      for (ProfileDefinition definition : definitionList) {\r
+        DeviceVersion versionDTO =\r
+            new DeviceVersion(definition.getSoftwareVersion(), definition.getHardwareVersion());\r
+        String profileId = definition.getProfileId();\r
+        deviceVersionMap.put(versionDTO, profileId);\r
+        profileDefinitionMap.put(profileId, definition);\r
+      }\r
+    } catch (IOException e) {\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+    }\r
+  }\r
+\r
+  public static void main(String[] args) {\r
+    System.out.println("started loading the json file");\r
+    DeviceVersionManagerImpl impl = new DeviceVersionManagerImpl();\r
+    try {\r
+      impl.loadProfileConfiguration();\r
+      System.out.println(impl.getAssociatedProfileId("4.3.0.0", "*"));\r
+    } catch (Exception e) {\r
+      System.out.println("Exception While loading the file");\r
+      // TODO Auto-generated catch block\r
+      e.printStackTrace();\r
+    }\r
+    System.out.println("File loading is completed");\r
+  }\r
+\r
+  @Override\r
+  public String getNetconfYangSchemaPath(String swVersion, String hwVersion) {\r
+    String profileId = getAssociatedProfileId(swVersion, hwVersion);\r
+    ProfileDefinition profileDefinition = profileDefinitionMap.get(profileId);\r
+    return profileDefinition.getNetConfSchemaPath();\r
+  }\r
+\r
+  @Override\r
+  public String getBaseNetconfYangSchemaPath() {\r
+    return profileDefinitionMap.firstEntry().getValue().getNetConfSchemaPath();\r
+  }\r
+\r
+  @Override\r
+  public ProfileDefinition getProfileDefinition(String swVersion, String hwVersion) {\r
+    String profileId = getAssociatedProfileId(swVersion, hwVersion);\r
+    return profileDefinitionMap.get(profileId);\r
+  }\r
+\r
+  @Override\r
+  public List<ProfileDefinition> getSupportedProfileDefinitions() {\r
+    List<ProfileDefinition> proDeflist = new ArrayList<>();\r
+    for (Iterator<String> iterator = profileDefinitionMap.keySet().iterator(); iterator\r
+        .hasNext();) {\r
+      String key = iterator.next();\r
+      proDeflist.add(profileDefinitionMap.get(key));\r
+    }\r
+    return proDeflist;\r
+  }\r
+\r
+  @Override\r
+  public String getAssociatedProfileId(String swVersion, String hwVersion) {\r
+    String profileId = null;\r
+    if (null != swVersion) // TODO: Consider hardware version also.\r
+      profileId = getProfileName(deviceVersionMap, swVersion, hwVersion);\r
+\r
+    if (profileId == null) {\r
+      profileId = profileDefinitionMap.firstEntry().getValue().getProfileId();\r
+    }\r
+\r
+    return profileId;\r
+  }\r
+\r
+  private String getProfileName(TreeMap<DeviceVersion, String> deviceVersionMap, String swVersion,\r
+      String hwVersion) {\r
+    DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false);\r
+    ArrayList<DeviceVersion> mSoftwareList = new ArrayList<>();\r
+\r
+    for (Iterator<Entry<DeviceVersion, String>> iterator =\r
+        deviceVersionMap.entrySet().iterator(); iterator.hasNext();) {\r
+      Entry<DeviceVersion, String> entry = iterator.next();\r
+      DeviceVersion profileVersion = entry.getKey();\r
+      if (profileVersion.isHwRegex() || profileVersion.isSwRegex()) {\r
+        if (profileVersion.isSwRegex()) {\r
+          if (deviceVersion.getSwVersion().equalsIgnoreCase(profileVersion.getSwVersion())\r
+              || deviceVersion.getSwVersion().matches(profileVersion.getSwVersion())) {\r
+            if (profileVersion.isHwRegex()) {\r
+              if (deviceVersion.getHwVersion() != null) {\r
+\r
+                if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
+                    || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())\r
+                    || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {\r
+                  return entry.getValue();\r
+                }\r
+              } else {\r
+\r
+              }\r
+            } else {\r
+              // Check Strict match of Hardware\r
+              if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
+                  || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {\r
+                return entry.getValue();\r
+              }\r
+            }\r
+          }\r
+        } else if (profileVersion.isHwRegex()) {\r
+          if (deviceVersion.getHwVersion() != null) {\r
+            if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
+                || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())\r
+                || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {\r
+              // Add all software version which matching\r
+              if (profileVersion.getSwVersion()\r
+                  .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {\r
+                mSoftwareList.add(profileVersion);\r
+              }\r
+            }\r
+          }\r
+        }\r
+      } else {\r
+        // Check Strict match of Hardware\r
+        if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
+            || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {\r
+          if (profileVersion.getSwVersion()\r
+              .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {\r
+            mSoftwareList.add(profileVersion);\r
+          }\r
+        }\r
+      }\r
+    }\r
+\r
+    if (mSoftwareList.size() > 0) {\r
+      // return the least matched software version profile\r
+      Collections.sort(mSoftwareList, DeviceVersion.softwareComparator);\r
+      return deviceVersionMap.get(mSoftwareList.get(mSoftwareList.size() - 1));\r
+    }\r
+\r
+    return null;\r
+  }\r
+}\r