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