6472e66532e77dbed229f1e1a9d8b7f227e12f9b
[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         ActiveAlarmList actAlarmList = this.getActiveAlarmList(this.netConfAccesor);
72         if (actAlarmList != null) {
73             Collection<ActiveAlarms> activeAlarms = YangHelper.getCollection(actAlarmList.getActiveAlarms());
74             if (!activeAlarms.isEmpty()) {
75                 for (ActiveAlarms activeAlarm : activeAlarms) {
76                     faultData.add(this.netConfAccesor.getNodeId(), this.count, activeAlarm.getRaiseTime(),
77                             activeAlarm.getResource().getDevice().getNodeId().getValue(),
78                             activeAlarm.getProbableCause().getCause().getName(),
79                             checkSeverityValue(activeAlarm.getSeverity()));
80                     count = count + 1;
81                 }
82                 return faultData;
83             }
84         }
85         return faultData;
86     }
87
88     // Write into the FaultLog
89     protected void writeAlarmLog(FaultData faultData) {
90         if (faultData != null) {
91             List<Faultlog> faultLog = faultData.getProblemList();
92             for (Faultlog fe : faultLog) {
93                 this.dataProvider.writeFaultLog(fe);
94             }
95         }
96     }
97
98     // Use the FaultService for Alarm notifications
99     protected void faultService() {
100         this.faultEventListener.initCurrentProblemStatus(this.netConfAccesor.getNodeId(), writeFaultData());
101         writeAlarmLog(writeFaultData());
102     }
103     // Mapping Severity of AlarmNotification to SeverityType of FaultLog
104     protected static SeverityType checkSeverityValue(Severity severity) {
105         SeverityType severityType = null;
106         log.info("Device Severity: {}", severity.getName());
107
108         switch (severity.getName()) {
109             case ("warning"):
110                 severityType = SeverityType.Warning;
111                 break;
112             case ("major"):
113                 severityType = SeverityType.Major;
114                 break;
115             case ("minor"):
116                 severityType = SeverityType.Minor;
117                 break;
118             case ("clear"):
119                 severityType = SeverityType.NonAlarmed;
120                 break;
121             case ("critical"):
122                 severityType = SeverityType.Critical;
123                 break;
124             case ("indeterminate"):
125                 severityType = SeverityType.Critical;
126                 break;
127             default:
128                 severityType = SeverityType.Critical;
129                 break;
130
131         }
132         return severityType;
133
134     }
135     // end of protected methods
136
137     // private methods
138
139     // Read Alarm Data
140     private ActiveAlarmList getActiveAlarmList(NetconfBindingAccessor accessor) {
141         final Class<ActiveAlarmList> classAlarm = ActiveAlarmList.class;
142         log.info("Get Alarm data for element {}", accessor.getNodeId().getValue());
143         InstanceIdentifier<ActiveAlarmList> alarmDataIid = InstanceIdentifier.builder(classAlarm).build();
144
145         ActiveAlarmList alarmData = accessor.getTransactionUtils().readData(accessor.getDataBroker(),
146                 LogicalDatastoreType.OPERATIONAL, alarmDataIid);
147
148         log.info("AlarmData {}", alarmData);
149         return alarmData;
150     }
151
152
153     // end of private methods
154
155
156
157 }