0c712c56389ce7092d639dc354ebc3fae0e71231
[oam/tr069-adapter.git] / common / src / main / java / org / commscope / tr069adapter / common / deviceversion / DeviceVersionManagerImpl.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.common.deviceversion;
20
21 import com.fasterxml.jackson.databind.ObjectMapper;
22
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;
34
35 import javax.annotation.PostConstruct;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class DeviceVersionManagerImpl implements DeviceVersionManager {
43   private static final Logger LOG = LoggerFactory.getLogger(DeviceVersionManagerImpl.class);
44
45   TreeMap<DeviceVersion, String> deviceVersionMap = new TreeMap<>();
46   TreeMap<String, ProfileDefinition> profileDefinitionMap = new TreeMap<>();
47
48   @PostConstruct
49   public void loadProfileConfiguration() throws IOException {
50     ObjectMapper objectMapper = new ObjectMapper();
51     String contents;
52     try (
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()));
57     }
58
59     ProfileDefinitions versions;
60     try {
61       versions = objectMapper.readValue(contents, ProfileDefinitions.class);
62
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);
70       }
71     } catch (IOException e) {
72       LOG.info("context", e);
73
74     }
75   }
76
77
78
79   @Override
80   public String getNetconfYangSchemaPath(String swVersion, String hwVersion) {
81     String profileId = getAssociatedProfileId(swVersion, hwVersion);
82     ProfileDefinition profileDefinition = profileDefinitionMap.get(profileId);
83     return profileDefinition.getNetConfSchemaPath();
84   }
85
86   @Override
87   public String getBaseNetconfYangSchemaPath() {
88     return profileDefinitionMap.firstEntry().getValue().getNetConfSchemaPath();
89   }
90
91   @Override
92   public ProfileDefinition getProfileDefinition(String swVersion, String hwVersion) {
93     String profileId = getAssociatedProfileId(swVersion, hwVersion);
94     return profileDefinitionMap.get(profileId);
95   }
96
97   @Override
98   public List<ProfileDefinition> getSupportedProfileDefinitions() {
99     List<ProfileDefinition> proDeflist = new ArrayList<>();
100     for (Iterator<String> iterator = profileDefinitionMap.keySet().iterator(); iterator
101         .hasNext();) {
102       String key = iterator.next();
103       proDeflist.add(profileDefinitionMap.get(key));
104     }
105     return proDeflist;
106   }
107
108   @Override
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);
113
114     if (profileId == null) {
115       profileId = profileDefinitionMap.firstEntry().getValue().getProfileId();
116     }
117
118     return profileId;
119   }
120
121   private String getProfileName(TreeMap<DeviceVersion, String> deviceVersionMap, String swVersion,
122       String hwVersion) {
123     DeviceVersion deviceVersion = new DeviceVersion(swVersion, hwVersion, false, false);
124     ArrayList<DeviceVersion> mSoftwareList = new ArrayList<>();
125
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) {
136
137                 if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
138                     || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())
139                     || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {
140                   return entry.getValue();
141                 }
142               }
143             } else {
144               // Check Strict match of Hardware
145               if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
146                   || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {
147                 return entry.getValue();
148               }
149             }
150           }
151         } else if (profileVersion.isHwRegex()) {
152           if (deviceVersion.getHwVersion() != null) {
153             if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
154                 || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())
155                 || deviceVersion.getHwVersion().matches(profileVersion.getHwVersion())) {
156               // Add all software version which matching
157               if (profileVersion.getSwVersion()
158                   .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {
159                 mSoftwareList.add(profileVersion);
160               }
161             }
162           }
163         }
164       } else {
165         // Check Strict match of Hardware
166         if ("*".equalsIgnoreCase(profileVersion.getHwVersion())
167             || deviceVersion.getHwVersion().equalsIgnoreCase(profileVersion.getHwVersion())) {
168           if (profileVersion.getSwVersion()
169               .compareToIgnoreCase(deviceVersion.getSwVersion()) <= 0) {
170             mSoftwareList.add(profileVersion);
171           }
172         }
173       }
174     }
175
176     if (!mSoftwareList.isEmpty()) {
177       // return the least matched software version profile
178       Collections.sort(mSoftwareList, DeviceVersion.softwareComparator);
179       return deviceVersionMap.get(mSoftwareList.get(mSoftwareList.size() - 1));
180     }
181
182     return null;
183   }
184 }