Merge "fix oauth code"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / listener / NetconfChangeListener.java
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 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 = InstanceIdentifier
52             .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 String CONTROLLER = "controller-config";
57
58     private final DeviceManagerService deviceManagerService;
59     private final DataBroker dataBroker;
60     private ListenerRegistration<NetconfChangeListener> dlcReg;
61
62     public NetconfChangeListener(DeviceManagerService deviceManagerService, DataBroker dataBroker) {
63         this.deviceManagerService = deviceManagerService;
64         this.dataBroker = dataBroker;
65     }
66
67     public void register() {
68         DataTreeIdentifier<Node> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
69                 NETCONF_NODE_TOPO_IID);
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
81      */
82
83     @Override
84     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
85         if (LOG.isTraceEnabled()) {
86             LOG.trace("OnDataChange, TreeChange {}", changes);
87         } else if (LOG.isDebugEnabled()) {
88             LOG.debug("OnDataChange, TreeChange");
89         }
90
91         for (final DataTreeModification<Node> change : changes) {
92             final DataObjectModification<Node> root = change.getRootNode();
93             final ModificationType modificationType = root.getModificationType();
94             if (LOG.isTraceEnabled()) {
95                 LOG.trace("Handle this modificationType:{} path:{} root:{}", modificationType, change.getRootPath(), root);
96             }
97             switch (modificationType) {
98                 case SUBTREE_MODIFIED:
99                     // Change of subtree information
100                     // update(change); OLD
101                     doProcessing(Action.UPDATE, root.getDataAfter());
102                     break;
103                 case WRITE:
104                     // Create or modify top level node
105                     // Treat an overwrite as an update
106                     boolean update = root.getDataBefore() != null;
107                     if (update) {
108                         //update(change);
109                         doProcessing(Action.UPDATE, root.getDataAfter());
110                     } else {
111                         //add(change);
112                         doProcessing(Action.ADD, root.getDataAfter());
113                     }
114                     break;
115                 case DELETE:
116                     // Node removed
117                     //remove(change);
118                     doProcessing(Action.REMOVE, root.getDataBefore());
119                     break;
120             }
121         }
122     }
123
124     /* ----------------------------------------------------------------
125      * Functions to select the right node from DataObjectModification
126      */
127
128     /**
129      * Process event and forward to clients
130      * @param action
131      * @param node   Basis node
132      */
133     private void doProcessing(Action action, Node node) {
134
135         NodeId nodeId;
136         NetconfNode nnode;
137         try {
138             NodeKey nodeKey = node.key();
139             nodeId = nodeKey.getNodeId();
140             nnode = node.augmentation(NetconfNode.class);
141         } catch (NullPointerException e) {
142             LOG.warn("Unexpected null .. stop processing.", e);
143             return;
144         }
145
146         LOG.debug("doProcessing action {} {}",action, nodeId);
147         String nodeIdString = nodeId.getValue();
148         // Do not forward any controller related events to devicemanager
149         if (nodeIdString.equals(CONTROLLER)) {
150             LOG.debug("Stop processing for [{}]", nodeIdString);
151             return;
152         }
153
154         // Related to action
155         if (action == Action.REMOVE) {
156             deviceManagerService.mountpointNodeRemoved(nodeId); //Stop Monitor
157             deviceManagerService.leaveConnectedState(nodeId, nnode); //Remove Mountpoint handler
158             return;
159         }
160
161         // Related to Mountpoint status
162         ConnectionStatus csts = nnode.getConnectionStatus();
163         LOG.debug("NETCONF Node handled with status: {} {}", csts, nnode.getClusteredConnectionStatus());
164         if (csts != null) {
165             switch (csts) {
166                 case Connected: {
167                     deviceManagerService.startListenerOnNodeForConnectedState(action, nodeId, nnode);
168                     break;
169                 }
170                 case UnableToConnect:
171                 case Connecting: {
172                     deviceManagerService.leaveConnectedState(nodeId, nnode);
173                     break;
174                 }
175             }
176         } else {
177             LOG.debug("NETCONF Node handled with null status");
178         }
179     }
180 }