755f8ed9f6a782499a299d2197470aaae7b9b446
[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.util.ArrayList;
25 import java.util.List;
26 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.HistoricalPmList;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.group.HistoricalPm;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.list.HistoricalPmEntry;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev191129.historical.pm.val.group.Measurement;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev191129.PmGranularity;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.GranularityPeriodType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.PmdataEntity;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.PmdataEntityBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.pmdata.entity.PerformanceData;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.pmdata.entity.PerformanceDataBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * @author shabnam
44  *
45  *         Reading Openroadm PM data and returning as PmDataEntitiy data
46  */
47 public class PmDataBuilderOpenRoadm {
48     // variables
49     private static final Logger log = LoggerFactory.getLogger(OpenroadmNetworkElement.class);
50     private PmdataEntityBuilder pmDataBuilder;
51
52     // end of variables
53     // constructors
54     public PmDataBuilderOpenRoadm(NetconfAccessor accessor) {
55         this.pmDataBuilder = new PmdataEntityBuilder();
56         this.pmDataBuilder.setNodeName(accessor.getNodeId().getValue());
57     }
58
59     // end of constructors
60     // public methods
61     // Read PM data
62     public HistoricalPmList getPmData(NetconfAccessor accessor) {
63         final Class<HistoricalPmList> pmDataClass = HistoricalPmList.class;
64         log.info("Get PM data for element {}", accessor.getNodeId().getValue());
65         InstanceIdentifier<HistoricalPmList> pmDataIid = InstanceIdentifier.builder(pmDataClass).build();
66         return accessor.getTransactionUtils().readData(accessor.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
67                 pmDataIid);
68
69     }
70
71     // Build PM entity for writing into the database
72     public List<PmdataEntity> buildPmDataEntity(HistoricalPmList historicalPmEnitityList) {
73         List<PmdataEntity> pmEntitiyList = new ArrayList<>();
74         List<HistoricalPmEntry> pmDataEntryList = historicalPmEnitityList.getHistoricalPmEntry();
75         for (HistoricalPmEntry pmDataEntry : pmDataEntryList) {
76             pmDataBuilder.setUuidInterface(pmDataEntry.getPmResourceType().getName());
77             List<HistoricalPm> historicalPmList = pmDataEntry.getHistoricalPm();
78             for (HistoricalPm historicalPm : historicalPmList) {
79                 log.info("PmName:{}", historicalPm.getType());
80                 this.pmDataBuilder.setScannerId(historicalPm.getType().getName());
81                 writeperformanceData(historicalPm);
82                 log.info("NodeName: {}, Scanner Id:{}, Period: {}", this.pmDataBuilder.getNodeName(),
83                         this.pmDataBuilder.getScannerId(), this.pmDataBuilder.getGranularityPeriod().getName());
84                 pmEntitiyList.add(this.pmDataBuilder.build());
85                 log.info("PmListSize before db writing: {}", pmEntitiyList.size());
86             }
87             log.info("PmListSize before db writing: {}", pmEntitiyList.size());
88         }
89         return pmEntitiyList;
90     }
91     // end of public methods
92
93     // private methods
94     private void writeperformanceData(HistoricalPm historicalPm) {
95         List<Measurement> measurementList = historicalPm.getMeasurement();
96
97         for (Measurement measurementData : measurementList) {
98             this.pmDataBuilder.setGranularityPeriod(mapGranularityPeriod(measurementData.getGranularity()))
99                     .setPerformanceData(getPerformancedata(measurementData))
100                     .setTimeStamp(measurementData.getCompletionTime());
101             if (measurementData.getValidity().getName().equals("suspect")) {
102                 this.pmDataBuilder.setSuspectIntervalFlag(true);
103             }
104             log.info("Time:d{}, \n Scannerid: {}, \n UUID: {}", this.pmDataBuilder.getGranularityPeriod().getName(),
105                     this.pmDataBuilder.getScannerId(), this.pmDataBuilder.getUuidInterface());
106         }
107     }
108
109     //Map Performance data of PmDataEntity with  MeasurmentData-HistoricalPm
110     private PerformanceData getPerformancedata(Measurement measurementData) {
111         PerformanceData performanceData;
112         PerformanceDataBuilder performanceDataBuilder = new PerformanceDataBuilder();
113         performanceData = performanceDataBuilder.setCses(measurementData.getBinNumber())
114                 .setSes(measurementData.getPmParameterValue().getUint64().intValue()).build();
115         return performanceData;
116     }
117
118     // Mapping Granularity period of PmDataEntity with PmGranularity of MeasurmentData-HistoricalPm
119     private GranularityPeriodType mapGranularityPeriod(PmGranularity pmGranularity) {
120
121         GranularityPeriodType granPeriod = null;
122         switch (pmGranularity.getName()) {
123             case ("notApplicable"):
124                 granPeriod = GranularityPeriodType.Unknown;
125                 break;
126             case ("15min"):
127                 granPeriod = GranularityPeriodType.Period15Min;
128                 break;
129             case ("24Hour"):
130                 granPeriod = GranularityPeriodType.Period24Hours;
131                 break;
132             default:
133                 granPeriod = GranularityPeriodType.Period15Min;
134                 break;
135         }
136         return granPeriod;
137     }
138     // end of private methods
139 }