Align initial alarms and notifications parsing
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / notification / ORanDOMFaultNotificationListener.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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.oran.notification;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import java.time.Instant;
26 import java.time.format.DateTimeParseException;
27 import java.util.Collection;
28 import java.util.Objects;
29 import java.util.Optional;
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.dataprovider.ORanDOMToInternalDataModel;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDMDOMUtility;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.vesmapper.ORanDOMFaultToVESFaultMapper;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.yangspecs.ORANFM;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESFaultFieldsPOJO;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
45 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService;
46 import org.opendaylight.mdsal.dom.api.DOMNotification;
47 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class ORanDOMFaultNotificationListener implements DOMNotificationListener {
55
56     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMFaultNotificationListener.class);
57
58     private final @NonNull NetconfDomAccessor netconfDomAccessor;
59     private final @NonNull VESCollectorService vesCollectorService;
60     private final @NonNull ORanDOMFaultToVESFaultMapper mapper;
61     private final @NonNull FaultService faultService;
62     private final @NonNull WebsocketManagerService websocketManagerService;
63     private final @NonNull DataProvider databaseService;
64     private final @NonNull ORANFM oranfm;
65
66     private Integer counter; //Local counter is assigned to Events in EventLog
67
68     public ORanDOMFaultNotificationListener(@NonNull NetconfDomAccessor netconfDomAccessor, Optional<ORANFM> oranfm,
69             @NonNull VESCollectorService vesCollectorService, @NonNull FaultService faultService,
70             @NonNull WebsocketManagerService websocketManagerService, @NonNull DataProvider databaseService) {
71         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
72         this.vesCollectorService = Objects.requireNonNull(vesCollectorService);
73         this.faultService = Objects.requireNonNull(faultService);
74         this.websocketManagerService = Objects.requireNonNull(websocketManagerService);
75         this.databaseService = Objects.requireNonNull(databaseService);
76         this.oranfm = oranfm.get();
77         this.mapper = new ORanDOMFaultToVESFaultMapper(netconfDomAccessor.getNodeId(), this.vesCollectorService,
78                 this.oranfm, "AlarmNotif");
79         this.counter = 0;
80     }
81
82     @Override
83     public void onNotification(@NonNull DOMNotification notification) {
84         onAlarmNotif(notification);
85     }
86
87     /**
88      * Gets the mfg name, mode-name and Uuid of the root component (Ex: Chassis.) In cases where there are multiple root
89      * components i.e., components with no parent, the Uuid of the last occurred component from the componentList will
90      * be considered. Till now we haven't seen Uuid set for root components, so not an issue for now.
91      *
92      * @param componentList
93      */
94     public void setComponentList(Collection<MapEntryNode> componentList) {
95         for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
96             mapper.setMfgName(
97                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MFG_NAME));
98             mapper.setUuid(ORanDMDOMUtility.getLeafValue(component,
99                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_UUID) != null
100                             ? ORanDMDOMUtility.getLeafValue(component,
101                                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_UUID)
102                             : netconfDomAccessor.getNodeId().getValue());
103             mapper.setModelName(ORanDMDOMUtility.getLeafValue(component,
104                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MODEL_NAME));
105         }
106     }
107
108     public void onAlarmNotif(DOMNotification notification) {
109
110         LOG.debug("onAlarmNotif {}", notification.getClass().getSimpleName());
111         counter++;
112         // Send devicemanager specific notification for database and ODLUX
113         Instant eventTimeInstant = ORanDMDOMUtility.getNotificationInstant(notification);
114         faultService.faultNotification(
115                 ORanDOMToInternalDataModel.getFaultLog(notification, oranfm, netconfDomAccessor.getNodeId()));
116         // Send model specific notification to WebSocketManager
117         websocketManagerService.sendNotification(notification, netconfDomAccessor.getNodeId(),
118                 oranfm.getAlarmNotifQName());
119
120         try {
121             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
122                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(notification, eventTimeInstant);
123                 VESFaultFieldsPOJO body = mapper.mapFaultFields(notification);
124                 VESMessage vesMsg = vesCollectorService.generateVESEvent(header, body);
125                 vesCollectorService.publishVESMessage(vesMsg);
126                 LOG.debug("VES Message is  {}", vesMsg.getMessage());
127                 writeToEventLog(vesMsg.getMessage(), eventTimeInstant, oranfm.getAlarmNotifQName().getLocalName(),
128                         counter);
129             }
130         } catch (JsonProcessingException | DateTimeParseException e) {
131             LOG.debug("Can not convert event into VES message {}", notification, e);
132         }
133     }
134
135     private void writeToEventLog(String data, Instant eventTimeInstant, String notificationName, int sequenceNo) {
136         EventlogBuilder eventlogBuilder = new EventlogBuilder();
137
138         eventlogBuilder.setObjectId("Device");
139         eventlogBuilder.setCounter(sequenceNo);
140         eventlogBuilder.setAttributeName(notificationName);
141         eventlogBuilder.setNodeId(netconfDomAccessor.getNodeId().getValue());
142         String eventLogMsgLvl = vesCollectorService.getConfig().getEventLogMsgDetail();
143         if (eventLogMsgLvl.equalsIgnoreCase("SHORT")) {
144             data = getShortEventLogMessage(data);
145         } else if (eventLogMsgLvl.equalsIgnoreCase("MEDIUM")) {
146             data = getMediumEventLogMessage(data);
147         } else if (eventLogMsgLvl.equalsIgnoreCase("LONG")) {
148             // do nothing, data already contains long message
149         } else { // Unknown value, default to "SHORT"
150             data = getShortEventLogMessage(data);
151         }
152         eventlogBuilder.setNewValue(data);
153         eventlogBuilder.setSourceType(SourceType.Netconf);
154         eventlogBuilder.setTimestamp(ORanDMDOMUtility.getDateAndTimeOfInstant(eventTimeInstant));
155
156         databaseService.writeEventLog(eventlogBuilder.build());
157     }
158
159     private String getShortEventLogMessage(String data) {
160         try {
161             JSONObject jsonObj = new JSONObject(data);
162             String domain = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("domain");
163             String eventId = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("eventId");
164             return "domain:" + domain + " eventId:" + eventId;
165         } catch (JSONException e) {
166             LOG.debug("{}", e);
167             return "Invalid message received";
168         }
169     }
170
171     private String getMediumEventLogMessage(String data) {
172         try {
173             JSONObject jsonObj = new JSONObject(data);
174             return jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").toString();
175         } catch (JSONException e) {
176             LOG.debug("{}", e);
177             return "Invalid message received";
178         }
179     }
180 }