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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 package org.commscope.tr069adapter.common.deviceversion;
\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
37 public class DeviceVersionManagerImpl implements DeviceVersionManager {
\r
39 TreeMap<DeviceVersion, String> deviceVersionMap = new TreeMap<>();
\r
40 TreeMap<String, ProfileDefinition> profileDefinitionMap = new TreeMap<>();
\r
43 public void loadProfileConfiguration() throws IOException {
\r
44 ObjectMapper objectMapper = new ObjectMapper();
\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
53 ProfileDefinitions versions;
\r
55 versions = objectMapper.readValue(contents, ProfileDefinitions.class);
\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
65 } catch (IOException e) {
\r
66 // TODO Auto-generated catch block
\r
67 e.printStackTrace();
\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
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
82 System.out.println("File loading is completed");
\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
93 public String getBaseNetconfYangSchemaPath() {
\r
94 return profileDefinitionMap.firstEntry().getValue().getNetConfSchemaPath();
\r
98 public ProfileDefinition getProfileDefinition(String swVersion, String hwVersion) {
\r
99 String profileId = getAssociatedProfileId(swVersion, hwVersion);
\r
100 return profileDefinitionMap.get(profileId);
\r
104 public List<ProfileDefinition> getSupportedProfileDefinitions() {
\r
105 List<ProfileDefinition> proDeflist = new ArrayList<>();
\r
106 for (Iterator<String> iterator = profileDefinitionMap.keySet().iterator(); iterator
\r
108 String key = iterator.next();
\r
109 proDeflist.add(profileDefinitionMap.get(key));
\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
120 if (profileId == null) {
\r
121 profileId = profileDefinitionMap.firstEntry().getValue().getProfileId();
\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
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
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
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
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
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
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