f1fd4471f9bc25ece16787f3465c0d0551cec268
[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         Collection<HistoricalPmEntry> pmDataEntryList =
98                 YangHelper.getCollection(historicalPmEnitityList.getHistoricalPmEntry());
99         for (HistoricalPmEntry pmDataEntry : pmDataEntryList) {
100             pmDataBuilder.setUuidInterface(pmDataEntry.getPmResourceType().getName());
101             Collection<HistoricalPm> historicalPmList = YangHelper.getCollection(pmDataEntry.getHistoricalPm());
102             for (HistoricalPm historicalPm : historicalPmList) {
103                 log.info("PmName:{}", historicalPm.getType());
104                 //              pmDataBuilder.setPerformanceData(value)
105
106                 try {
107                     writeperformanceData(historicalPm);
108                 } catch (ClassNotFoundException e) {
109                     log.info("No relevant data found");
110                 }
111                 pmEntitiyList.add(this.pmDataBuilder.build());
112
113                 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
114             }
115             log.info("PmListSize before db writing: {}", pmEntitiyList.size());
116         }
117         return pmEntitiyList;
118     }
119     // end of public methods
120
121     // private methods
122     private void writeperformanceData(HistoricalPm historicalPm) throws ClassNotFoundException {
123         Collection<Measurement> measurementList = YangHelper.getCollection(historicalPm.getMeasurement());
124           Map<MeasurementKey, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement> measurementMap=new HashMap<>();
125         // Map Performance data of PmDataEntity with MeasurmentData-HistoricalPm
126         PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
127         for (Measurement measurementData : measurementList) {
128             this.pmDataBuilder.setGranularityPeriod(mapGranularityPeriod(measurementData.getGranularity()))
129                     .setTimeStamp(measurementData.getCompletionTime());
130             if (measurementData.getValidity().getName().equals("suspect")) {
131                 this.pmDataBuilder.setSuspectIntervalFlag(true);
132             }
133             measurementMap.put(new MeasurementKey(measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
134                     measurementData.getPmParameterValue()).getPmKey()), measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
135                     measurementData.getPmParameterValue()));
136
137
138         }
139
140         pmDataBuilder.setPerformanceData(performanceDataBuilder.setMeasurement(measurementMap).build());
141     }
142
143
144
145     // Mapping Granularity period of PmDataEntity with PmGranularity of MeasurmentData-HistoricalPm
146     private GranularityPeriodType mapGranularityPeriod(PmGranularity pmGranularity) {
147
148         GranularityPeriodType granPeriod = null;
149         switch (pmGranularity.getName()) {
150             case ("notApplicable"):
151                 granPeriod = GranularityPeriodType.Unknown;
152                 break;
153             case ("15min"):
154                 granPeriod = GranularityPeriodType.Period15Min;
155                 break;
156             case ("24Hour"):
157                 granPeriod = GranularityPeriodType.Period24Hours;
158                 break;
159             default:
160                 granPeriod = GranularityPeriodType.Period15Min;
161                 break;
162         }
163         return granPeriod;
164     }
165
166     private List<Class<? extends PerformanceMeasurementTypeId>> setMeasurementTypeId() {
167         String packageName =
168                 "org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413";
169         String packageName1 =
170                 "/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/data/provider/openroadm/pm/types/rev200413/";
171         List<Class<? extends PerformanceMeasurementTypeId>> measTypeObjList =
172                 new ArrayList<>();
173         URL root = Thread.currentThread().getContextClassLoader().getResource(packageName1);
174
175         log.info("path for type package: {}", root);
176
177         Enumeration<URL> results = getFileURL(b, packageName);
178         log.info("FOund Packages {}", results);
179         if(results != null) {
180             while (results.hasMoreElements()) {
181                 URL path = results.nextElement();
182
183                 Class<?> cls1 = loadClass(b, path.getFile());
184
185                 if (PerformanceMeasurementTypeId.class.isAssignableFrom(cls1)) {
186                     measTypeObjList.add((Class<? extends PerformanceMeasurementTypeId>) cls1);
187
188
189                 }
190                 if(cls1!=null) {
191                     log.info("Class Added {}", cls1.getSimpleName());
192                 }
193
194         }
195
196
197         }
198
199         return measTypeObjList;
200     }
201
202     private Class<? extends PerformanceMeasurementUnitId> setMeasurementUnit(String unitName) {
203         Class<? extends PerformanceMeasurementUnitId>  measurementUnitClass = null;
204         switch (unitName) {
205             case ("celsius"):
206                 measurementUnitClass = Celsius.class;
207                 break;
208             case("dB"):
209                 measurementUnitClass = DB.class;
210                 break;
211             case("dBm"):
212                 measurementUnitClass = DBm.class;
213                 break;
214             case("fahrenheit"):
215                 measurementUnitClass = Fahrenheit.class;
216                 break;
217             case("kHz"):
218                 measurementUnitClass = KHz.class;
219                 break;
220             case("mW"):
221                 measurementUnitClass = MW.class;
222                 break;
223             default:
224                 break;
225         }
226             return measurementUnitClass;
227      }
228
229     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement measurementBuilder(
230             PmNamesEnum pmType, String pmUnit, PmDataType pmDataType) {
231
232         MeasurementBuilder measBuilder = new MeasurementBuilder();
233         if (pmType.getName().equals("erroredSeconds") ) {
234             measBuilder.setPmKey(ErroredSecond.class);
235         } else if (pmType.getName().equals("severelyErroredSeconds")) {
236             measBuilder.setPmKey(SeverelyErroredSecond.class);
237         } else {
238             for (Class<? extends PerformanceMeasurementTypeId> obj : setMeasurementTypeId()) {
239                 if (obj.toString().contains(pmType.name())) {
240                     measBuilder.setPmKey(obj);
241                 }
242             }
243         }
244         measBuilder.setPmUnit(setMeasurementUnit(pmUnit));
245         measBuilder.setPmValue(pmDataType);
246         return measBuilder.build();
247
248     }
249
250     private Class<?> loadClass(Bundle bundle, String classFilePath) {
251         String className = classFilePath.replaceFirst("^/", "").replace('/', '.').replaceFirst(".class$", "");
252         try {
253             return bundle.loadClass(className);
254         } catch (Exception e) {
255             log.info(String.format("Class [%s] could not be loaded. Message: [%s].", className, e.getMessage()));
256         }
257         return null;
258     }
259
260     private static Enumeration<URL> getFileURL(Bundle b, String classPath) {
261
262         BundleContext context = b == null ? null : b.getBundleContext();
263         if (context == null) {
264             log.info("no bundle context available");
265             return null;
266         }
267         Bundle[] bundles = context.getBundles();
268         if (bundles == null || bundles.length <= 0) {
269             log.info("no bundles found");
270             return null;
271         }
272         log.info("found {} bundles", bundles.length);
273         Enumeration<URL> resultUrl = null;
274
275         for (Bundle bundle : bundles) {
276             resultUrl = bundle.findEntries("/" + classPath.replace(".", "/"), "*.class", false);
277             if (resultUrl != null) {
278                 break;
279
280             }
281
282         }
283
284         return resultUrl;
285     }
286
287
288     // end of private methods
289 }
290