2 * Copyright (c) 2015 Inocybe 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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Matchers.same;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
39 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
40 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
43 import org.opendaylight.ovsdb.lib.notation.Column;
44 import org.opendaylight.ovsdb.lib.notation.Row;
45 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
47 /* TODO SB_MIGRATION */
48 //import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
49 //import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
50 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
51 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yangtools.yang.binding.DataObject;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import com.google.common.base.Optional;
65 import com.google.common.util.concurrent.CheckedFuture;
68 * Unit test for {@link AbstractServiceInstance}
70 /* TODO SB_MIGRATION */
72 @RunWith(MockitoJUnitRunner.class)
73 public class AbstractServiceInstanceTest {
75 @InjectMocks AbstractServiceInstance abstractServiceInstance = mock(AbstractServiceInstance.class, Mockito.CALLS_REAL_METHODS);
77 /* TODO SB_MIGRATION */
78 //@Mock private OvsdbConfigurationService ovsdbConfigService;
79 //@Mock private OvsdbConnectionService connectionService;
80 @Mock private PipelineOrchestrator orchestrator;
81 @Mock private MdsalConsumer mdsalConsumer;
84 private Service service = Service.L3_FORWARDING;
86 private final String ID = "5710881121";
87 private final String NODE_ID = Constants.INTEGRATION_BRIDGE + ":" + ID;
88 private final String DPID = "154652161";
91 * Test method {@link AbstractServiceInstance#isBridgeInPipeline(String)}
94 public void testIsBridgeInPipeline() {
95 Node node = mock(Node.class);
96 when(node.getId()).thenReturn(mock(NodeId.class));
98 List<Node> nodes = new ArrayList();
100 /* TODO SB_MIGRATION */
101 //when(connectionService.getNodes()).thenReturn(nodes);
103 ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
104 bridges.put("key", mock(Row.class));
105 //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
107 Bridge bridge = mock(Bridge.class);
108 Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
109 when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
110 when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
111 Set<String> dpids = new HashSet();
113 when(datapathIdColumn.getData()).thenReturn(dpids);
114 //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
116 /* TODO SB_MIGRATION */
117 //assertTrue("Error, isBridgeInPipeline() did not return the correct value", abstractServiceInstance.isBridgeInPipeline(NODE_ID));
121 * Test method {@link AbstractServiceInstance#getTable()}
124 public void testGetTable() {
125 abstractServiceInstance.setService(service);
126 assertEquals("Error, getTable() did not return the correct value", 70, abstractServiceInstance.getTable());
130 * Test method {@link AbstractServiceInstance#createNodeBuilder(String)}
133 public void testCreateNodeBuilder() {
134 NodeId id = new NodeId(NODE_ID);
136 NodeBuilder nodeBuilder = abstractServiceInstance.createNodeBuilder(NODE_ID);
137 assertNotNull("Error, createNodeBuilder() did not return the correct value", nodeBuilder);
138 assertEquals("Error, createNodeBuilder() did not return the correct ID", id, nodeBuilder.getId());
139 assertEquals("Error, createNodeBuilder() did not return the correct Key", new NodeKey(id), nodeBuilder.getKey());
143 * Test method {@link AbstractServiceInstance#getMutablePipelineInstructionBuilder()}
146 public void testGetMutablePipelineInstructionBuilder() {
148 assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
149 assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
151 when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
154 assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
155 assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
159 * Test method {@link AbstractServiceInstance#writeFlow(FlowBuilder, NodeBuilder)}
162 public void testWriteFlow() throws Exception {
163 DataBroker dataBrocker = mock(DataBroker.class);
164 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
165 when(dataBrocker.newReadWriteTransaction()).thenReturn(transaction);
166 when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
167 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
168 when(transaction.submit()).thenReturn(commitFuture);
170 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
171 when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
173 FlowBuilder flowBuilder = mock(FlowBuilder.class);
174 when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
176 abstractServiceInstance.writeFlow(flowBuilder, nodeBuilder);
178 verify(transaction, times(2)).put(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class), any(DataObject.class), eq(true));
179 verify(commitFuture, times(1)).get();
183 * Test method {@link AbstractServiceInstance#removeFlow(FlowBuilder, NodeBuilder)}
186 public void testRemoveFlow() throws Exception {
187 DataBroker dataBrocker = mock(DataBroker.class);
188 WriteTransaction transaction = mock(WriteTransaction.class);
189 when(dataBrocker.newWriteOnlyTransaction()).thenReturn(transaction);
190 when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
191 CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
192 when(transaction.submit()).thenReturn(commitFuture);
194 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
195 when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
197 FlowBuilder flowBuilder = mock(FlowBuilder.class);
198 when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
200 abstractServiceInstance.removeFlow(flowBuilder, nodeBuilder);
201 verify(transaction, times(1)).delete(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
202 verify(commitFuture, times(1)).get();
206 * Test method {@link AbstractServiceInstance#getFlow(FlowBuilder, NodeBuilder)}
209 public void testGetFlow() throws Exception {
210 DataBroker dataBrocker = mock(DataBroker.class);
211 ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
212 when(dataBrocker.newReadOnlyTransaction()).thenReturn(transaction);
213 when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
215 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
216 when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
218 FlowBuilder flowBuilder = mock(FlowBuilder.class);
219 when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
221 CheckedFuture dataRead = mock(CheckedFuture.class);
222 when(transaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(dataRead);
223 Optional<Flow> data = mock(Optional.class);
224 when(dataRead.get()).thenReturn(data);
226 abstractServiceInstance.getFlow(flowBuilder, nodeBuilder);
227 verify(transaction, times(1)).read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
231 * Test method {@link AbstractServiceInstance#programDefaultPipelineRule(String)}
234 public void testProgramDefaultPipelineRule() {
235 Node node = mock(Node.class);
236 when(node.getId()).thenReturn(mock(NodeId.class));
238 List<Node> nodes = new ArrayList();
240 /* TODO SB_MIGRATION */
241 //when(connectionService.getNodes()).thenReturn(nodes);
243 ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
244 bridges.put("key", mock(Row.class));
245 //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
247 Bridge bridge = mock(Bridge.class);
248 Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
249 when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
250 when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
251 Set<String> dpids = new HashSet();
253 when(datapathIdColumn.getData()).thenReturn(dpids);
254 /* TODO SB_MIGRATION */
255 //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
257 abstractServiceInstance.setService(service);
259 /* TODO SB_MIGRATION */ // Need topology Node rather than the NODE_ID
260 //abstractServiceInstance.programDefaultPipelineRule(NODE_ID);
262 /* TODO SB_MIGRATION */
263 //verify(abstractServiceInstance, times(1)).isBridgeInPipeline(NODE_ID);
264 //verify(abstractServiceInstance, times(1)).writeFlow(any(FlowBuilder.class), any(NodeBuilder.class));