2334bd1818d244d161ca0da9c1c3dc5257feceb6
[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.impl.listener;
19
20 import java.util.Collection;
21
22 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.NetconfNodeService;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.NetconfNodeService.Action;
24 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.DataObjectModification;
27 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
28 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.mdsal.binding.api.DataTreeModification;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 // 07.09.18 Switched to DataTreeChangeListener from ClusteredDataTreeChangeListener -> DM Service is
45 // running at all nodes
46 // This is not correct
47 public class NetconfChangeListener implements ClusteredDataTreeChangeListener<Node>, AutoCloseable {
48
49     private static final Logger LOG = LoggerFactory.getLogger(NetconfChangeListener.class);
50
51     private static final InstanceIdentifier<Node> NETCONF_NODE_TOPO_IID =
52             InstanceIdentifier.create(NetworkTopology.class)
53                     .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
54                     .child(Node.class);
55     // Name of ODL controller NETCONF instance
56     private static final NodeId CONTROLLER = new NodeId("controller-config");
57
58     private final NetconfNodeService deviceManagerService;
59     private final DataBroker dataBroker;
60     private ListenerRegistration<NetconfChangeListener> dlcReg;
61
62     public NetconfChangeListener(NetconfNodeService deviceManagerService, DataBroker dataBroker) {
63         this.deviceManagerService = deviceManagerService;
64         this.dataBroker = dataBroker;
65     }
66
67     public void register() {
68         DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NETCONF_NODE_TOPO_IID);
69
70         dlcReg = dataBroker.registerDataTreeChangeListener(treeId, this);
71     }
72
73     @Override
74     public void close() {
75         if (dlcReg != null) {
76             dlcReg.close();
77         }
78     }
79     /**
80      * Listener function to select the right node from DataObjectModification
81      */
82     @Override
83     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
84         LOG.debug("OnDataChange, TreeChange, changes:{}", changes.size());
85
86         for (final DataTreeModification<Node> change : changes) {
87             final DataObjectModification<Node> root = change.getRootNode();
88             final ModificationType modificationType = root.getModificationType();
89             if (LOG.isTraceEnabled()) {
90                 LOG.trace("Handle this modificationType:{} path:{} root:{}", modificationType, change.getRootPath(),
91                         root);
92             }
93             switch (modificationType) {
94                 case SUBTREE_MODIFIED:
95                     // Change of subtree information
96                     // update(change); OLD
97                     doProcessing(Action.UPDATE, root.getDataAfter());
98                     break;
99                 case WRITE:
100                     // Create or modify top level node
101                     // Treat an overwrite as an update
102                     boolean update = root.getDataBefore() != null;
103                     if (update) {
104                         // update(change);
105                         doProcessing(Action.UPDATE, root.getDataAfter());
106                     } else {
107                         // add(change);
108                         doProcessing(Action.CREATE, root.getDataAfter());
109                     }
110                     break;
111                 case DELETE:
112                     // Node removed
113                     // remove(change);
114                     doProcessing(Action.REMOVE, root.getDataBefore());
115                     break;
116             }
117         }
118     }
119
120     /*
121      * ----------------------------------------------------------------
122      */
123
124     /**
125      * Process event and forward to clients if Node is a NetconfNode
126      * @param action
127      * @param node Basis node
128      */
129     private void doProcessing(Action action, Node node) {
130
131         NodeId nodeId = null;
132         NetconfNode nnode = null;
133
134         try {
135             if (node != null) {
136                 nodeId = node.key().getNodeId(); //Never null
137                 nnode = node.augmentation(NetconfNode.class);
138             }
139
140             if (node == null || nnode == null) {
141                 LOG.warn("Unexpected node {}, netconf node {} id {}", node, nnode, nodeId);
142             } else {
143                 // Do not forward any controller related events to devicemanager
144                 if (nodeId.equals(CONTROLLER)) {
145                     LOG.debug("Stop processing for [{}]", nodeId);
146                 } else {
147                       // Action forwarded to devicehandler
148                        deviceManagerService.netconfNodeChangeHandler(action, nodeId, nnode);
149                 }
150             }
151         } catch (NullPointerException e) {
152             LOG.warn("Unexpected null .. stop processing.", e);
153         }
154     }
155
156 }