2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.topology.manager;
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;
31 public class NodeChangeListenerImpl extends DataTreeChangeListenerImpl<FlowCapableNode> {
32 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeListenerImpl.class);
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
38 super(operationProcessor, dataBroker,
39 InstanceIdentifier.builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build());
43 public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
44 for (DataTreeModification modification : modifications) {
45 switch (modification.getRootNode().getModificationType()) {
47 processAddedNode(modification);
49 case SUBTREE_MODIFIED:
53 processRemovedNode(modification);
56 throw new IllegalArgumentException(
57 "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
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);
80 LOG.debug("Instance identifier to inventory wasn't translated to topology while deleting node.");
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);
93 LOG.debug("Inventory node key is null. Data can't be written to topology");
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();
103 final NodeBuilder topologyNodeBuilder = new NodeBuilder();
104 topologyNodeBuilder.setNodeId(nodeIdInTopology);
105 topologyNodeBuilder.addAugmentation(InventoryNode.class, inventoryNode);
107 return topologyNodeBuilder.build();