Fix transaction manager closing.
[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 listener)
32         super(operationProcessor, dataBroker, InstanceIdentifier.builder(Nodes.class).child(Node.class)
33                 .augmentation(FlowCapableNode.class).build());
34     }
35
36     @Override
37     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
38         for (DataTreeModification modification : modifications) {
39             switch (modification.getRootNode().getModificationType()) {
40                 case WRITE:
41                     processAddedNode(modification);
42                     break;
43                 case SUBTREE_MODIFIED:
44                     // NOOP
45                     break;
46                 case DELETE:
47                     processRemovedNode(modification);
48                     break;
49                 default:
50                     throw new IllegalArgumentException("Unhandled modification type: {}" +
51                             modification.getRootNode().getModificationType());
52             }
53         }
54     }
55
56     private void processRemovedNode(final DataTreeModification<FlowCapableNode> modification) {
57         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
58         final NodeId nodeId = provideTopologyNodeId(iiToNodeInInventory);
59         final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node> iiToTopologyRemovedNode = provideIIToTopologyNode(nodeId);
60         if (iiToTopologyRemovedNode != null) {
61             operationProcessor.enqueueOperation(manager -> {
62                 manager.addDeleteOperationTotTxChain(LogicalDatastoreType.OPERATIONAL, iiToTopologyRemovedNode);
63                 TopologyManagerUtil.removeAffectedLinks(nodeId, manager, II_TO_TOPOLOGY);
64             });
65         } else {
66             LOG.debug("Instance identifier to inventory wasn't translated to topology while deleting node.");
67         }
68     }
69
70     private void processAddedNode(final DataTreeModification<FlowCapableNode> modification) {
71         final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory = modification.getRootPath().getRootIdentifier();
72         final NodeId nodeIdInTopology = provideTopologyNodeId(iiToNodeInInventory);
73         if (nodeIdInTopology != null) {
74             final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node> iiToTopologyNode = provideIIToTopologyNode(nodeIdInTopology);
75             sendToTransactionChain(prepareTopologyNode(nodeIdInTopology, iiToNodeInInventory), iiToTopologyNode);
76         } else {
77             LOG.debug("Inventory node key is null. Data can't be written to topology");
78         }
79     }
80
81     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node
82     prepareTopologyNode(final NodeId nodeIdInTopology, final InstanceIdentifier<FlowCapableNode> iiToNodeInInventory) {
83         final InventoryNode inventoryNode = new InventoryNodeBuilder()
84             .setInventoryNodeRef(new NodeRef(iiToNodeInInventory.firstIdentifierOf(Node.class)))
85             .build();
86
87         final NodeBuilder topologyNodeBuilder = new NodeBuilder();
88         topologyNodeBuilder.setNodeId(nodeIdInTopology);
89         topologyNodeBuilder.addAugmentation(InventoryNode.class, inventoryNode);
90
91         return topologyNodeBuilder.build();
92     }
93
94 }