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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.listener;
20 import java.util.Collection;
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;
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 {
49 private static final Logger LOG = LoggerFactory.getLogger(NetconfChangeListener.class);
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())))
55 // Name of ODL controller NETCONF instance
56 private static final NodeId CONTROLLER = new NodeId("controller-config");
58 private final NetconfNodeService deviceManagerService;
59 private final DataBroker dataBroker;
60 private ListenerRegistration<NetconfChangeListener> dlcReg;
62 public NetconfChangeListener(NetconfNodeService deviceManagerService, DataBroker dataBroker) {
63 this.deviceManagerService = deviceManagerService;
64 this.dataBroker = dataBroker;
67 public void register() {
68 DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NETCONF_NODE_TOPO_IID);
70 dlcReg = dataBroker.registerDataTreeChangeListener(treeId, this);
80 * Listener function to select the right node from DataObjectModification
83 public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
84 LOG.debug("OnDataChange, TreeChange, changes:{}", changes.size());
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(),
93 switch (modificationType) {
94 case SUBTREE_MODIFIED:
95 // Change of subtree information
96 // update(change); OLD
97 doProcessing(Action.UPDATE, root.getDataAfter());
100 // Create or modify top level node
101 // Treat an overwrite as an update
102 boolean update = root.getDataBefore() != null;
105 doProcessing(Action.UPDATE, root.getDataAfter());
108 doProcessing(Action.CREATE, root.getDataAfter());
114 doProcessing(Action.REMOVE, root.getDataBefore());
121 * ----------------------------------------------------------------
125 * Process event and forward to clients if Node is a NetconfNode
127 * @param node Basis node
129 private void doProcessing(Action action, Node node) {
131 NodeId nodeId = null;
132 NetconfNode nnode = null;
136 nodeId = node.key().getNodeId(); //Never null
137 nnode = node.augmentation(NetconfNode.class);
140 if (node == null || nnode == null) {
141 LOG.warn("Unexpected node {}, netconf node {} id {}", node, nnode, nodeId);
143 // Do not forward any controller related events to devicemanager
144 if (nodeId.equals(CONTROLLER)) {
145 LOG.debug("Stop processing for [{}]", nodeId);
147 // Action forwarded to devicehandler
148 deviceManagerService.netconfNodeChangeHandler(action, nodeId, nnode);
151 } catch (NullPointerException e) {
152 LOG.warn("Unexpected null .. stop processing.", e);