2  * ============LICENSE_START=======================================================
 
   3  * ONAP : ccsdk features
 
   4  * ================================================================================
 
   5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
 
   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
 
  12  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification;
 
  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;
 
  54 public class ORanDOMFaultNotificationListener implements DOMNotificationListener {
 
  56     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMFaultNotificationListener.class);
 
  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;
 
  66     private Integer counter; //Local counter is assigned to Notifications
 
  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();
 
  78                 new ORanDOMFaultToVESFaultMapper(netconfDomAccessor.getNodeId(), vesCollectorService, "AlarmNotif");
 
  83     public void onNotification(@NonNull DOMNotification notification) {
 
  84         onAlarmNotif(notification);
 
  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.
 
  92      * @param componentList
 
  94     public void setComponentList(Collection<MapEntryNode> componentList) {
 
  95         for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
 
  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));
 
 108     public void onAlarmNotif(DOMNotification notification) {
 
 110         LOG.debug("onAlarmNotif {}", notification.getClass().getSimpleName());
 
 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());
 
 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);
 
 128         } catch (JsonProcessingException | DateTimeParseException e) {
 
 129             LOG.debug("Can not convert event into VES message {}", notification, e);
 
 133     private void writeToEventLog(String data, Instant eventTimeInstant, String notificationName, int sequenceNo) {
 
 134         EventlogBuilder eventlogBuilder = new EventlogBuilder();
 
 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);
 
 150         eventlogBuilder.setNewValue(data);
 
 151         eventlogBuilder.setSourceType(SourceType.Netconf);
 
 152         eventlogBuilder.setTimestamp(ORanDOMToInternalDataModel.getDateAndTimeOfInstant(eventTimeInstant));
 
 154         databaseService.writeEventLog(eventlogBuilder.build());
 
 157     private String getShortEventLogMessage(String data) {
 
 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) {
 
 165             return "Invalid message received";
 
 169     private String getMediumEventLogMessage(String data) {
 
 171             JSONObject jsonObj = new JSONObject(data);
 
 172             return jsonObj.getJSONObject("event").getJSONObject("commonEventHeader").toString();
 
 173         } catch (JSONException e) {
 
 175             return "Invalid message received";