63d8f2787e392c3ad2794f4b58f26826df1ea2b2
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl;
19
20 import com.fasterxml.jackson.core.JsonProcessingException;
21 import java.util.List;
22 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationProxyParser;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
27 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.IetfNetconfNotificationsListener;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfigChange;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfConfirmedCommit;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionEnd;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfSessionStart;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.netconf.config.change.Edit;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Listener for change notifications
43  */
44 public class ORanChangeNotificationListener implements IetfNetconfNotificationsListener {
45
46     private static final Logger log = LoggerFactory.getLogger(ORanChangeNotificationListener.class);
47
48     private final NetconfBindingAccessor netconfAccessor;
49     private final DataProvider databaseService;
50     private final VESCollectorService vesCollectorService;
51     private final NotificationProxyParser notificationProxyParser;
52     private ORanNotifToVESEventAssembly mapper = null;
53
54     private static int sequenceNo = 0;
55
56     public ORanChangeNotificationListener(NetconfBindingAccessor netconfAccessor, DataProvider databaseService,
57             VESCollectorService vesCollectorService, NotificationProxyParser notificationProxyParser) {
58         this.netconfAccessor = netconfAccessor;
59         this.databaseService = databaseService;
60         this.vesCollectorService = vesCollectorService;
61         this.notificationProxyParser = notificationProxyParser;
62     }
63
64     @Override
65     public void onNetconfConfirmedCommit(NetconfConfirmedCommit notification) {
66         log.info("onNetconfConfirmedCommit {}", notification);
67     }
68
69     @Override
70     public void onNetconfSessionStart(NetconfSessionStart notification) {
71         log.info("onNetconfSessionStart {}", notification);
72     }
73
74     @Override
75     public void onNetconfSessionEnd(NetconfSessionEnd notification) {
76         log.info("onNetconfSessionEnd {}", notification);
77     }
78
79     @Override
80     public void onNetconfCapabilityChange(NetconfCapabilityChange notification) {
81         log.info("onNetconfCapabilityChange {}", notification);
82     }
83
84     @Override
85     public void onNetconfConfigChange(NetconfConfigChange notification) {
86         log.info("onNetconfConfigChange (1) {}", notification);
87         sequenceNo++;
88         StringBuffer sb = new StringBuffer();
89         List<Edit> editList = notification.nonnullEdit();
90         for (Edit edit : editList) {
91             if (sb.length() > 0) {
92                 sb.append(", ");
93             }
94             sb.append(edit);
95
96             EventlogBuilder eventlogBuilder = new EventlogBuilder();
97
98             InstanceIdentifier<?> target = edit.getTarget();
99             if (target != null) {
100                 eventlogBuilder.setObjectId(target.toString());
101                 log.info("TARGET: {} {} {}", target.getClass(), target.getTargetType());
102                 for (PathArgument pa : target.getPathArguments()) {
103                     log.info("PathArgument {}", pa);
104                 }
105             }
106             eventlogBuilder.setNodeId(netconfAccessor.getNodeId().getValue());
107             eventlogBuilder.setNewValue(String.valueOf(edit.getOperation()));
108             databaseService.writeEventLog(eventlogBuilder.build());
109         }
110         log.info("onNetconfConfigChange (2) {}", sb);
111
112         if (vesCollectorService.getConfig().isVESCollectorEnabled()) {
113             if (mapper == null) {
114                 this.mapper = new ORanNotifToVESEventAssembly(netconfAccessor, vesCollectorService);
115             }
116             VESCommonEventHeaderPOJO header = mapper.createVESCommonEventHeader(notificationProxyParser.getTime(notification),
117                     NetconfConfigChange.class.getSimpleName(), sequenceNo);
118             VESNotificationFieldsPOJO body =
119                     mapper.createVESNotificationFields(notificationProxyParser.parseNotificationProxy(notification),
120                             NetconfConfigChange.class.getSimpleName());
121             try {
122                 vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
123             } catch (JsonProcessingException e) {
124                 log.warn("Exception while generating JSON object ", e);
125
126             }
127         }
128
129     }
130 }