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