b4f0fe01059c1668e4d42a023b7ba6783b204f7e
[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 org.eclipse.jdt.annotation.NonNull;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
34 import org.opendaylight.mdsal.dom.api.DOMEvent;
35 import org.opendaylight.mdsal.dom.api.DOMNotification;
36 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
44 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class ORanDOMChangeNotificationListener implements DOMNotificationListener {
49
50     private static final Logger log = LoggerFactory.getLogger(ORanDOMChangeNotificationListener.class);
51
52     private final NetconfDomAccessor netconfDomAccessor;
53     private final DataProvider databaseService;
54     private @NonNull VESCollectorService vesCollectorService;
55     private final ORanDOMNotificationToXPath domNotificationXPath;
56     private ORanDOMNotifToVESEventAssembly mapper = null;
57     private static int sequenceNo = 0;
58
59     public ORanDOMChangeNotificationListener(NetconfDomAccessor netconfDomAccessor,
60             @NonNull VESCollectorService vesCollectorService, DataProvider databaseService) {
61         this.netconfDomAccessor = netconfDomAccessor;
62         this.databaseService = databaseService;
63         this.vesCollectorService = vesCollectorService;
64         domNotificationXPath = new ORanDOMNotificationToXPath();
65     }
66
67     @Override
68     public void onNotification(@NonNull DOMNotification domNotification) {
69         if (domNotification.getType()
70                 .equals(Absolute.of(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE))) {
71             handleNetconfConfigChange(domNotification);
72         }
73     }
74
75     private void handleNetconfConfigChange(@NonNull DOMNotification domNotification) {
76         DateAndTime eventTime;
77         Instant notificationEventTime = null;
78         if (domNotification instanceof DOMEvent) {
79             notificationEventTime = ((DOMEvent) domNotification).getEventInstant();
80             eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp(notificationEventTime.toString());
81         } else {
82             eventTime = NetconfTimeStampImpl.getConverter().getTimeStamp();
83         }
84
85         ContainerNode cn = domNotification.getBody();
86
87         // Process the changed-by child
88         //              ContainerNode changedByContainerNode = (ContainerNode) cn
89         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_CHANGEDBY));
90         //              ChoiceNode serverOrUserIDVal = (ChoiceNode) changedByContainerNode
91         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SERVERORUSER));
92         //              @SuppressWarnings("unused")
93         //              String userIDValue = serverOrUserIDVal
94         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_USERNAME)).body()
95         //                              .toString();
96         //              @SuppressWarnings("unused")
97         //              Integer sessionIDVal = Integer.valueOf(serverOrUserIDVal
98         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SESSIONID)).body()
99         //                              .toString());
100         //
101         //              // Process the datastore child
102         //              @SuppressWarnings("unused")
103         //              String datastoreValue = cn
104         //                              .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_DATASTORE)).body()
105         //                              .toString();
106
107         // Process the edit child
108         UnkeyedListNode editList = (UnkeyedListNode) cn
109                 .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_EDITNODE));
110         if (editList != null) {
111             for (int listCnt = 0; listCnt < editList.size(); listCnt++) {
112                 String operationValue = editList.childAt(listCnt)
113                         .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_OPERATION))
114                         .body().toString();
115                 String targetValue = editList.childAt(listCnt)
116                         .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_TARGET))
117                         .body().toString();
118
119                 EventlogEntity eventLogEntity1 = new EventlogBuilder()
120                         .setNodeId(netconfDomAccessor.getNodeId().getValue()).setCounter(sequenceNo++)
121                         .setTimestamp(eventTime).setObjectId(targetValue).setAttributeName("N.A")
122                         .setSourceType(SourceType.Netconf).setNewValue(String.valueOf(operationValue)).build();
123                 databaseService.writeEventLog(eventLogEntity1);
124             }
125         }
126
127         if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
128             if (mapper == null) {
129                 this.mapper = new ORanDOMNotifToVESEventAssembly(netconfDomAccessor, vesCollectorService);
130             }
131             VESCommonEventHeaderPOJO header =
132                     mapper.createVESCommonEventHeader(domNotificationXPath.getTime(domNotification),
133                             ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName(),
134                             sequenceNo);
135             VESNotificationFieldsPOJO body =
136                     mapper.createVESNotificationFields(domNotificationXPath.convertDomNotifToXPath(domNotification),
137                             ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE.getLocalName());
138             log.debug("domNotification in XPath format = {}",
139                     domNotificationXPath.convertDomNotifToXPath(domNotification));
140             try {
141                 vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
142             } catch (JsonProcessingException e) {
143                 log.warn("Exception while generating JSON object ", e);
144
145             }
146         }
147
148     }
149 }