2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.openroadm.impl;
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;
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;
67 * Reading Openroadm PM data and returning as PmDataEntitiy data
69 public class PmDataBuilderOpenRoadm {
71 private static final Logger log = LoggerFactory.getLogger(PmDataBuilderOpenRoadm.class);
72 private PmdataEntityBuilder pmDataBuilder;
73 private Bundle b = FrameworkUtil.getBundle(this.getClass());
77 public PmDataBuilderOpenRoadm(NetconfBindingAccessor accessor) {
78 this.pmDataBuilder = new PmdataEntityBuilder();
79 this.pmDataBuilder.setNodeName(accessor.getNodeId().getValue());
81 // end of constructors
84 // Instantiate historical PM data list
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,
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());
106 writeperformanceData(historicalPm);
107 } catch (ClassNotFoundException e) {
108 log.info("No relevant data found");
110 pmEntitiyList.add(this.pmDataBuilder.build());
112 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
114 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
116 return pmEntitiyList;
118 // end of public methods
121 // Build performance data of Data provider by mapping values from device
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 =
126 // Map Performance data of PmDataEntity with MeasurmentData-HistoricalPm
127 PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
128 for (Measurement measurementData : measurementList) {
129 this.pmDataBuilder.setGranularityPeriod(mapGranularityPeriod(measurementData.getGranularity()))
130 .setTimeStamp(measurementData.getCompletionTime());
131 if (measurementData.getValidity().getName().equals("suspect")) {
132 this.pmDataBuilder.setSuspectIntervalFlag(true);
135 new MeasurementKey(measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
136 measurementData.getPmParameterValue()).getPmKey()),
137 measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
138 measurementData.getPmParameterValue()));
143 pmDataBuilder.setPerformanceData(performanceDataBuilder.setMeasurement(measurementMap).build());
148 // Mapping Granularity period of PmDataEntity with PmGranularity of MeasurmentData-HistoricalPm
149 private GranularityPeriodType mapGranularityPeriod(PmGranularity pmGranularity) {
151 GranularityPeriodType granPeriod = null;
152 switch (pmGranularity.getName()) {
153 case ("notApplicable"):
154 granPeriod = GranularityPeriodType.Unknown;
157 granPeriod = GranularityPeriodType.Period15Min;
160 granPeriod = GranularityPeriodType.Period24Hours;
163 granPeriod = GranularityPeriodType.Period15Min;
170 // Build the measurement list of data provider based on various values from device
171 private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.pmdata.grp.Measurement measurementBuilder(
172 PmNamesEnum pmType, String pmUnit, PmDataType pmDataType) {
174 MeasurementBuilder measBuilder = new MeasurementBuilder();
175 if (pmType.getName().equals("erroredSeconds")) {
176 measBuilder.setPmKey(ErroredSecond.class);
177 } else if (pmType.getName().equals("severelyErroredSeconds")) {
178 measBuilder.setPmKey(SeverelyErroredSecond.class);
180 for (Class<? extends PerformanceMeasurementTypeId> obj : setMeasurementTypeId()) {
181 if (obj.toString().contains(pmType.name())) {
182 measBuilder.setPmKey(obj);
186 measBuilder.setPmUnit(setMeasurementUnit(pmUnit));
187 measBuilder.setPmValue(pmDataType);
188 return measBuilder.build();
191 // Find the proper PerformanceMeasurementTypeId class based on the PmNames Enum value from device
192 private List<Class<? extends PerformanceMeasurementTypeId>> setMeasurementTypeId() {
194 "org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.openroadm.pm.types.rev200413";
195 String packageName1 =
196 "/org/opendaylight/yang/gen/v1/urn/opendaylight/params/xml/ns/yang/data/provider/openroadm/pm/types/rev200413/";
197 List<Class<? extends PerformanceMeasurementTypeId>> measTypeObjList = new ArrayList<>();
198 URL root = Thread.currentThread().getContextClassLoader().getResource(packageName1);
200 log.info("path for type package: {}", root);
202 Enumeration<URL> results = getFileURL(b, packageName);
203 log.info("FOund Packages {}", results);
204 if (results != null) {
205 while (results.hasMoreElements()) {
206 URL path = results.nextElement();
208 Class<?> cls1 = loadClass(b, path.getFile());
210 if (PerformanceMeasurementTypeId.class.isAssignableFrom(cls1)) {
211 measTypeObjList.add((Class<? extends PerformanceMeasurementTypeId>) cls1);
216 log.info("Class Added {}", cls1.getSimpleName());
224 return measTypeObjList;
227 //Map matching PerformanceMeasurementUnitId class based on the string value from device
228 private Class<? extends PerformanceMeasurementUnitId> setMeasurementUnit(String unitName) {
229 Class<? extends PerformanceMeasurementUnitId> measurementUnitClass = null;
232 measurementUnitClass = Celsius.class;
235 measurementUnitClass = DB.class;
238 measurementUnitClass = DBm.class;
241 measurementUnitClass = Fahrenheit.class;
244 measurementUnitClass = KHz.class;
247 measurementUnitClass = MW.class;
252 return measurementUnitClass;
256 private Class<?> loadClass(Bundle bundle, String classFilePath) {
257 String className = classFilePath.replaceFirst("^/", "").replace('/', '.').replaceFirst(".class$", "");
259 return bundle.loadClass(className);
260 } catch (Exception e) {
261 log.info(String.format("Class [%s] could not be loaded. Message: [%s].", className, e.getMessage()));
266 private static Enumeration<URL> getFileURL(Bundle b, String classPath) {
268 BundleContext context = b == null ? null : b.getBundleContext();
269 if (context == null) {
270 log.info("no bundle context available");
273 Bundle[] bundles = context.getBundles();
274 if (bundles == null || bundles.length <= 0) {
275 log.info("no bundles found");
278 log.info("found {} bundles", bundles.length);
279 Enumeration<URL> resultUrl = null;
281 for (Bundle bundle : bundles) {
282 resultUrl = bundle.findEntries("/" + classPath.replace(".", "/"), "*.class", false);
283 if (resultUrl != null) {
294 // end of private methods