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