ee36f90de96dd9c10773e87fd6a4ad3af5cb9207
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / transactions / md / OpenVSwitchUpdateCommandTest.java
1 /*
2  * Copyright © 2015, 2017 Inocybe Technologies 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.southbound.transactions.md;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.ovsdb.lib.message.TableUpdates;
37 import org.opendaylight.ovsdb.lib.notation.Column;
38 import org.opendaylight.ovsdb.lib.notation.UUID;
39 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
40 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
41 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
42 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
43 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
44 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
45 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
46 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.DatapathTypeEntry;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntry;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.api.support.membermodification.MemberMatcher;
63 import org.powermock.api.support.membermodification.MemberModifier;
64 import org.powermock.reflect.Whitebox;
65
66 @RunWith(MockitoJUnitRunner.class)
67 public class OpenVSwitchUpdateCommandTest {
68
69     @Mock private OpenVSwitchUpdateCommand openVSwitchUpdateCommand;
70     @Mock private TableUpdates updates;
71     @Mock private DatabaseSchema dbSchema;
72
73     @Before
74     public void setUp() {
75         openVSwitchUpdateCommand = mock(OpenVSwitchUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
76     }
77
78     @SuppressWarnings("unchecked")
79     @Test
80     // TODO This test needs to be re-done
81     @Ignore("Broken mock-based test")
82     public void testExecute() throws Exception {
83         PowerMockito.mockStatic(TyperUtils.class);
84         UUID uuid = mock(UUID.class);
85         OpenVSwitch ovs = mock(OpenVSwitch.class);
86         when(TyperUtils.extractRowsUpdated(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
87                 .thenReturn(ImmutableMap.of(uuid, ovs));
88         OpenVSwitch ovs1 = mock(OpenVSwitch.class);
89         when(TyperUtils.extractRowsOld(eq(OpenVSwitch.class), any(TableUpdates.class), any(DatabaseSchema.class)))
90                 .thenReturn(ImmutableMap.of(uuid, ovs1));
91
92         //mock getUpdates() and getDbSchema()
93         when(openVSwitchUpdateCommand.getUpdates()).thenReturn(updates);
94         when(openVSwitchUpdateCommand.getDbSchema()).thenReturn(dbSchema);
95
96         //Test getInstanceIdentifier(): case 1:
97         // ovs.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) == true
98         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
99         when(ovs.getExternalIdsColumn()).thenReturn(column);
100         when(column.getData()).thenReturn(ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY, "iidString"));
101         PowerMockito.mockStatic(SouthboundUtil.class);
102         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getOvsdbConnectionInstance"));
103         InstanceIdentifier<Node> iid = mock(InstanceIdentifier.class);
104 //        when((InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(
105 //                any(InstanceIdentifierCodec.class), anyString())).thenReturn(iid);
106         OvsdbConnectionInstance ovsdbConnectionInstance = mock(OvsdbConnectionInstance.class);
107         when(ovsdbConnectionInstance.getInstanceIdentifier()).thenReturn(iid);
108         when(openVSwitchUpdateCommand.getOvsdbConnectionInstance()).thenReturn(ovsdbConnectionInstance);
109         doNothing().when(ovsdbConnectionInstance).setInstanceIdentifier(any(InstanceIdentifier.class));
110
111         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
112         PowerMockito.whenNew(OvsdbNodeAugmentationBuilder.class).withNoArguments().thenReturn(ovsdbNodeBuilder);
113
114         //suppress the setter methods of the class
115         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOvsVersion",
116                 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
117         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDbVersion",
118                 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
119         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setDataPathTypes",
120                 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
121         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setInterfaceTypes",
122                 OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class));
123         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setExternalIds",
124                 ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class, OpenVSwitch.class, OpenVSwitch.class));
125         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "setOtherConfig",
126                 InstanceIdentifierCodec.class, ReadWriteTransaction.class, OvsdbNodeAugmentationBuilder.class,
127                 OpenVSwitch.class, OpenVSwitch.class));
128         MemberModifier.suppress(MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getConnectionInfo"));
129         when(openVSwitchUpdateCommand.getConnectionInfo()).thenReturn(mock(ConnectionInfo.class));
130         when(ovsdbNodeBuilder.setConnectionInfo(any(ConnectionInfo.class))).thenReturn(ovsdbNodeBuilder);
131
132         NodeBuilder nodeBuilder = mock(NodeBuilder.class);
133         PowerMockito.whenNew(NodeBuilder.class).withNoArguments().thenReturn(nodeBuilder);
134
135         //suppress getNodeId()
136         MemberModifier.suppress(
137                 MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getNodeId", InstanceIdentifierCodec.class,
138                         OpenVSwitch.class));
139         when(nodeBuilder.setNodeId(any(NodeId.class))).thenReturn(nodeBuilder);
140         when(ovsdbNodeBuilder.build()).thenReturn(mock(OvsdbNodeAugmentation.class));
141         when(nodeBuilder.addAugmentation(eq(OvsdbNodeAugmentation.class), any(OvsdbNodeAugmentation.class)))
142                 .thenReturn(nodeBuilder);
143         when(nodeBuilder.build()).thenReturn(mock(Node.class));
144         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
145         doNothing().when(transaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
146                 any(Node.class));
147
148         openVSwitchUpdateCommand.execute(transaction);
149         verify(openVSwitchUpdateCommand, times(2)).getUpdates();
150         verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
151
152         //Test getInstanceIdentifier(): case 2: ovs.getExternalIdsColumn() is null
153         when(ovs.getExternalIdsColumn()).thenReturn(null);
154         when(ovs.getUuid()).thenReturn(uuid);
155         openVSwitchUpdateCommand.execute(transaction);
156         verify(openVSwitchUpdateCommand, times(4)).getUpdates();
157         verify(openVSwitchUpdateCommand, times(4)).getDbSchema();
158     }
159
160     @Test
161     @SuppressWarnings("unchecked")
162     public void testSetOtherConfig() throws Exception {
163         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
164
165         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
166         when(openVSwitch.getOtherConfigColumn()).thenReturn(column);
167         when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
168
169         OpenVSwitch oldEntry = mock(OpenVSwitch.class);
170         when(oldEntry.getOtherConfigColumn()).thenReturn(column);
171         doNothing().when(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
172             any(OpenVSwitch.class));
173         doNothing().when(openVSwitchUpdateCommand).setNewOtherConfigs(any(OvsdbNodeAugmentationBuilder.class),
174             any(Map.class));
175
176         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
177         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
178         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOtherConfig",
179                 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
180         verify(openVSwitch, times(2)).getOtherConfigColumn();
181         verify(oldEntry, times(2)).getOtherConfigColumn();
182         verify(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
183             any(OpenVSwitch.class));
184     }
185
186     @Test
187     public void testRemoveOldConfigs() throws Exception {
188         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
189         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
190
191         //suppress getNodeId()
192         doReturn(null).when(openVSwitchUpdateCommand).getNodeId(any());
193         OpenVSwitch ovs = mock(OpenVSwitch.class);
194         Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
195                 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
196         verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
197     }
198
199     @Test
200     public void testSetNewOtherConfigs() throws Exception {
201         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
202         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
203             ImmutableMap.of("otherConfigKey", "otherConfigValue"));
204
205         final List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
206         assertEquals(1, otherConfigsList.size());
207         final OpenvswitchOtherConfigs otherConfig = otherConfigsList.get(0);
208         assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
209         assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
210     }
211
212     @Test
213     @SuppressWarnings("unchecked")
214     public void testSetExternalIds() throws Exception {
215         OpenVSwitch oldEntry = mock(OpenVSwitch.class);
216         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
217
218         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
219         when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
220         when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
221         when(oldEntry.getExternalIdsColumn()).thenReturn(column);
222         doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
223                 any(OpenVSwitch.class));
224         doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
225             any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
226
227         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
228         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
229         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
230                 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
231         verify(openVSwitch, times(2)).getExternalIdsColumn();
232         verify(oldEntry, times(2)).getExternalIdsColumn();
233         verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
234                 any(OpenVSwitch.class));
235     }
236
237     @Test
238     public void testRemoveExternalIds() throws Exception {
239         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
240         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
241
242         //suppress getNodeId()
243         OpenVSwitch ovs = mock(OpenVSwitch.class);
244         doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
245         Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
246                 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
247         verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
248     }
249
250     @Test
251     public void testSetNewExternalIds() throws Exception {
252         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
253         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
254             ImmutableMap.of("externalIdsKey", "externalIdsValue"));
255
256         final List<OpenvswitchExternalIds> externalIdsList = ovsdbNodeBuilder.getOpenvswitchExternalIds();
257         assertEquals(1, externalIdsList.size());
258         final OpenvswitchExternalIds externalId = externalIdsList.get(0);
259         assertEquals("externalIdsKey", externalId.getExternalIdKey());
260         assertEquals("externalIdsValue", externalId.getExternalIdValue());
261     }
262
263     @Test
264     @SuppressWarnings("unchecked")
265     public void testSetInterfaceTypes() throws Exception {
266         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
267
268         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
269         when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
270         when(column.getData()).thenReturn(ImmutableSet.of(
271             "dpdk",
272             "dpdkr",
273             "dpdkvhostuser",
274             "dpdkvhostuserclient",
275             "geneve",
276             "gre",
277             "internal",
278             "ipsec_gre",
279             "lisp",
280             "patch",
281             "stt",
282             "system",
283             "tap",
284             "vxlan"));
285         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
286
287         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
288         verify(openVSwitch).getIfaceTypesColumn();
289
290         List<InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
291         assertEquals(14, interfaceTypeEntries.size());
292     }
293
294     @Test
295     @SuppressWarnings("unchecked")
296     public void testSetDataPathTypes() throws Exception {
297         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
298         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
299         when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
300         when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
301
302         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
303         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
304
305         verify(openVSwitch).getDatapathTypesColumn();
306         List<DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
307         assertEquals(2, entries.size());
308         assertEquals(DatapathTypeNetdev.class, entries.get(0).getDatapathType());
309         assertEquals(DatapathTypeSystem.class, entries.get(1).getDatapathType());
310     }
311
312     @Test
313     @SuppressWarnings("unchecked")
314     public void testSetOvsVersion() throws Exception {
315         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
316         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
317         when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
318         when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
319         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
320         when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
321
322         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
323         verify(ovsdbNodeBuilder).setOvsVersion(anyString());
324         verify(openVSwitch).getOvsVersionColumn();
325     }
326
327     @Test
328     @SuppressWarnings("unchecked")
329     public void testSetDbVersion() throws Exception {
330         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
331         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
332         when(openVSwitch.getDbVersionColumn()).thenReturn(column);
333         when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
334         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
335
336         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
337         verify(ovsdbNodeBuilder).setDbVersion(anyString());
338         verify(openVSwitch).getDbVersionColumn();
339     }
340 }