b9ed578c092811fe805d67fec5f5c9c46d60fd4e
[openflowplugin.git] / applications / topology-manager / src / test / java / org / opendaylight / openflowplugin / applications / topology / manager / FlowCapableTopologyExporterTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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
9 package org.opendaylight.openflowplugin.applications.topology.manager;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.never;
17 import static org.mockito.Mockito.verify;
18 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newDestTp;
19 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newInvNodeConnKey;
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.newNodeConnID;
23 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.newSourceTp;
24 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.setupStubbedSubmit;
25 import static org.opendaylight.openflowplugin.applications.topology.manager.TestUtils.waitForSubmit;
26
27 import com.google.common.base.Optional;
28 import com.google.common.util.concurrent.Futures;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
39 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
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.TransactionChainListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 public class FlowCapableTopologyExporterTest {
56
57     private OperationProcessor processor;
58     private FlowCapableTopologyExporter exporter;
59     private InstanceIdentifier<Topology> topologyIID;
60     private final ExecutorService executor = Executors.newFixedThreadPool(1);
61     @Mock
62     private DataBroker mockDataBroker;
63     @Mock
64     private BindingTransactionChain mockTxChain;
65
66     @Before
67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69
70         doReturn(mockTxChain).when(mockDataBroker)
71                 .createTransactionChain(any(TransactionChainListener.class));
72
73         processor = new OperationProcessor(mockDataBroker);
74
75         topologyIID = InstanceIdentifier.create(NetworkTopology.class)
76                 .child(Topology.class, new TopologyKey(new TopologyId("flow:1")));
77         exporter = new FlowCapableTopologyExporter(processor, topologyIID);
78         executor.execute(processor);
79     }
80
81     @After
82     public void tearDown() {
83         executor.shutdownNow();
84     }
85
86
87     @Test
88     public void testOnLinkDiscovered() {
89
90         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
91                 sourceNodeKey = newInvNodeKey("sourceNode");
92         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
93                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
94         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
95
96         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
97                 destNodeKey = newInvNodeKey("destNode");
98         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
99                 destNodeConnKey = newInvNodeConnKey("destTP");
100         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
101
102         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
103         CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
104         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
105
106         exporter.onLinkDiscovered(new LinkDiscoveredBuilder().setSource(
107                 new NodeConnectorRef(sourceConnID)).setDestination(
108                         new NodeConnectorRef(destConnID)).build());
109
110         waitForSubmit(submitLatch);
111
112         ArgumentCaptor<Link> mergedNode = ArgumentCaptor.forClass(Link.class);
113         verify(mockTx).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(topologyIID.child(
114                         Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId())))),
115                 mergedNode.capture(), eq(true));
116         assertEquals("Source node ID", "sourceNode",
117                 mergedNode.getValue().getSource().getSourceNode().getValue());
118         assertEquals("Dest TP ID", "sourceTP",
119                 mergedNode.getValue().getSource().getSourceTp().getValue());
120         assertEquals("Dest node ID", "destNode",
121                 mergedNode.getValue().getDestination().getDestNode().getValue());
122         assertEquals("Dest TP ID", "destTP",
123                 mergedNode.getValue().getDestination().getDestTp().getValue());
124     }
125
126     @Test
127     public void testOnLinkRemoved() {
128
129         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
130                 sourceNodeKey = newInvNodeKey("sourceNode");
131         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
132                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
133         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
134
135         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
136                 destNodeKey = newInvNodeKey("destNode");
137         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
138                 destNodeConnKey = newInvNodeConnKey("destTP");
139         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
140
141         Link link = newLink(sourceNodeConnKey.getId().getValue(), newSourceTp(sourceNodeConnKey.getId().getValue()),
142                 newDestTp(destNodeConnKey.getId().getValue()));
143
144         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
145         final CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
146         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
147         doReturn(Futures.immediateCheckedFuture(Optional.of(link))).when(mockTx)
148                 .read(LogicalDatastoreType.OPERATIONAL,
149                       topologyIID.child(Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
150
151         exporter.onLinkRemoved(new LinkRemovedBuilder().setSource(
152                 new NodeConnectorRef(sourceConnID)).setDestination(
153                 new NodeConnectorRef(destConnID)).build());
154
155         waitForSubmit(submitLatch);
156
157         verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
158                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
159     }
160
161     @Test
162     public void testOnLinkRemovedLinkDoesNotExist() {
163
164         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
165                 sourceNodeKey = newInvNodeKey("sourceNode");
166         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
167                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
168         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
169
170         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
171                 destNodeKey = newInvNodeKey("destNode");
172         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
173                 destNodeConnKey = newInvNodeConnKey("destTP");
174         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
175
176         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
177         final CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
178         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
179         doReturn(Futures.immediateCheckedFuture(Optional.<Link>absent())).when(mockTx)
180                 .read(LogicalDatastoreType.OPERATIONAL,
181                       topologyIID.child(Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
182
183         exporter.onLinkRemoved(new LinkRemovedBuilder().setSource(
184                 new NodeConnectorRef(sourceConnID)).setDestination(
185                 new NodeConnectorRef(destConnID)).build());
186
187         waitForSubmit(submitLatch);
188
189         verify(mockTx, never()).delete(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
190                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
191     }
192 }