2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
19 package org.commscope.tr069adapter.common.deviceversion;
21 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.TreeMap;
33 import java.util.stream.Collectors;
35 import javax.annotation.PostConstruct;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Component;
42 public class DeviceVersionManagerImpl implements DeviceVersionManager {
43 private static final Logger LOG = LoggerFactory.getLogger(DeviceVersionManagerImpl.class);
45 TreeMap<DeviceVersion, String> deviceVersionMap = new TreeMap<>();
46 TreeMap<String, ProfileDefinition> profileDefinitionMap = new TreeMap<>();
49 public void loadProfileConfiguration() throws IOException {
50 ObjectMapper objectMapper = new ObjectMapper();
53 InputStream inputStream =
54 getClass().getResourceAsStream("/profile-definition-mapping.json");
55 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
56 contents = reader.lines().collect(Collectors.joining(System.lineSeparator()));
59 ProfileDefinitions versions;
61 versions = objectMapper.readValue(contents, ProfileDefinitions.class);
63 List<ProfileDefinition> definitionList = versions.getProfileDefinition();
64 for (ProfileDefinition definition : definitionList) {
65 DeviceVersion versionDTO =
66 new DeviceVersion(definition.getSoftwareVersion(), definition.getHardwareVersion());
67 String profileId = definition.getProfileId();
68 deviceVersionMap.put(versionDTO, profileId);
69 profileDefinitionMap.put(profileId, definition);
71 } catch (IOException e) {
72 LOG.info("context", e);
80 public String getNetconfYangSchemaPath(String swVersion, String hwVersion) {
81 String profileId = getAssociatedProfileId(swVersion, hwVersion);
82 ProfileDefinition profileDefinition = profileDefinitionMap.get(profileId);
83 return profileDefinition.getNetConfSchemaPath();
87 public String getBaseNetconfYangSchemaPath() {
88 return profileDefinitionMap.firstEntry().getValue().getNetConfSchemaPath();
92 public ProfileDefinition getProfileDefinition(String swVersion, String hwVersion) {
93 String profileId = getAssociatedProfileId(swVersion, hwVersion);
94 return profileDefinitionMap.get(profileId);
98 public List<ProfileDefinition> getSupportedProfileDefinitions() {
99 List<ProfileDefinition> proDeflist = new ArrayList<>();
100 for (Iterator<String> iterator = profileDefinitionMap.keySet().iterator(); iterator
102 String key = iterator.next();
103 proDeflist.add(profileDefinitionMap.get(key));
109 public String getAssociatedProfileId(String swVersion, String hwVersion) {
110 String profileId = null;
111 if (null != swVersion) // TODO: Consider hardware version also.
112 profileId = getProfileName(deviceVersionMap, swVersion, hwVersion);
114 if (profileId == null) {
115 profileId = profileDefinitionMap.firstEntry().getValue().getProfileId();
121 private String getProfileName(TreeMap<DeviceVersion, String> deviceVersionMap, String swVersion,
123 DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false);
124 ArrayList<DeviceVersion> mSoftwareList = new ArrayList<>();
126 for (Iterator<Entry<DeviceVersion, String>> iterator =
127 deviceVersionMap.entrySet().iterator(); iterator.hasNext();) {
128 Entry<DeviceVersion, String> entry = iterator.next();
129 DeviceVersion profileVersion = entry.getKey();
130 if (profileVersion.isHwRegex() || profileVersion.isSwRegex()) {
131 if (profileVersion.isSwRegex()) {
132 if (deviceVersion.getSwVersion().equalsIgnoreCase(profileVersion.getSwVersion())
133 || deviceVersion.getSwVersion().matches(profileVersion.getSwVersion())) {
134 if (profileVersion.isHwRegex()) {
135 if (deviceVersion.getHwVersion() != null && hwVersionCheck(deviceVersion, profileVersion)) {
136 return entry.getValue();
139 // Check Strict match of Hardware
140 if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
141 || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {
142 return entry.getValue();
146 } else if (profileVersion.isHwRegex() && (deviceVersion.getHwVersion() != null && hwVersionCheck(deviceVersion, profileVersion)) ) {
147 // Add all software version which matching
148 if (profileVersion.getSwVersion()
149 .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {
150 mSoftwareList.add(profileVersion);
154 // Check Strict match of Hardware
155 if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
156 || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {
157 if (profileVersion.getSwVersion()
158 .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {
159 mSoftwareList.add(profileVersion);
165 if (!mSoftwareList.isEmpty()) {
166 // return the least matched software version profile
167 Collections.sort(mSoftwareList, DeviceVersion.softwareComparator);
168 return deviceVersionMap.get(mSoftwareList.get(mSoftwareList.size() - 1));
174 private boolean hwVersionCheck(DeviceVersion deviceVersion, DeviceVersion profileVersion) {
175 return "*".equalsIgnoreCase(profileVersion.getHwVersion()) || deviceVersion.getHwVersion()
176 .equalsIgnoreCase(profileVersion.getHwVersion()) || deviceVersion.getHwVersion()
177 .matches(profileVersion.getHwVersion());