7e900a614bec0ec7a261f94df5bc1b11fb00f7a1
[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.PreDestroy;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.apache.aries.blueprint.annotation.service.Reference;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.DataTreeModification;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNodeBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Singleton
31 public class NodeChangeListenerImpl extends DataTreeChangeListenerImpl<FlowCapableNode> {
32     private static final Logger LOG = LoggerFactory.getLogger(NodeChangeListenerImpl.class);
33
34     @Inject
35     public NodeChangeListenerImpl(@Reference final DataBroker dataBroker, final OperationProcessor operationProcessor) {
36         // TODO: listener on FlowCapableNode. what if node id in Node.class is changed (it won't be caught by this
37         // listener)
38         super(operationProcessor, dataBroker,
39               InstanceIdentifier.builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build());
40     }
41
42     @Override
43     public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
44         for (DataTreeModification modification : modifications) {
45             switch (modification.getRootNode().getModificationType()) {
46                 case WRITE:
47                     processAddedNode(modification);
48                     break;
49                 case SUBTREE_MODIFIED:
50                     // NOOP
51                     break;
52                 case DELETE:
53                     processRemovedNode(modification);
54                     break;
55                 default:
56                     throw new IllegalArgumentException(
57                             "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
58             }
59         }
60     }
61
62     @Override
63     @PreDestroy
64     public void close() {
65         super.close();
66     }
67
68     private void processRemovedNode(final DataTreeModification<FlowCapableNode> modification) {
69         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
70         final NodeId nodeId = provideTopologyNodeId(iiToNodeInInventory);
71         final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology
72                 .rev131021.network.topology.topology.Node>
73                 iiToTopologyRemovedNode = provideIIToTopologyNode(nodeId);
74         if (iiToTopologyRemovedNode != null) {
75             operationProcessor.enqueueOperation(manager -> {
76                 manager.addDeleteOperationToTxChain(LogicalDatastoreType.OPERATIONAL, iiToTopologyRemovedNode);
77                 TopologyManagerUtil.removeAffectedLinks(nodeId, manager, II_TO_TOPOLOGY);
78             });
79         } else {
80             LOG.debug("Instance identifier to inventory wasn't translated to topology while deleting node.");
81         }
82     }
83
84     private void processAddedNode(final DataTreeModification<FlowCapableNode> modification) {
85         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
86         final NodeId nodeIdInTopology = provideTopologyNodeId(iiToNodeInInventory);
87         if (nodeIdInTopology != null) {
88             final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology
89                     .rev131021.network.topology.topology.Node>
90                     iiToTopologyNode = provideIIToTopologyNode(nodeIdInTopology);
91             sendToTransactionChain(prepareTopologyNode(nodeIdInTopology, iiToNodeInInventory), iiToTopologyNode);
92         } else {
93             LOG.debug("Inventory node key is null. Data can't be written to topology");
94         }
95     }
96
97     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
98             .topology.topology.Node prepareTopologyNode(
99             final NodeId nodeIdInTopology, final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory) {
100         final InventoryNode inventoryNode = new InventoryNodeBuilder()
101                 .setInventoryNodeRef(new NodeRef(iiToNodeInInventory.firstIdentifierOf(Node.class))).build();
102
103         final NodeBuilder topologyNodeBuilder = new NodeBuilder();
104         topologyNodeBuilder.setNodeId(nodeIdInTopology);
105         topologyNodeBuilder.addAugmentation(InventoryNode.class, inventoryNode);
106
107         return topologyNodeBuilder.build();
108     }
109
110 }