3e99bcd6de23e42d319086a5837ffcfa05488047
[ccsdk/features.git] /
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 Notifications
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 =
78                 new ORanDOMFaultToVESFaultMapper(netconfDomAccessor.getNodeId(), vesCollectorService, "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(), counter));
116         // Send model specific notification to WebSocketManager
117         websocketManagerService.sendNotification(notification, netconfDomAccessor.getNodeId(), oranfm.getAlarmNotifQName());
118
119         try {
120             if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
121                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(notification, eventTimeInstant, counter);
122                 VESFaultFieldsPOJO body = mapper.mapFaultFields(notification, oranfm);
123                 VESMessage vesMsg = vesCollectorService.generateVESEvent(header, body);
124                 vesCollectorService.publishVESMessage(vesMsg);
125                 LOG.debug("VES Message is  {}", vesMsg.getMessage());
126                 writeToEventLog(vesMsg.getMessage(), eventTimeInstant, oranfm.getAlarmNotifQName().getLocalName(), counter);
127             }
128         } catch (JsonProcessingException | DateTimeParseException e) {
129             LOG.debug("Can not convert event into VES message {}", notification, e);
130         }
131     }
132
133     private void writeToEventLog(String data, Instant eventTimeInstant, String notificationName, int sequenceNo) {
134         EventlogBuilder eventlogBuilder = new EventlogBuilder();
135
136         eventlogBuilder.setObjectId("Device");
137         eventlogBuilder.setCounter(sequenceNo);
138         eventlogBuilder.setAttributeName(notificationName);
139         eventlogBuilder.setNodeId(netconfDomAccessor.getNodeId().getValue());
140         String eventLogMsgLvl = vesCollectorService.getConfig().getEventLogMsgDetail();
141         if (eventLogMsgLvl.equalsIgnoreCase("SHORT")) {
142             data = getShortEventLogMessage(data);
143         } else if (eventLogMsgLvl.equalsIgnoreCase("MEDIUM")) {
144             data = getMediumEventLogMessage(data);
145         } else if (eventLogMsgLvl.equalsIgnoreCase("LONG")) {
146             // do nothing, data already contains long message
147         } else { // Unknown value, default to "SHORT"
148             data = getShortEventLogMessage(data);
149         }
150         eventlogBuilder.setNewValue(data);
151         eventlogBuilder.setSourceType(SourceType.Netconf);
152         eventlogBuilder.setTimestamp(ORanDOMToInternalDataModel.getDateAndTimeOfInstant(eventTimeInstant));
153
154         databaseService.writeEventLog(eventlogBuilder.build());
155     }
156
157     private String getShortEventLogMessage(String data) {
158         try {
159             JSONObject jsonObj = new JSONObject(data);
160             String domain = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("domain");
161             String eventId = jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").getString("eventId");
162             return "domain:" + domain + " eventId:" + eventId;
163         } catch (JSONException e) {
164             LOG.debug("{}", e);
165             return "Invalid message received";
166         }
167     }
168
169     private String getMediumEventLogMessage(String data) {
170         try {
171             JSONObject jsonObj = new JSONObject(data);
172             return jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").toString();
173         } catch (JSONException e) {
174             LOG.debug("{}", e);
175             return "Invalid message received";
176         }
177     }
178 }