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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.DELETE;
17 import static org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType.WRITE;
18 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.assertDeletedIDs;
19 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newDestNode;
20 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newInvNodeKey;
21 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newLink;
22 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newSourceNode;
23 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setReadFutureAsync;
24 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setupStubbedDeletes;
25 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setupStubbedSubmit;
26 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.verifyMockTx;
27 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.waitForDeletes;
28 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.waitForSubmit;
30 import com.google.common.base.Optional;
31 import com.google.common.util.concurrent.Futures;
32 import com.google.common.util.concurrent.SettableFuture;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.concurrent.CountDownLatch;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
40 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.topology.inventory.rev131030.InventoryNode;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 public class NodeChangeListenerImplTest extends DataChangeListenerBase {
56 @SuppressWarnings({ "rawtypes" })
58 public void testOnNodeRemoved() {
60 NodeKey topoNodeKey = new NodeKey(new NodeId("node1"));
61 InstanceIdentifier<Node> topoNodeII = topologyIID.child(Node.class, topoNodeKey);
62 Node topoNode = new NodeBuilder().setKey(topoNodeKey).build();
64 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
65 nodeKey = newInvNodeKey(topoNodeKey.getNodeId().getValue());
66 InstanceIdentifier<?> invNodeID = InstanceIdentifier.create(Nodes.class).child(
67 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
70 List<Link> linkList = Arrays.asList(
71 newLink("link1", newSourceNode("node1"), newDestNode("dest")),
72 newLink("link2", newSourceNode("source"), newDestNode("node1")),
73 newLink("link2", newSourceNode("source2"), newDestNode("dest2")));
74 final Topology topology = new TopologyBuilder().setLink(linkList).build();
76 InstanceIdentifier[] expDeletedIIDs = {
77 topologyIID.child(Link.class, linkList.get(0).getKey()),
78 topologyIID.child(Link.class, linkList.get(1).getKey()),
79 topologyIID.child(Node.class, new NodeKey(new NodeId("node1")))
82 SettableFuture<Optional<Topology>> readFuture = SettableFuture.create();
83 readFuture.set(Optional.of(topology));
84 ReadWriteTransaction mockTx1 = mock(ReadWriteTransaction.class);
85 doReturn(Futures.makeChecked(readFuture, ReadFailedException.MAPPER)).when(mockTx1)
86 .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
88 SettableFuture<Optional<Node>> readFutureNode = SettableFuture.create();
89 readFutureNode.set(Optional.of(topoNode));
90 doReturn(Futures.makeChecked(readFutureNode, ReadFailedException.MAPPER)).when(mockTx1)
91 .read(LogicalDatastoreType.OPERATIONAL, topoNodeII);
93 CountDownLatch submitLatch1 = setupStubbedSubmit(mockTx1);
95 int expDeleteCalls = expDeletedIIDs.length;
96 CountDownLatch deleteLatch = new CountDownLatch(expDeleteCalls);
97 ArgumentCaptor<InstanceIdentifier> deletedLinkIDs =
98 ArgumentCaptor.forClass(InstanceIdentifier.class);
99 setupStubbedDeletes(mockTx1, deletedLinkIDs, deleteLatch);
101 doReturn(mockTx1).when(mockTxChain).newReadWriteTransaction();
103 DataTreeModification dataTreeModification = setupDataTreeChange(DELETE, invNodeID);
104 nodeChangeListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
106 waitForSubmit(submitLatch1);
108 setReadFutureAsync(topology, readFuture);
110 waitForDeletes(expDeleteCalls, deleteLatch);
112 assertDeletedIDs(expDeletedIIDs, deletedLinkIDs);
114 verifyMockTx(mockTx1);
117 @SuppressWarnings({ "rawtypes" })
119 public void testOnNodeRemovedWithNoTopology() {
121 NodeKey topoNodeKey = new NodeKey(new NodeId("node1"));
122 InstanceIdentifier<Node> topoNodeII = topologyIID.child(Node.class, topoNodeKey);
123 Node topoNode = new NodeBuilder().setKey(topoNodeKey).build();
125 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
126 nodeKey = newInvNodeKey(topoNodeKey.getNodeId().getValue());
127 InstanceIdentifier<?> invNodeID = InstanceIdentifier.create(Nodes.class).child(
128 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
131 InstanceIdentifier[] expDeletedIIDs = {
132 topologyIID.child(Node.class, new NodeKey(new NodeId("node1")))
135 ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
136 doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockTx)
137 .read(LogicalDatastoreType.OPERATIONAL, topologyIID);
138 CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
140 SettableFuture<Optional<Node>> readFutureNode = SettableFuture.create();
141 readFutureNode.set(Optional.of(topoNode));
142 doReturn(Futures.makeChecked(readFutureNode, ReadFailedException.MAPPER)).when(mockTx)
143 .read(LogicalDatastoreType.OPERATIONAL, topoNodeII);
145 CountDownLatch deleteLatch = new CountDownLatch(1);
146 ArgumentCaptor<InstanceIdentifier> deletedLinkIDs =
147 ArgumentCaptor.forClass(InstanceIdentifier.class);
148 setupStubbedDeletes(mockTx, deletedLinkIDs, deleteLatch);
150 doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
152 DataTreeModification dataTreeModification = setupDataTreeChange(DELETE, invNodeID);
153 nodeChangeListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
155 waitForSubmit(submitLatch);
157 waitForDeletes(1, deleteLatch);
159 assertDeletedIDs(expDeletedIIDs, deletedLinkIDs);
163 public void testOnNodeAdded() {
165 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
166 nodeKey = newInvNodeKey("node1");
167 InstanceIdentifier<?> invNodeID = InstanceIdentifier.create(Nodes.class).child(
168 org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class,
171 ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
172 CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
173 doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
175 DataTreeModification dataTreeModification = setupDataTreeChange(WRITE, invNodeID);
176 nodeChangeListener.onDataTreeChanged(Collections.singleton(dataTreeModification));
178 waitForSubmit(submitLatch);
180 ArgumentCaptor<Node> mergedNode = ArgumentCaptor.forClass(Node.class);
181 NodeId expNodeId = new NodeId("node1");
182 verify(mockTx).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(topologyIID.child(Node.class,
183 new NodeKey(expNodeId))), mergedNode.capture(), eq(true));
184 assertEquals("getNodeId", expNodeId, mergedNode.getValue().getNodeId());
185 InventoryNode augmentation = mergedNode.getValue().getAugmentation(InventoryNode.class);
186 assertNotNull("Missing augmentation", augmentation);
187 assertEquals("getInventoryNodeRef", new NodeRef(invNodeID), augmentation.getInventoryNodeRef());