6c8837327e55ec77d5ac0bd7b3d71cd9fba81a5e
[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.Collection;
25 import java.util.List;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
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.alarm.rev191129.ActiveAlarmList;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev191129.Severity;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev191129.active.alarm.list.ActiveAlarms;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Faultlog;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * @author Shabnam Sultana
45  *
46  *         Class to read the initial alarms at the time of device registration
47  *
48  **/
49
50 public class InitialDeviceAlarmReader {
51     // variables
52     private Integer count = 1;
53     private static final Logger log = LoggerFactory.getLogger(InitialDeviceAlarmReader.class);
54     private final NetconfBindingAccessor netConfAccesor;
55     private final @NonNull FaultService faultEventListener;
56     private final DataProvider dataProvider;
57     // end of variables
58
59     // constructors
60     public InitialDeviceAlarmReader(NetconfBindingAccessor accessor, DeviceManagerServiceProvider serviceProvider) {
61         this.netConfAccesor = accessor;
62         this.faultEventListener = serviceProvider.getFaultService();
63         this.dataProvider = serviceProvider.getDataProvider();
64     }
65     // end of constructors
66
67     // protected methods
68     // Mapping the alarm data with the fault data
69     protected FaultData writeFaultData() {
70         FaultData faultData = new FaultData();
71         if (this.getActiveAlarmList(this.netConfAccesor).getActiveAlarms() != null) {
72             Collection<ActiveAlarms> activeAlarms = YangHelper.getCollection(this.getActiveAlarmList(this.netConfAccesor).getActiveAlarms());
73             if (!activeAlarms.isEmpty()) {
74                 for (ActiveAlarms activeAlarm : activeAlarms) {
75                     faultData.add(this.netConfAccesor.getNodeId(), this.count, activeAlarm.getRaiseTime(),
76                             activeAlarm.getResource().getDevice().getNodeId().getValue(),
77                             activeAlarm.getProbableCause().getCause().getName(),
78                             checkSeverityValue(activeAlarm.getSeverity()));
79                     count = count + 1;
80                 }
81                 return faultData;
82             }
83         }
84         return faultData;
85     }
86
87     // Write into the FaultLog
88     protected void writeAlarmLog(FaultData faultData) {
89         if (faultData != null) {
90             List<Faultlog> faultLog = faultData.getProblemList();
91             for (Faultlog fe : faultLog) {
92                 this.dataProvider.writeFaultLog(fe);
93             }
94         }
95     }
96
97     // Use the FaultService for Alarm notifications
98     protected void faultService() {
99         this.faultEventListener.initCurrentProblemStatus(this.netConfAccesor.getNodeId(), writeFaultData());
100         writeAlarmLog(writeFaultData());
101     }
102     // Mapping Severity of AlarmNotification to SeverityType of FaultLog
103     protected static SeverityType checkSeverityValue(Severity severity) {
104         SeverityType severityType = null;
105         log.info("Device Severity: {}", severity.getName());
106
107         switch (severity.getName()) {
108             case ("warning"):
109                 severityType = SeverityType.Warning;
110                 break;
111             case ("major"):
112                 severityType = SeverityType.Major;
113                 break;
114             case ("minor"):
115                 severityType = SeverityType.Minor;
116                 break;
117             case ("clear"):
118                 severityType = SeverityType.NonAlarmed;
119                 break;
120             case ("critical"):
121                 severityType = SeverityType.Critical;
122                 break;
123             case ("indeterminate"):
124                 severityType = SeverityType.Critical;
125                 break;
126             default:
127                 severityType = SeverityType.Critical;
128                 break;
129
130         }
131         return severityType;
132
133     }
134     // end of protected methods
135
136     // private methods
137
138     // Read Alarm Data
139     private ActiveAlarmList getActiveAlarmList(NetconfBindingAccessor accessor) {
140         final Class<ActiveAlarmList> classAlarm = ActiveAlarmList.class;
141         log.info("Get Alarm data for element {}", accessor.getNodeId().getValue());
142         InstanceIdentifier<ActiveAlarmList> alarmDataIid = InstanceIdentifier.builder(classAlarm).build();
143
144         ActiveAlarmList alarmData = accessor.getTransactionUtils().readData(accessor.getDataBroker(),
145                 LogicalDatastoreType.OPERATIONAL, alarmDataIid);
146
147         log.info("AlarmData {}", alarmData);
148         return alarmData;
149     }
150
151
152     // end of private methods
153
154
155
156 }