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