7ad2003c4979c867412c1ab9abed62d1222540f2
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.openroadm.impl;
23
24 import java.net.URL;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
32 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.YangHelper2;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.HistoricalPmList;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.group.HistoricalPm;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.list.HistoricalPmEntry;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.val.group.Measurement;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmDataType;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmGranularity;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmNamesEnum;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.g836.pm.types.rev200413.ErroredSecond;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.g836.pm.types.rev200413.SeverelyErroredSecond;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413.PerformanceMeasurementTypeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GranularityPeriodType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntityBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.entity.PerformanceData;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.entity.PerformanceDataBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.MeasurementBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.MeasurementKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.PerformanceMeasurementUnitId;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.osgi.framework.Bundle;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.FrameworkUtil;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 /**
61  * @author shabnam
62  *
63  *         Reading Openroadm PM data and returning as PmDataEntitiy data
64  */
65 public class PmDataBuilderOpenRoadm {
66     // variables
67     private static final Logger log = LoggerFactory.getLogger(OpenroadmNetworkElement.class);
68     private PmdataEntityBuilder pmDataBuilder;
69     private Bundle b = FrameworkUtil.getBundle(this.getClass());
70
71     // end of variables
72     // constructors
73     public PmDataBuilderOpenRoadm(NetconfBindingAccessor accessor) {
74         this.pmDataBuilder = new PmdataEntityBuilder();
75         this.pmDataBuilder.setNodeName(accessor.getNodeId().getValue());
76     }
77
78     // end of constructors
79     // public methods
80     // Read PM data
81     public HistoricalPmList getPmData(NetconfBindingAccessor accessor) {
82         final Class<HistoricalPmList> pmDataClass = HistoricalPmList.class;
83         log.info("Get PM data for element {}", accessor.getNodeId().getValue());
84         InstanceIdentifier<HistoricalPmList> pmDataIid = InstanceIdentifier.builder(pmDataClass).build();
85         return accessor.getTransactionUtils().readData(accessor.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
86                 pmDataIid);
87
88     }
89
90     // Build PM entity for writing into the database
91     public List<PmdataEntity> buildPmDataEntity(HistoricalPmList historicalPmEnitityList) {
92         List<PmdataEntity> pmEntitiyList = new ArrayList<>();
93         Collection<HistoricalPmEntry> pmDataEntryList =
94                 YangHelper.getCollection(historicalPmEnitityList.getHistoricalPmEntry());
95         for (HistoricalPmEntry pmDataEntry : pmDataEntryList) {
96             pmDataBuilder.setUuidInterface(pmDataEntry.getPmResourceType().getName());
97             Collection<HistoricalPm> historicalPmList = YangHelper.getCollection(pmDataEntry.getHistoricalPm());
98             for (HistoricalPm historicalPm : historicalPmList) {
99                 log.info("PmName:{}", historicalPm.getType());
100                 //              pmDataBuilder.setPerformanceData(value)
101
102                 try {
103                     writeperformanceData(historicalPm);
104                 } catch (ClassNotFoundException e) {
105                     log.info("No relevant data found");
106                 }
107                 //              log.info("NodeName: {}, Scanner Id:{}, Period: {}", this.getNodeName(),
108                 //                      this.getScannerId(), this.getGranularityPeriod().getName());
109                 pmEntitiyList.add(this.pmDataBuilder.build());
110
111                 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
112             }
113             log.info("PmListSize before db writing: {}", pmEntitiyList.size());
114         }
115         return pmEntitiyList;
116     }
117     // end of public methods
118
119     // private methods
120     private void writeperformanceData(HistoricalPm historicalPm) throws ClassNotFoundException {
121         Collection<Measurement> measurementList = YangHelper.getCollection(historicalPm.getMeasurement());
122           Map<MeasurementKey, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement> measurementMap=new HashMap<MeasurementKey, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement>();
123         // Map Performance data of PmDataEntity with MeasurmentData-HistoricalPm
124         PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
125         for (Measurement measurementData : measurementList) {
126             this.pmDataBuilder.setGranularityPeriod(mapGranularityPeriod(measurementData.getGranularity()))
127                     .setTimeStamp(measurementData.getCompletionTime());
128             if (measurementData.getValidity().getName().equals("suspect")) {
129                 this.pmDataBuilder.setSuspectIntervalFlag(true);
130             }
131             measurementMap.put(new MeasurementKey(measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
132                     measurementData.getPmParameterValue()).getPmKey()), measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
133                     measurementData.getPmParameterValue()));
134
135
136
137             //    log.info("Time:d{}, \n Scannerid: {}, \n UUID: {}", this.getGranularityPeriod().getName(),
138             //          pmDataBuilder.getScannerId(), this.getUuidInterface());
139         }
140
141         pmDataBuilder.setPerformanceData(performanceDataBuilder.setMeasurement(measurementMap).build());
142     }
143
144
145
146     //Map Performance data of PmDataEntity with  MeasurmentData-HistoricalPm
147     private PerformanceData getPerformancedata(Measurement measurementData) {
148         PerformanceData performanceData;
149         PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
150         performanceData = performanceDataBuilder.setCses(YangHelper2.getInteger(measurementData.getBinNumber()))
151                 .setSes(measurementData.getPmParameterValue().getUint64().intValue()).build();
152         return performanceData;
153     }
154
155     // Mapping Granularity period of PmDataEntity with PmGranularity of MeasurmentData-HistoricalPm
156     private GranularityPeriodType mapGranularityPeriod(PmGranularity pmGranularity) {
157
158         GranularityPeriodType granPeriod = null;
159         switch (pmGranularity.getName()) {
160             case ("notApplicable"):
161                 granPeriod = GranularityPeriodType.Unknown;
162                 break;
163             case ("15min"):
164                 granPeriod = GranularityPeriodType.Period15Min;
165                 break;
166             case ("24Hour"):
167                 granPeriod = GranularityPeriodType.Period24Hours;
168                 break;
169             default:
170                 granPeriod = GranularityPeriodType.Period15Min;
171                 break;
172         }
173         return granPeriod;
174     }
175
176     private List<Class<? extends PerformanceMeasurementTypeId>> setMeasurementTypeId() throws ClassNotFoundException {
177         String packageName =
178                 "org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413";
179         String packageName1 =
180                 "/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/data/provider/openroadm/pm/types/rev200413/";
181         List<Class<? extends PerformanceMeasurementTypeId>> measTypeObjList =
182                 new ArrayList<Class<? extends PerformanceMeasurementTypeId>>();
183         URL root = Thread.currentThread().getContextClassLoader().getResource(packageName1);
184
185         log.info("path for type package: {}", root);
186
187         Enumeration<URL> results = getFileURL(b, packageName);
188         log.info("FOund Packages {}", results);
189         while (results.hasMoreElements()) {
190             URL path = results.nextElement();
191             //          log.info("Enumeration URL-file {}", path.getFile());
192             //          log.info("Enumeration URL-String {}", path.toString());
193             Class<?> cls1 = loadClass(b, path.getFile());
194
195             if (PerformanceMeasurementTypeId.class.isAssignableFrom(cls1)) {
196                 measTypeObjList.add((Class<? extends PerformanceMeasurementTypeId>) cls1);
197             }
198             log.info("Class Added {}", cls1.getSimpleName());
199
200         }
201
202         return measTypeObjList;
203     }
204
205     private List<Class<? extends PerformanceMeasurementUnitId>> setMeasurementUnit() throws ClassNotFoundException {
206         String packageName =
207                 "org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413";
208         List<Class<? extends PerformanceMeasurementUnitId>> measUnitObjList =
209                 new ArrayList<Class<? extends PerformanceMeasurementUnitId>>();
210         URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
211         log.info("path for unit package{}", root);
212
213         Enumeration<URL> results_unit = getFileURL(b, packageName);
214         log.info("FOund Packages {}", results_unit);
215         while (results_unit.hasMoreElements()) {
216             URL path = results_unit.nextElement();
217             Class<?> cls1 = loadClass(b, path.getFile());
218             if (PerformanceMeasurementUnitId.class.isAssignableFrom(cls1)) {
219                 measUnitObjList.add((Class<? extends PerformanceMeasurementUnitId>) cls1);
220             }
221             log.info("Class Added {}", cls1.getSimpleName());
222
223         }
224
225         return measUnitObjList;
226     }
227
228     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement measurementBuilder(
229             PmNamesEnum pmType, String pmUnit, PmDataType pmDataType) throws ClassNotFoundException {
230
231         MeasurementBuilder measBuilder = new MeasurementBuilder();
232         if (pmType.getName() == "erroredSeconds") {
233             measBuilder.setPmKey(ErroredSecond.class);
234         } else if (pmType.getName() == "severelyErroredSeconds") {
235             measBuilder.setPmKey(SeverelyErroredSecond.class);
236         } else {
237             for (Class<? extends PerformanceMeasurementTypeId> obj : setMeasurementTypeId()) {
238                 if (obj.toString().contains(pmType.name())) {
239                     measBuilder.setPmKey(obj);
240                 }
241             }
242         }
243         for (Class<? extends PerformanceMeasurementUnitId> obj : setMeasurementUnit()) {
244             if (obj.toString().contains(pmUnit)) {
245                 measBuilder.setPmUnit(obj);
246             }
247         }
248         measBuilder.setPmValue(pmDataType);
249         return measBuilder.build();
250
251     }
252
253     private Class<?> loadClass(Bundle bundle, String classFilePath) {
254         String className = classFilePath.replaceFirst("^/", "").replace('/', '.').replaceFirst(".class$", "");
255         try {
256             return bundle.loadClass(className);
257         } catch (Throwable e) {
258             log.info(String.format("Class [%s] could not be loaded. Message: [%s].", className, e.getMessage()));
259         }
260         return null;
261     }
262
263     private static Enumeration<URL> getFileURL(Bundle b, String classPath) {
264
265         BundleContext context = b == null ? null : b.getBundleContext();
266         if (context == null) {
267             log.info("no bundle context available");
268             return null;
269         }
270         Bundle[] bundles = context.getBundles();
271         if (bundles == null || bundles.length <= 0) {
272             log.info("no bundles found");
273             return null;
274         }
275         log.info("found {} bundles", bundles.length);
276         Enumeration<URL> resultUrl = null;
277
278         for (Bundle bundle : bundles) {
279             resultUrl = bundle.findEntries("/" + classPath.replace(".", "/"), "*.class", false);
280             //          resultUrl = bundle.getEntryPaths("/" + classPath.replace(".", "/"));
281             if (resultUrl != null) {
282                 b = bundle;
283                 break;
284
285             }
286
287         }
288
289         return resultUrl;
290     }
291
292
293     // end of private methods
294 }
295