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