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