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