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