4b76ae432943aa98a738719aeb3c8b4ca8afb683
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / deviceversion / DeviceVersionManagerImpl.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.common.deviceversion;\r
20 \r
21 import java.io.BufferedReader;\r
22 import java.io.IOException;\r
23 import java.io.InputStream;\r
24 import java.io.InputStreamReader;\r
25 import java.util.ArrayList;\r
26 import java.util.Collections;\r
27 import java.util.Iterator;\r
28 import java.util.List;\r
29 import java.util.Map.Entry;\r
30 import java.util.TreeMap;\r
31 import java.util.stream.Collectors;\r
32 import javax.annotation.PostConstruct;\r
33 import org.springframework.stereotype.Component;\r
34 import com.fasterxml.jackson.databind.ObjectMapper;\r
35 \r
36 @Component\r
37 public class DeviceVersionManagerImpl implements DeviceVersionManager {\r
38 \r
39   TreeMap<DeviceVersion, String> deviceVersionMap = new TreeMap<>();\r
40   TreeMap<String, ProfileDefinition> profileDefinitionMap = new TreeMap<>();\r
41 \r
42   @PostConstruct\r
43   public void loadProfileConfiguration() throws IOException {\r
44     ObjectMapper objectMapper = new ObjectMapper();\r
45     String contents;\r
46     try (\r
47         InputStream inputStream =\r
48             getClass().getResourceAsStream("/profile-definition-mapping.json");\r
49         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {\r
50       contents = reader.lines().collect(Collectors.joining(System.lineSeparator()));\r
51     }\r
52 \r
53     ProfileDefinitions versions;\r
54     try {\r
55       versions = objectMapper.readValue(contents, ProfileDefinitions.class);\r
56 \r
57       List<ProfileDefinition> definitionList = versions.getProfileDefinition();\r
58       for (ProfileDefinition definition : definitionList) {\r
59         DeviceVersion versionDTO =\r
60             new DeviceVersion(definition.getSoftwareVersion(), definition.getHardwareVersion());\r
61         String profileId = definition.getProfileId();\r
62         deviceVersionMap.put(versionDTO, profileId);\r
63         profileDefinitionMap.put(profileId, definition);\r
64       }\r
65     } catch (IOException e) {\r
66       // TODO Auto-generated catch block\r
67       e.printStackTrace();\r
68     }\r
69   }\r
70 \r
71   public static void main(String[] args) {\r
72     System.out.println("started loading the json file");\r
73     DeviceVersionManagerImpl impl = new DeviceVersionManagerImpl();\r
74     try {\r
75       impl.loadProfileConfiguration();\r
76       System.out.println(impl.getAssociatedProfileId("4.3.0.0", "*"));\r
77     } catch (Exception e) {\r
78       System.out.println("Exception While loading the file");\r
79       // TODO Auto-generated catch block\r
80       e.printStackTrace();\r
81     }\r
82     System.out.println("File loading is completed");\r
83   }\r
84 \r
85   @Override\r
86   public String getNetconfYangSchemaPath(String swVersion, String hwVersion) {\r
87     String profileId = getAssociatedProfileId(swVersion, hwVersion);\r
88     ProfileDefinition profileDefinition = profileDefinitionMap.get(profileId);\r
89     return profileDefinition.getNetConfSchemaPath();\r
90   }\r
91 \r
92   @Override\r
93   public String getBaseNetconfYangSchemaPath() {\r
94     return profileDefinitionMap.firstEntry().getValue().getNetConfSchemaPath();\r
95   }\r
96 \r
97   @Override\r
98   public ProfileDefinition getProfileDefinition(String swVersion, String hwVersion) {\r
99     String profileId = getAssociatedProfileId(swVersion, hwVersion);\r
100     return profileDefinitionMap.get(profileId);\r
101   }\r
102 \r
103   @Override\r
104   public List<ProfileDefinition> getSupportedProfileDefinitions() {\r
105     List<ProfileDefinition> proDeflist = new ArrayList<>();\r
106     for (Iterator<String> iterator = profileDefinitionMap.keySet().iterator(); iterator\r
107         .hasNext();) {\r
108       String key = iterator.next();\r
109       proDeflist.add(profileDefinitionMap.get(key));\r
110     }\r
111     return proDeflist;\r
112   }\r
113 \r
114   @Override\r
115   public String getAssociatedProfileId(String swVersion, String hwVersion) {\r
116     String profileId = null;\r
117     if (null != swVersion) // TODO: Consider hardware version also.\r
118       profileId = getProfileName(deviceVersionMap, swVersion, hwVersion);\r
119 \r
120     if (profileId == null) {\r
121       profileId = profileDefinitionMap.firstEntry().getValue().getProfileId();\r
122     }\r
123 \r
124     return profileId;\r
125   }\r
126 \r
127   private String getProfileName(TreeMap<DeviceVersion, String> deviceVersionMap, String swVersion,\r
128       String hwVersion) {\r
129     DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false);\r
130     ArrayList<DeviceVersion> mSoftwareList = new ArrayList<>();\r
131 \r
132     for (Iterator<Entry<DeviceVersion, String>> iterator =\r
133         deviceVersionMap.entrySet().iterator(); iterator.hasNext();) {\r
134       Entry<DeviceVersion, String> entry = iterator.next();\r
135       DeviceVersion profileVersion = entry.getKey();\r
136       if (profileVersion.isHwRegex() || profileVersion.isSwRegex()) {\r
137         if (profileVersion.isSwRegex()) {\r
138           if (deviceVersion.getSwVersion().equalsIgnoreCase(profileVersion.getSwVersion())\r
139               || deviceVersion.getSwVersion().matches(profileVersion.getSwVersion())) {\r
140             if (profileVersion.isHwRegex()) {\r
141               if (deviceVersion.getHwVersion() != null) {\r
142 \r
143                 if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
144                     || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())\r
145                     || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {\r
146                   return entry.getValue();\r
147                 }\r
148               } else {\r
149 \r
150               }\r
151             } else {\r
152               // Check Strict match of Hardware\r
153               if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
154                   || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {\r
155                 return entry.getValue();\r
156               }\r
157             }\r
158           }\r
159         } else if (profileVersion.isHwRegex()) {\r
160           if (deviceVersion.getHwVersion() != null) {\r
161             if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
162                 || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())\r
163                 || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {\r
164               // Add all software version which matching\r
165               if (profileVersion.getSwVersion()\r
166                   .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {\r
167                 mSoftwareList.add(profileVersion);\r
168               }\r
169             }\r
170           }\r
171         }\r
172       } else {\r
173         // Check Strict match of Hardware\r
174         if ("*".equalsIgnoreCase(profileVersion.getHwVersion())\r
175             || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {\r
176           if (profileVersion.getSwVersion()\r
177               .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {\r
178             mSoftwareList.add(profileVersion);\r
179           }\r
180         }\r
181       }\r
182     }\r
183 \r
184     if (mSoftwareList.size() > 0) {\r
185       // return the least matched software version profile\r
186       Collections.sort(mSoftwareList, DeviceVersion.softwareComparator);\r
187       return deviceVersionMap.get(mSoftwareList.get(mSoftwareList.size() - 1));\r
188     }\r
189 \r
190     return null;\r
191   }\r
192 }\r