Merge remote-tracking branch 'liblldp/master'
[openflowplugin.git] / applications / topology-manager / src / main / java / org / opendaylight / openflowplugin / applications / topology / manager / NodeChangeListenerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.applications.topology.manager;
9
10 import java.util.Collection;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNodeBuilder;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NodeChangeListenerImpl extends DataTreeChangeListenerImpl<FlowCapableNode> {
28     private static final Logger LOG = LoggerFactory.getLogger(NodeChangeListenerImpl.class);
29
30     public NodeChangeListenerImpl(final DataBroker dataBroker, final OperationProcessor operationProcessor) {
31         // TODO: listener on FlowCapableNode. what if node id in Node.class is changed (it won't be caught by this
32         // listener)
33         super(operationProcessor, dataBroker,
34               InstanceIdentifier.builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build());
35     }
36
37     @Override
38     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
39         for (DataTreeModification modification : modifications) {
40             switch (modification.getRootNode().getModificationType()) {
41                 case WRITE:
42                     processAddedNode(modification);
43                     break;
44                 case SUBTREE_MODIFIED:
45                     // NOOP
46                     break;
47                 case DELETE:
48                     processRemovedNode(modification);
49                     break;
50                 default:
51                     throw new IllegalArgumentException(
52                             "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
53             }
54         }
55     }
56
57     private void processRemovedNode(final DataTreeModification<FlowCapableNode> modification) {
58         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
59         final NodeId nodeId = provideTopologyNodeId(iiToNodeInInventory);
60         final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology
61                 .rev131021.network.topology.topology.Node>
62                 iiToTopologyRemovedNode = provideIIToTopologyNode(nodeId);
63         if (iiToTopologyRemovedNode != null) {
64             operationProcessor.enqueueOperation(manager -> {
65                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, iiToTopologyRemovedNode);
66                 TopologyManagerUtil.removeAffectedLinks(nodeId, manager, II_TO_TOPOLOGY);
67             });
68         } else {
69             LOG.debug("Instance identifier to inventory wasn't translated to topology while deleting node.");
70         }
71     }
72
73     private void processAddedNode(final DataTreeModification<FlowCapableNode> modification) {
74         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
75         final NodeId nodeIdInTopology = provideTopologyNodeId(iiToNodeInInventory);
76         if (nodeIdInTopology != null) {
77             final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology
78                     .rev131021.network.topology.topology.Node>
79                     iiToTopologyNode = provideIIToTopologyNode(nodeIdInTopology);
80             sendToTransactionChain(prepareTopologyNode(nodeIdInTopology, iiToNodeInInventory), iiToTopologyNode);
81         } else {
82             LOG.debug("Inventory node key is null. Data can't be written to topology");
83         }
84     }
85
86     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
87             .topology.topology.Node prepareTopologyNode(
88             final NodeId nodeIdInTopology, final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory) {
89         final InventoryNode inventoryNode = new InventoryNodeBuilder()
90                 .setInventoryNodeRef(new NodeRef(iiToNodeInInventory.firstIdentifierOf(Node.class))).build();
91
92         final NodeBuilder topologyNodeBuilder = new NodeBuilder();
93         topologyNodeBuilder.setNodeId(nodeIdInTopology);
94         topologyNodeBuilder.addAugmentation(InventoryNode.class, inventoryNode);
95
96         return topologyNodeBuilder.build();
97     }
98
99 }