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