Remove unused files
[netvirt.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
73     private Service service = Service.L3_FORWARDING;
74
75     private final String ID = "5710881121";
76     private final String NODE_ID = Constants.INTEGRATION_BRIDGE + ":" +  ID;
77     private final String DPID = "154652161";
78
79     /**
80      * Test method {@link AbstractServiceInstance#isBridgeInPipeline(String)}
81      */
82     @Test
83     public void testIsBridgeInPipeline() {
84         Node node = mock(Node.class);
85         when(node.getId()).thenReturn(mock(NodeId.class));
86
87         List<Node> nodes = new ArrayList();
88         nodes.add(node);
89         /* TODO SB_MIGRATION */
90         //when(connectionService.getBridgeNodes()).thenReturn(nodes);
91
92         //ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
93         //bridges.put("key", mock(Row.class));
94         //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
95
96         //Bridge bridge = mock(Bridge.class);
97         //Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
98         //when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
99         //when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
100         Set<String> dpids = new HashSet();
101         dpids.add(DPID);
102         //when(datapathIdColumn.getData()).thenReturn(dpids);
103         //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
104
105         /* TODO SB_MIGRATION */
106         //assertTrue("Error, isBridgeInPipeline() did not return the correct value", abstractServiceInstance.isBridgeInPipeline(NODE_ID));
107     }
108
109     /**
110      * Test method {@link AbstractServiceInstance#getTable()}
111      */
112     @Test
113     public void testGetTable() {
114         abstractServiceInstance.setService(service);
115         assertEquals("Error, getTable() did not return the correct value", 70, abstractServiceInstance.getTable());
116     }
117
118     /**
119      * Test method {@link AbstractServiceInstance#createNodeBuilder(String)}
120      */
121     @Test
122     public void testCreateNodeBuilder() {
123         NodeId id = new NodeId(NODE_ID);
124
125         NodeBuilder nodeBuilder = abstractServiceInstance.createNodeBuilder(NODE_ID);
126         assertNotNull("Error, createNodeBuilder() did not return the correct value", nodeBuilder);
127         assertEquals("Error, createNodeBuilder() did not return the correct ID", id, nodeBuilder.getId());
128         assertEquals("Error, createNodeBuilder() did not return the correct Key", new NodeKey(id), nodeBuilder.getKey());
129     }
130
131     /**
132      * Test method {@link AbstractServiceInstance#getMutablePipelineInstructionBuilder()}
133      */
134     @Test
135     public void testGetMutablePipelineInstructionBuilder() {
136         // service == null
137         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
138         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
139
140         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
141
142         // service defined
143         assertNotNull("Error, getMutablePipelineInstructionBuilder() did not return the correct value", abstractServiceInstance.getMutablePipelineInstructionBuilder());
144         assertTrue("Error, getMutablePipelineInstructionBuilder() did not return a InstructionBuilder object", abstractServiceInstance.getMutablePipelineInstructionBuilder() instanceof InstructionBuilder);
145     }
146
147     /**
148      * Test method {@link AbstractServiceInstance#writeFlow(FlowBuilder, NodeBuilder)}
149      */
150     @Test
151     public void testWriteFlow() throws Exception {
152         DataBroker dataBrocker = mock(DataBroker.class);
153         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
154         when(dataBrocker.newReadWriteTransaction()).thenReturn(transaction);
155         ///when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
156         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
157         when(transaction.submit()).thenReturn(commitFuture);
158
159         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
160         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
161
162         FlowBuilder flowBuilder = mock(FlowBuilder.class);
163         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
164
165         abstractServiceInstance.writeFlow(flowBuilder, nodeBuilder);
166
167         verify(transaction, times(2)).put(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class), any(DataObject.class), eq(true));
168         verify(commitFuture, times(1)).get();
169     }
170
171     /**
172      * Test method {@link AbstractServiceInstance#removeFlow(FlowBuilder, NodeBuilder)}
173      */
174     @Test
175     public void testRemoveFlow() throws Exception {
176         DataBroker dataBrocker = mock(DataBroker.class);
177         WriteTransaction transaction = mock(WriteTransaction.class);
178         when(dataBrocker.newWriteOnlyTransaction()).thenReturn(transaction);
179         //when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
180         CheckedFuture<Void, TransactionCommitFailedException> commitFuture = mock(CheckedFuture.class);
181         when(transaction.submit()).thenReturn(commitFuture);
182
183         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
184         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
185
186         FlowBuilder flowBuilder = mock(FlowBuilder.class);
187         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
188
189         abstractServiceInstance.removeFlow(flowBuilder, nodeBuilder);
190         verify(transaction, times(1)).delete(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
191         verify(commitFuture, times(1)).get();
192     }
193
194     /**
195      * Test method {@link AbstractServiceInstance#getFlow(FlowBuilder, NodeBuilder)}
196      */
197     @Test
198     public void testGetFlow() throws Exception {
199         DataBroker dataBrocker = mock(DataBroker.class);
200         ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
201         when(dataBrocker.newReadOnlyTransaction()).thenReturn(transaction);
202         //when(mdsalConsumer.getDataBroker()).thenReturn(dataBrocker);
203
204         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
205         when(nodeBuilder.getKey()).thenReturn(mock(NodeKey.class));
206
207         FlowBuilder flowBuilder = mock(FlowBuilder.class);
208         when(flowBuilder.getKey()).thenReturn(mock(FlowKey.class));
209
210         CheckedFuture dataRead = mock(CheckedFuture.class);
211         when(transaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(dataRead);
212         Optional<Flow> data = mock(Optional.class);
213         when(dataRead.get()).thenReturn(data);
214
215         abstractServiceInstance.getFlow(flowBuilder, nodeBuilder);
216         verify(transaction, times(1)).read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class));
217     }
218
219     /**
220      * Test method {@link AbstractServiceInstance#programDefaultPipelineRule(String)}
221      */
222     @Test
223     public void testProgramDefaultPipelineRule() {
224         Node node = mock(Node.class);
225         when(node.getId()).thenReturn(mock(NodeId.class));
226
227         List<Node> nodes = new ArrayList();
228         nodes.add(node);
229         /* TODO SB_MIGRATION */
230         //when(connectionService.getBridgeNodes()).thenReturn(nodes);
231
232         //ConcurrentMap<String, Row> bridges = new ConcurrentHashMap();
233         //bridges.put("key", mock(Row.class));
234         //when(ovsdbConfigService.getRows(any(Node.class), anyString())).thenReturn(bridges);
235
236         //Bridge bridge = mock(Bridge.class);
237         //Column<GenericTableSchema, Set<String>> datapathIdColumn = mock(Column.class);
238         //when(bridge.getDatapathIdColumn()).thenReturn(datapathIdColumn);
239         //when(bridge.getName()).thenReturn(Constants.INTEGRATION_BRIDGE);
240         Set<String> dpids = new HashSet();
241         dpids.add(DPID);
242         //when(datapathIdColumn.getData()).thenReturn(dpids);
243         /* TODO SB_MIGRATION */
244         //when(ovsdbConfigService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
245
246         abstractServiceInstance.setService(service);
247
248         /* TODO SB_MIGRATION */ // Need topology Node rather than the NODE_ID
249         //abstractServiceInstance.programDefaultPipelineRule(NODE_ID);
250
251         /* TODO SB_MIGRATION */
252         //verify(abstractServiceInstance, times(1)).isBridgeInPipeline(NODE_ID);
253         //verify(abstractServiceInstance, times(1)).writeFlow(any(FlowBuilder.class), any(NodeBuilder.class));
254     }
255 }