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=4b76ae432943aa98a738719aeb3c8b4ca8afb683;hb=aa7991e2bb89e56479a79541a5d9b659ae619cd7;hp=0000000000000000000000000000000000000000;hpb=17d7d2966615671ef113dd32c7ba5ebff47f5e6f;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 new file mode 100644 index 0000000..4b76ae4 --- /dev/null +++ b/common/src/main/java/org/commscope/tr069adapter/common/deviceversion/DeviceVersionManagerImpl.java @@ -0,0 +1,192 @@ +/* + * ============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; + } +}