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