Merge "Topology manager small sonar issues (+ optimized imports)"
[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.AsyncDataChangeEvent;
42 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemovedBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56
57 public class FlowCapableTopologyExporterTest {
58
59     @Mock
60     private DataBroker mockDataBroker;
61
62     @Mock
63     private BindingTransactionChain mockTxChain;
64
65     private OperationProcessor processor;
66
67     private FlowCapableTopologyExporter exporter;
68
69     private TerminationPointChangeListenerImpl terminationPointListener;
70     private NodeChangeListenerImpl nodeChangeListener;
71
72     private InstanceIdentifier<Topology> topologyIID;
73
74     private final ExecutorService executor = Executors.newFixedThreadPool(1);
75
76     @Mock
77     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> mockedDataChangeListener;
78
79     @Before
80     public void setUp() {
81         MockitoAnnotations.initMocks(this);
82
83         doReturn(mockTxChain).when(mockDataBroker)
84                 .createTransactionChain(any(TransactionChainListener.class));
85
86         processor = new OperationProcessor(mockDataBroker);
87
88         topologyIID = InstanceIdentifier.create(NetworkTopology.class)
89                 .child(Topology.class, new TopologyKey(new TopologyId("flow:1")));
90         exporter = new FlowCapableTopologyExporter(processor, topologyIID);
91         terminationPointListener = new TerminationPointChangeListenerImpl(mockDataBroker, processor);
92         nodeChangeListener = new NodeChangeListenerImpl(mockDataBroker, processor);
93
94         executor.execute(processor);
95     }
96
97     @After
98     public void tearDown() {
99         executor.shutdownNow();
100     }
101
102
103     @Test
104     public void testOnLinkDiscovered() {
105
106         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
107                 sourceNodeKey = newInvNodeKey("sourceNode");
108         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
109                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
110         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
111
112         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
113                 destNodeKey = newInvNodeKey("destNode");
114         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
115                 destNodeConnKey = newInvNodeConnKey("destTP");
116         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
117
118         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
119         CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
120         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
121
122         exporter.onLinkDiscovered(new LinkDiscoveredBuilder().setSource(
123                 new NodeConnectorRef(sourceConnID)).setDestination(
124                         new NodeConnectorRef(destConnID)).build());
125
126         waitForSubmit(submitLatch);
127
128         ArgumentCaptor<Link> mergedNode = ArgumentCaptor.forClass(Link.class);
129         verify(mockTx).merge(eq(LogicalDatastoreType.OPERATIONAL), eq(topologyIID.child(
130                         Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId())))),
131                 mergedNode.capture(), eq(true));
132         assertEquals("Source node ID", "sourceNode",
133                 mergedNode.getValue().getSource().getSourceNode().getValue());
134         assertEquals("Dest TP ID", "sourceTP",
135                 mergedNode.getValue().getSource().getSourceTp().getValue());
136         assertEquals("Dest node ID", "destNode",
137                 mergedNode.getValue().getDestination().getDestNode().getValue());
138         assertEquals("Dest TP ID", "destTP",
139                 mergedNode.getValue().getDestination().getDestTp().getValue());
140     }
141
142     @Test
143     public void testOnLinkRemoved() {
144
145         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
146                 sourceNodeKey = newInvNodeKey("sourceNode");
147         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
148                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
149         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
150
151         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
152                 destNodeKey = newInvNodeKey("destNode");
153         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
154                 destNodeConnKey = newInvNodeConnKey("destTP");
155         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
156
157         Link link = newLink(sourceNodeConnKey.getId().getValue(), newSourceTp(sourceNodeConnKey.getId().getValue()),
158                 newDestTp(destNodeConnKey.getId().getValue()));
159
160         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
161         CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
162         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
163         doReturn(Futures.immediateCheckedFuture(Optional.of(link))).when(mockTx).read(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
164                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
165
166         exporter.onLinkRemoved(new LinkRemovedBuilder().setSource(
167                 new NodeConnectorRef(sourceConnID)).setDestination(
168                 new NodeConnectorRef(destConnID)).build());
169
170         waitForSubmit(submitLatch);
171
172         verify(mockTx).delete(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
173                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
174     }
175
176     @Test
177     public void testOnLinkRemovedLinkDoesNotExist() {
178
179         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
180                 sourceNodeKey = newInvNodeKey("sourceNode");
181         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
182                 sourceNodeConnKey = newInvNodeConnKey("sourceTP");
183         InstanceIdentifier<?> sourceConnID = newNodeConnID(sourceNodeKey, sourceNodeConnKey);
184
185         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
186                 destNodeKey = newInvNodeKey("destNode");
187         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
188                 destNodeConnKey = newInvNodeConnKey("destTP");
189         InstanceIdentifier<?> destConnID = newNodeConnID(destNodeKey, destNodeConnKey);
190
191         ReadWriteTransaction mockTx = mock(ReadWriteTransaction.class);
192         CountDownLatch submitLatch = setupStubbedSubmit(mockTx);
193         doReturn(mockTx).when(mockTxChain).newReadWriteTransaction();
194         doReturn(Futures.immediateCheckedFuture(Optional.<Link>absent())).when(mockTx).read(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
195                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
196
197         exporter.onLinkRemoved(new LinkRemovedBuilder().setSource(
198                 new NodeConnectorRef(sourceConnID)).setDestination(
199                 new NodeConnectorRef(destConnID)).build());
200
201         waitForSubmit(submitLatch);
202
203         verify(mockTx, never()).delete(LogicalDatastoreType.OPERATIONAL, topologyIID.child(
204                 Link.class, new LinkKey(new LinkId(sourceNodeConnKey.getId()))));
205     }
206
207 }