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