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());
 
  82     // end of constructors
 
  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         if(historicalPmEnitityList==null) {
 
 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)
 
 110                     writeperformanceData(historicalPm);
 
 111                 } catch (ClassNotFoundException e) {
 
 112                     log.info("No relevant data found");
 
 114                 pmEntitiyList.add(this.pmDataBuilder.build());
 
 116                 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
 
 118             log.info("PmListSize before db writing: {}", pmEntitiyList.size());
 
 120         return pmEntitiyList;
 
 122     // end of public 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);
 
 136             measurementMap.put(new MeasurementKey(measurementBuilder(historicalPm.getType(), measurementData.getPmParameterUnit(),
 
 137                     measurementData.getPmParameterValue()).getPmKey()), 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;
 
 169     private List<Class<? extends PerformanceMeasurementTypeId>> setMeasurementTypeId() {
 
 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 =
 
 176         URL root = Thread.currentThread().getContextClassLoader().getResource(packageName1);
 
 178         log.info("path for type package: {}", root);
 
 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();
 
 186                 Class<?> cls1 = loadClass(b, path.getFile());
 
 188                 if (PerformanceMeasurementTypeId.class.isAssignableFrom(cls1)) {
 
 189                     measTypeObjList.add((Class<? extends PerformanceMeasurementTypeId>) cls1);
 
 194                     log.info("Class Added {}", cls1.getSimpleName());
 
 202         return measTypeObjList;
 
 205     private Class<? extends PerformanceMeasurementUnitId> setMeasurementUnit(String unitName) {
 
 206         Class<? extends PerformanceMeasurementUnitId>  measurementUnitClass = null;
 
 209                 measurementUnitClass = Celsius.class;
 
 212                 measurementUnitClass = DB.class;
 
 215                 measurementUnitClass = DBm.class;
 
 218                 measurementUnitClass = Fahrenheit.class;
 
 221                 measurementUnitClass = KHz.class;
 
 224                 measurementUnitClass = MW.class;
 
 229             return measurementUnitClass;
 
 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) {
 
 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);
 
 241             for (Class<? extends PerformanceMeasurementTypeId> obj : setMeasurementTypeId()) {
 
 242                 if (obj.toString().contains(pmType.name())) {
 
 243                     measBuilder.setPmKey(obj);
 
 247         measBuilder.setPmUnit(setMeasurementUnit(pmUnit));
 
 248         measBuilder.setPmValue(pmDataType);
 
 249         return measBuilder.build();
 
 253     private Class<?> loadClass(Bundle bundle, String classFilePath) {
 
 254         String className = classFilePath.replaceFirst("^/", "").replace('/', '.').replaceFirst(".class$", "");
 
 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()));
 
 263     private static Enumeration<URL> getFileURL(Bundle b, String classPath) {
 
 265         BundleContext context = b == null ? null : b.getBundleContext();
 
 266         if (context == null) {
 
 267             log.info("no bundle context available");
 
 270         Bundle[] bundles = context.getBundles();
 
 271         if (bundles == null || bundles.length <= 0) {
 
 272             log.info("no bundles found");
 
 275         log.info("found {} bundles", bundles.length);
 
 276         Enumeration<URL> resultUrl = null;
 
 278         for (Bundle bundle : bundles) {
 
 279             resultUrl = bundle.findEntries("/" + classPath.replace(".", "/"), "*.class", false);
 
 280             if (resultUrl != null) {
 
 291     // end of private methods