Squashed commit of the following:
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / AbstractServiceInstanceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.ovsdb.openstack.netvirt.providers.openflow13;
10
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;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
29
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.openstack.netvirt.api.Constants;
44 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.powermock.core.classloader.annotations.PrepareForTest;
56
57 import com.google.common.base.Optional;
58 import com.google.common.util.concurrent.CheckedFuture;
59
60 /**
61  * Unit test for {@link AbstractServiceInstance}
62  */
63 @Ignore // TODO SB_MIGRATION
64 @RunWith(MockitoJUnitRunner.class)
65 public class AbstractServiceInstanceTest {
66
67     @InjectMocks AbstractServiceInstance abstractServiceInstance = mock(AbstractServiceInstance.class, Mockito.CALLS_REAL_METHODS);
68
69     /* TODO SB_MIGRATION */
70     //@Mock private OvsdbConnectionService connectionService;
71     @Mock private PipelineOrchestrator orchestrator;
72     @Mock private MdsalConsumer mdsalConsumer;
73
74
75     private Service service = Service.L3_FORWARDING;
76
77     private final String ID = "5710881121";
78     private final String NODE_ID = Constants.INTEGRATION_BRIDGE + ":" +  ID;
79     private final String DPID = "154652161";
80
81     /**
82      * Test method {@link AbstractServiceInstance#isBridgeInPipeline(String)}
83      */
84     @Test
85     public void testIsBridgeInPipeline() {
86         Node node = mock(Node.class);
87         when(node.getId()).thenReturn(mock(NodeId.class));
88
89         List<Node> nodes = new ArrayList();
90         nodes.add(node);
91         /* TODO SB_MIGRATION */
92         //when(connectionService.getBridgeNodes()).thenReturn(nodes);
93
94         //ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
95         //bridges.put("key", mock(Row.class));
96         //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
97
98         //Bridge bridge = mock(Bridge.class);
99         //Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
100         //when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
101         //when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
102         Set<String> dpids = new HashSet();
103         dpids.add(DPID);
104         //when(datapathIdColumn.getData()).thenReturn(dpids);
105         //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
106
107         /* TODO SB_MIGRATION */
108         //assertTrue("Error, isBridgeInPipeline() did not return the correct value", abstractServiceInstance.isBridgeInPipeline(NODE_ID));
109     }
110
111     /**
112      * Test method {@link AbstractServiceInstance#getTable()}
113      */
114     @Test
115     public void testGetTable() {
116         abstractServiceInstance.setService(service);
117         assertEquals("Error, getTable() did not return the correct value", 70, abstractServiceInstance.getTable());
118     }
119
120     /**
121      * Test method {@link AbstractServiceInstance#createNodeBuilder(String)}
122      */
123     @Test
124     public void testCreateNodeBuilder() {
125         NodeId id = new NodeId(NODE_ID);
126
127         NodeBuilder nodeBuilder = abstractServiceInstance.createNodeBuilder(NODE_ID);
128         assertNotNull("Error, createNodeBuilder() did not return the correct value", nodeBuilder);
129         assertEquals("Error, createNodeBuilder() did not return the correct ID", id, nodeBuilder.getId());
130         assertEquals("Error, createNodeBuilder() did not return the correct Key", new NodeKey(id), nodeBuilder.getKey());
131     }
132
133     /**
134      * Test method {@link AbstractServiceInstance#getMutablePipelineInstructionBuilder()}
135      */
136     @Test
137     public void testGetMutablePipelineInstructionBuilder() {
138         // service == null
139         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
140         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
141
142         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
143
144         // service defined
145         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
146         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
147     }
148
149     /**
150      * Test method {@link AbstractServiceInstance#writeFlow(FlowBuilder, NodeBuilder)}
151      */
152     @Test
153     public void testWriteFlow() throws Exception {
154         DataBroker dataBrocker = mock(DataBroker.class);
155         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
156         when(dataBrocker.newReadWriteTransaction()).thenReturn(transaction);
157         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
158         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
159         when(transaction.submit()).thenReturn(commitFuture);
160
161         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
162         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
163
164         FlowBuilder flowBuilder = mock(FlowBuilder.class);
165         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
166
167         abstractServiceInstance.writeFlow(flowBuilder, nodeBuilder);
168
169         verify(transaction, times(2)).put(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class), any(DataObject.class), eq(true));
170         verify(commitFuture, times(1)).get();
171     }
172
173     /**
174      * Test method {@link AbstractServiceInstance#removeFlow(FlowBuilder, NodeBuilder)}
175      */
176     @Test
177     public void testRemoveFlow() throws Exception {
178         DataBroker dataBrocker = mock(DataBroker.class);
179         WriteTransaction transaction = mock(WriteTransaction.class);
180         when(dataBrocker.newWriteOnlyTransaction()).thenReturn(transaction);
181         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
182         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
183         when(transaction.submit()).thenReturn(commitFuture);
184
185         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
186         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
187
188         FlowBuilder flowBuilder = mock(FlowBuilder.class);
189         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
190
191         abstractServiceInstance.removeFlow(flowBuilder, nodeBuilder);
192         verify(transaction, times(1)).delete(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
193         verify(commitFuture, times(1)).get();
194     }
195
196     /**
197      * Test method {@link AbstractServiceInstance#getFlow(FlowBuilder, NodeBuilder)}
198      */
199     @Test
200     public void testGetFlow() throws Exception {
201         DataBroker dataBrocker = mock(DataBroker.class);
202         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
203         when(dataBrocker.newReadOnlyTransaction()).thenReturn(transaction);
204         when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
205
206         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
207         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
208
209         FlowBuilder flowBuilder = mock(FlowBuilder.class);
210         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
211
212         CheckedFuture dataRead = mock(CheckedFuture.class);
213         when(transaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(dataRead);
214         Optional<Flow> data = mock(Optional.class);
215         when(dataRead.get()).thenReturn(data);
216
217         abstractServiceInstance.getFlow(flowBuilder, nodeBuilder);
218         verify(transaction, times(1)).read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
219     }
220
221     /**
222      * Test method {@link AbstractServiceInstance#programDefaultPipelineRule(String)}
223      */
224     @Test
225     public void testProgramDefaultPipelineRule() {
226         Node node = mock(Node.class);
227         when(node.getId()).thenReturn(mock(NodeId.class));
228
229         List<Node> nodes = new ArrayList();
230         nodes.add(node);
231         /* TODO SB_MIGRATION */
232         //when(connectionService.getBridgeNodes()).thenReturn(nodes);
233
234         //ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
235         //bridges.put("key", mock(Row.class));
236         //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
237
238         //Bridge bridge = mock(Bridge.class);
239         //Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
240         //when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
241         //when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
242         Set<String> dpids = new HashSet();
243         dpids.add(DPID);
244         //when(datapathIdColumn.getData()).thenReturn(dpids);
245         /* TODO SB_MIGRATION */
246         //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
247
248         abstractServiceInstance.setService(service);
249
250         /* TODO SB_MIGRATION */ // Need topology Node rather than the NODE_ID
251         //abstractServiceInstance.programDefaultPipelineRule(NODE_ID);
252
253         /* TODO SB_MIGRATION */
254         //verify(abstractServiceInstance, times(1)).isBridgeInPipeline(NODE_ID);
255         //verify(abstractServiceInstance, times(1)).writeFlow(any(FlowBuilder.class), any(NodeBuilder.class));
256     }
257 }