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.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.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
18 import org.opendaylight.controller.md.sal.common.api.data.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;
32 public class NodeChangeListenerImpl extends DataTreeChangeListenerImpl<FlowCapableNode> {
33 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeListenerImpl.class);
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
39 super(operationProcessor, dataBroker,
40 InstanceIdentifier.builder(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class).build());
44 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
45 for (DataTreeModification modification : modifications) {
46 switch (modification.getRootNode().getModificationType()) {
48 processAddedNode(modification);
50 case SUBTREE_MODIFIED:
54 processRemovedNode(modification);
57 throw new IllegalArgumentException(
58 "Unhandled modification type: {}" + modification.getRootNode().getModificationType());
65 public void close() throws Exception {
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);
81 LOG.debug("Instance identifier to inventory wasn't translated to topology while deleting node.");
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);
94 LOG.debug("Inventory node key is null. Data can't be written to topology");
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();
104 final NodeBuilder topologyNodeBuilder = new NodeBuilder();
105 topologyNodeBuilder.setNodeId(nodeIdInTopology);
106 topologyNodeBuilder.addAugmentation(InventoryNode.class, inventoryNode);
108 return topologyNodeBuilder.build();