Update MRI projects for Aluminium
[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(eq(OvsdbNodeAugmentation.class), any(OvsdbNodeAugmentation.class)))
146                 .thenReturn(nodeBuilder);
147         when(nodeBuilder.build()).thenReturn(mock(Node.class));
148         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
149         doNothing().when(transaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
150                 any(Node.class));
151
152         openVSwitchUpdateCommand.execute(transaction);
153         verify(openVSwitchUpdateCommand, times(2)).getUpdates();
154         verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
155
156         //Test getInstanceIdentifier(): case 2: ovs.getExternalIdsColumn() is null
157         when(ovs.getExternalIdsColumn()).thenReturn(null);
158         when(ovs.getUuid()).thenReturn(uuid);
159         openVSwitchUpdateCommand.execute(transaction);
160         verify(openVSwitchUpdateCommand, times(4)).getUpdates();
161         verify(openVSwitchUpdateCommand, times(4)).getDbSchema();
162     }
163
164     @Test
165     @SuppressWarnings("unchecked")
166     public void testSetOtherConfig() throws Exception {
167         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
168
169         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
170         when(openVSwitch.getOtherConfigColumn()).thenReturn(column);
171         when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
172
173         OpenVSwitch oldEntry = mock(OpenVSwitch.class);
174         when(oldEntry.getOtherConfigColumn()).thenReturn(column);
175         doNothing().when(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
176             any(OpenVSwitch.class));
177         doNothing().when(openVSwitchUpdateCommand).setNewOtherConfigs(any(OvsdbNodeAugmentationBuilder.class),
178             any(Map.class));
179
180         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
181         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
182         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOtherConfig",
183                 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
184         verify(openVSwitch, times(2)).getOtherConfigColumn();
185         verify(oldEntry, times(2)).getOtherConfigColumn();
186         verify(openVSwitchUpdateCommand).removeOldConfigs(any(ReadWriteTransaction.class), any(Map.class),
187             any(OpenVSwitch.class));
188     }
189
190     @Test
191     public void testRemoveOldConfigs() throws Exception {
192         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
193         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
194
195         //suppress getNodeId()
196         doReturn(null).when(openVSwitchUpdateCommand).getNodeId(any());
197         OpenVSwitch ovs = mock(OpenVSwitch.class);
198         Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
199                 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
200         verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
201     }
202
203     @Test
204     public void testSetNewOtherConfigs() throws Exception {
205         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
206         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
207             ImmutableMap.of("otherConfigKey", "otherConfigValue"));
208
209         final Map<OpenvswitchOtherConfigsKey, OpenvswitchOtherConfigs> otherConfigsList =
210                 ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
211         assertEquals(1, otherConfigsList.size());
212         final OpenvswitchOtherConfigs otherConfig = otherConfigsList.values().iterator().next();
213         assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
214         assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
215     }
216
217     @Test
218     @SuppressWarnings("unchecked")
219     public void testSetExternalIds() throws Exception {
220         OpenVSwitch oldEntry = mock(OpenVSwitch.class);
221         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
222
223         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
224         when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
225         when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
226         when(oldEntry.getExternalIdsColumn()).thenReturn(column);
227         doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
228                 any(OpenVSwitch.class));
229         doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
230             any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
231
232         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
233         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
234         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
235                 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
236         verify(openVSwitch, times(2)).getExternalIdsColumn();
237         verify(oldEntry, times(2)).getExternalIdsColumn();
238         verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
239                 any(OpenVSwitch.class));
240     }
241
242     @Test
243     public void testRemoveExternalIds() throws Exception {
244         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
245         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
246
247         //suppress getNodeId()
248         OpenVSwitch ovs = mock(OpenVSwitch.class);
249         doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
250         Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
251                 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
252         verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
253     }
254
255     @Test
256     public void testSetNewExternalIds() throws Exception {
257         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
258         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
259             ImmutableMap.of("externalIdsKey", "externalIdsValue"));
260
261         final Map<OpenvswitchExternalIdsKey, OpenvswitchExternalIds> externalIdsList =
262                 ovsdbNodeBuilder.getOpenvswitchExternalIds();
263         assertEquals(1, externalIdsList.size());
264         final OpenvswitchExternalIds externalId = externalIdsList.values().iterator().next();
265         assertEquals("externalIdsKey", externalId.getExternalIdKey());
266         assertEquals("externalIdsValue", externalId.getExternalIdValue());
267     }
268
269     @Test
270     @SuppressWarnings("unchecked")
271     public void testSetInterfaceTypes() throws Exception {
272         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
273
274         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
275         when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
276         when(column.getData()).thenReturn(ImmutableSet.of(
277             "dpdk",
278             "dpdkr",
279             "dpdkvhostuser",
280             "dpdkvhostuserclient",
281             "geneve",
282             "gre",
283             "internal",
284             "ipsec_gre",
285             "lisp",
286             "patch",
287             "stt",
288             "system",
289             "tap",
290             "vxlan"));
291         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
292
293         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
294         verify(openVSwitch).getIfaceTypesColumn();
295
296         Map<InterfaceTypeEntryKey, InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
297         assertEquals(14, interfaceTypeEntries.size());
298     }
299
300     @Test
301     @SuppressWarnings("unchecked")
302     public void testSetDataPathTypes() throws Exception {
303         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
304         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
305         when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
306         when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
307
308         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
309         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
310
311         verify(openVSwitch).getDatapathTypesColumn();
312         Map<DatapathTypeEntryKey, DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
313         assertEquals(2, entries.size());
314         assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeNetdev.class)));
315         assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeSystem.class)));
316     }
317
318     @Test
319     @SuppressWarnings("unchecked")
320     public void testSetOvsVersion() throws Exception {
321         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
322         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
323         when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
324         when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
325         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
326         when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
327
328         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
329         verify(ovsdbNodeBuilder).setOvsVersion(anyString());
330         verify(openVSwitch).getOvsVersionColumn();
331     }
332
333     @Test
334     @SuppressWarnings("unchecked")
335     public void testSetDbVersion() throws Exception {
336         OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
337         Column<GenericTableSchema, Set<String>> column = mock(Column.class);
338         when(openVSwitch.getDbVersionColumn()).thenReturn(column);
339         when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
340         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
341
342         Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
343         verify(ovsdbNodeBuilder).setDbVersion(anyString());
344         verify(openVSwitch).getDbVersionColumn();
345     }
346 }