b032348ca0b68717be97b83c2de4bcbfbcb29044
[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.netconfnodestateservice.NetconfBindingAccessor;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.HistoricalPmList;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.group.HistoricalPm;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.list.HistoricalPmEntry;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.val.group.Measurement;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmDataType;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmGranularity;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmNamesEnum;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.g836.pm.types.rev200413.ErroredSecond;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.g836.pm.types.rev200413.SeverelyErroredSecond;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413.PerformanceMeasurementTypeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GranularityPeriodType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntity;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.PmdataEntityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.entity.PerformanceDataBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.MeasurementBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.MeasurementKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.Celsius;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.DB;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.DBm;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.Fahrenheit;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.KHz;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.MW;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.units.rev200413.PerformanceMeasurementUnitId;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.osgi.framework.Bundle;
59 import org.osgi.framework.BundleContext;
60 import org.osgi.framework.FrameworkUtil;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  * @author shabnam
66  *
67  *         Reading Openroadm PM data and returning as PmDataEntitiy data
68  */
69 public class PmDataBuilderOpenRoadm {
70     // variables
71     private static final Logger log = LoggerFactory.getLogger(PmDataBuilderOpenRoadm.class);
72     private PmdataEntityBuilder pmDataBuilder;
73     private Bundle b = FrameworkUtil.getBundle(this.getClass());
74
75     // end of variables
76     // constructors
77     public PmDataBuilderOpenRoadm(NetconfBindingAccessor accessor) {
78         this.pmDataBuilder = new PmdataEntityBuilder();
79         this.pmDataBuilder.setNodeName(accessor.getNodeId().getValue());
80     }
81
82     // end of constructors
83     // public methods
84     // Read PM data
85     public HistoricalPmList getPmData(NetconfBindingAccessor accessor) {
86         final Class<HistoricalPmList> pmDataClass = HistoricalPmList.class;
87         log.info("Get PM data for element {}", accessor.getNodeId().getValue());
88         InstanceIdentifier<HistoricalPmList> pmDataIid = InstanceIdentifier.builder(pmDataClass).build();
89         return accessor.getTransactionUtils().readData(accessor.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
90                 pmDataIid);
91
92     }
93
94     // Build PM entity for writing into the database
95     public List<PmdataEntity> buildPmDataEntity(HistoricalPmList historicalPmEnitityList) {
96         List<PmdataEntity> pmEntitiyList = new ArrayList<>();
97         if(historicalPmEnitityList==null) {
98             return pmEntitiyList;
99         }
100         Collection<HistoricalPmEntry> pmDataEntryList =
101                 YangHelper.getCollection(historicalPmEnitityList.getHistoricalPmEntry());
102         for (HistoricalPmEntry pmDataEntry : pmDataEntryList) {
103             pmDataBuilder.setUuidInterface(pmDataEntry.getPmResourceType().getName());
104             Collection<HistoricalPm> historicalPmList = YangHelper.getCollection(pmDataEntry.getHistoricalPm());
105             for (HistoricalPm historicalPm : historicalPmList) {
106                 log.info("PmName:{}", historicalPm.getType());
107                 //              pmDataBuilder.setPerformanceData(value)
108
109                 try {
110                     writeperformanceData(historicalPm);
111                 } catch (ClassNotFoundException e) {
112                     log.info("No relevant data found");
113                 }
114                 pmEntitiyList.add(this.pmDataBuilder.build());
115
116                 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
117             }
118             log.info("PmListSize before db writing: {}", pmEntitiyList.size());
119         }
120         return pmEntitiyList;
121     }
122     // end of public methods
123
124     // private methods
125     private void writeperformanceData(HistoricalPm historicalPm) throws ClassNotFoundException {
126         Collection<Measurement> measurementList = YangHelper.getCollection(historicalPm.getMeasurement());
127           Map<MeasurementKey, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement> measurementMap=new HashMap<>();
128         // Map Performance data of PmDataEntity with MeasurmentData-HistoricalPm
129         PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
130         for (Measurement measurementData : measurementList) {
131             this.pmDataBuilder.setGranularityPeriod(mapGranularityPeriod(measurementData.getGranularity()))
132                     .setTimeStamp(measurementData.getCompletionTime());
133             if (measurementData.getValidity().getName().equals("suspect")) {
134                 this.pmDataBuilder.setSuspectIntervalFlag(true);
135             }
136             measurementMap.put(new MeasurementKey(measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
137                     measurementData.getPmParameterValue()).getPmKey()), measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
138                     measurementData.getPmParameterValue()));
139
140
141         }
142
143         pmDataBuilder.setPerformanceData(performanceDataBuilder.setMeasurement(measurementMap).build());
144     }
145
146
147
148     // Mapping Granularity period of PmDataEntity with PmGranularity of MeasurmentData-HistoricalPm
149     private GranularityPeriodType mapGranularityPeriod(PmGranularity pmGranularity) {
150
151         GranularityPeriodType granPeriod = null;
152         switch (pmGranularity.getName()) {
153             case ("notApplicable"):
154                 granPeriod = GranularityPeriodType.Unknown;
155                 break;
156             case ("15min"):
157                 granPeriod = GranularityPeriodType.Period15Min;
158                 break;
159             case ("24Hour"):
160                 granPeriod = GranularityPeriodType.Period24Hours;
161                 break;
162             default:
163                 granPeriod = GranularityPeriodType.Period15Min;
164                 break;
165         }
166         return granPeriod;
167     }
168
169     private List<Class<? extends PerformanceMeasurementTypeId>> setMeasurementTypeId() {
170         String packageName =
171                 "org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413";
172         String packageName1 =
173                 "/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/data/provider/openroadm/pm/types/rev200413/";
174         List<Class<? extends PerformanceMeasurementTypeId>> measTypeObjList =
175                 new ArrayList<>();
176         URL root = Thread.currentThread().getContextClassLoader().getResource(packageName1);
177
178         log.info("path for type package: {}", root);
179
180         Enumeration<URL> results = getFileURL(b, packageName);
181         log.info("FOund Packages {}", results);
182         if(results != null) {
183             while (results.hasMoreElements()) {
184                 URL path = results.nextElement();
185
186                 Class<?> cls1 = loadClass(b, path.getFile());
187
188                 if (PerformanceMeasurementTypeId.class.isAssignableFrom(cls1)) {
189                     measTypeObjList.add((Class<? extends PerformanceMeasurementTypeId>) cls1);
190
191
192                 }
193                 if(cls1!=null) {
194                     log.info("Class Added {}", cls1.getSimpleName());
195                 }
196
197         }
198
199
200         }
201
202         return measTypeObjList;
203     }
204
205     private Class<? extends PerformanceMeasurementUnitId> setMeasurementUnit(String unitName) {
206         Class<? extends PerformanceMeasurementUnitId>  measurementUnitClass = null;
207         switch (unitName) {
208             case ("celsius"):
209                 measurementUnitClass = Celsius.class;
210                 break;
211             case("dB"):
212                 measurementUnitClass = DB.class;
213                 break;
214             case("dBm"):
215                 measurementUnitClass = DBm.class;
216                 break;
217             case("fahrenheit"):
218                 measurementUnitClass = Fahrenheit.class;
219                 break;
220             case("kHz"):
221                 measurementUnitClass = KHz.class;
222                 break;
223             case("mW"):
224                 measurementUnitClass = MW.class;
225                 break;
226             default:
227                 break;
228         }
229             return measurementUnitClass;
230      }
231
232     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement measurementBuilder(
233             PmNamesEnum pmType, String pmUnit, PmDataType pmDataType) {
234
235         MeasurementBuilder measBuilder = new MeasurementBuilder();
236         if (pmType.getName().equals("erroredSeconds") ) {
237             measBuilder.setPmKey(ErroredSecond.class);
238         } else if (pmType.getName().equals("severelyErroredSeconds")) {
239             measBuilder.setPmKey(SeverelyErroredSecond.class);
240         } else {
241             for (Class<? extends PerformanceMeasurementTypeId> obj : setMeasurementTypeId()) {
242                 if (obj.toString().contains(pmType.name())) {
243                     measBuilder.setPmKey(obj);
244                 }
245             }
246         }
247         measBuilder.setPmUnit(setMeasurementUnit(pmUnit));
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 (Exception 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             if (resultUrl != null) {
281                 break;
282
283             }
284
285         }
286
287         return resultUrl;
288     }
289
290
291     // end of private methods
292 }
293