X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=common%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fcommon%2Fdeviceversion%2FDeviceVersionManagerImpl.java;fp=common%2Fsrc%2Fmain%2Fjava%2Forg%2Fcommscope%2Ftr069adapter%2Fcommon%2Fdeviceversion%2FDeviceVersionManagerImpl.java;h=0c712c56389ce7092d639dc354ebc3fae0e71231;hb=76744e810f35c84ecbd1d9998e361052466e9483;hp=4b76ae432943aa98a738719aeb3c8b4ca8afb683;hpb=ce4e2d38e3d42725f61c39dd172325d2def4bc44;p=oam%2Ftr069-adapter.git 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 index 4b76ae4..0c712c5 100644 --- a/common/src/main/java/org/commscope/tr069adapter/common/deviceversion/DeviceVersionManagerImpl.java +++ b/common/src/main/java/org/commscope/tr069adapter/common/deviceversion/DeviceVersionManagerImpl.java @@ -1,192 +1,184 @@ -/* - * ============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 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.springframework.stereotype.Component; -import com.fasterxml.jackson.databind.ObjectMapper; - -@Component -public class DeviceVersionManagerImpl implements DeviceVersionManager { - - TreeMap deviceVersionMap = new TreeMap<>(); - TreeMap 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 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) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static void main(String[] args) { - System.out.println("started loading the json file"); - DeviceVersionManagerImpl impl = new DeviceVersionManagerImpl(); - try { - impl.loadProfileConfiguration(); - System.out.println(impl.getAssociatedProfileId("4.3.0.0", "*")); - } catch (Exception e) { - System.out.println("Exception While loading the file"); - // TODO Auto-generated catch block - e.printStackTrace(); - } - System.out.println("File loading is completed"); - } - - @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 getSupportedProfileDefinitions() { - List proDeflist = new ArrayList<>(); - for (Iterator 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 deviceVersionMap, String swVersion, - String hwVersion) { - DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false); - ArrayList mSoftwareList = new ArrayList<>(); - - for (Iterator> iterator = - deviceVersionMap.entrySet().iterator(); iterator.hasNext();) { - Entry 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 { - - } - } 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.size() > 0) { - // return the least matched software version profile - Collections.sort(mSoftwareList, DeviceVersion.softwareComparator); - return deviceVersionMap.get(mSoftwareList.get(mSoftwareList.size() - 1)); - } - - return null; - } -} +/* + * ============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 deviceVersionMap = new TreeMap<>(); + TreeMap 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 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 getSupportedProfileDefinitions() { + List proDeflist = new ArrayList<>(); + for (Iterator 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 deviceVersionMap, String swVersion, + String hwVersion) { + DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false); + ArrayList mSoftwareList = new ArrayList<>(); + + for (Iterator> iterator = + deviceVersionMap.entrySet().iterator(); iterator.hasNext();) { + Entry 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; + } +}