2 * Copyright © 2015, 2017 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.southbound.transactions.md;
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;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.ImmutableSet;
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;
70 @RunWith(MockitoJUnitRunner.class)
71 public class OpenVSwitchUpdateCommandTest {
73 @Mock private OpenVSwitchUpdateCommand openVSwitchUpdateCommand;
74 @Mock private TableUpdates updates;
75 @Mock private DatabaseSchema dbSchema;
79 openVSwitchUpdateCommand = mock(OpenVSwitchUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
82 @SuppressWarnings("unchecked")
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));
96 //mock getUpdates() and getDbSchema()
97 when(openVSwitchUpdateCommand.getUpdates()).thenReturn(updates);
98 when(openVSwitchUpdateCommand.getDbSchema()).thenReturn(dbSchema);
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));
115 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
116 PowerMockito.whenNew(OvsdbNodeAugmentationBuilder.class).withNoArguments().thenReturn(ovsdbNodeBuilder);
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);
136 NodeBuilder nodeBuilder = mock(NodeBuilder.class);
137 PowerMockito.whenNew(NodeBuilder.class).withNoArguments().thenReturn(nodeBuilder);
139 //suppress getNodeId()
140 MemberModifier.suppress(
141 MemberMatcher.method(OpenVSwitchUpdateCommand.class, "getNodeId", InstanceIdentifierCodec.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),
151 openVSwitchUpdateCommand.execute(transaction);
152 verify(openVSwitchUpdateCommand, times(2)).getUpdates();
153 verify(openVSwitchUpdateCommand, times(2)).getDbSchema();
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();
164 @SuppressWarnings("unchecked")
165 public void testSetOtherConfig() throws Exception {
166 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
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"));
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),
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));
190 public void testRemoveOldConfigs() throws Exception {
191 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
192 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
194 //suppress getNodeId()
195 doReturn(null).when(openVSwitchUpdateCommand).getNodeId(any());
196 OpenVSwitch ovs = mock(OpenVSwitch.class);
197 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeOldConfigs",
198 transaction, ImmutableMap.of("OpenvswitchOtherConfigsKey", "OpenvswitchOtherConfigsValue"), ovs);
199 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
203 public void testSetNewOtherConfigs() throws Exception {
204 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
205 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewOtherConfigs", ovsdbNodeBuilder,
206 ImmutableMap.of("otherConfigKey", "otherConfigValue"));
208 final Map<OpenvswitchOtherConfigsKey, OpenvswitchOtherConfigs> otherConfigsList =
209 ovsdbNodeBuilder.getOpenvswitchOtherConfigs();
210 assertEquals(1, otherConfigsList.size());
211 final OpenvswitchOtherConfigs otherConfig = otherConfigsList.values().iterator().next();
212 assertEquals("otherConfigKey", otherConfig.getOtherConfigKey());
213 assertEquals("otherConfigValue", otherConfig.getOtherConfigValue());
217 @SuppressWarnings("unchecked")
218 public void testSetExternalIds() throws Exception {
219 OpenVSwitch oldEntry = mock(OpenVSwitch.class);
220 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
222 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
223 when(openVSwitch.getExternalIdsColumn()).thenReturn(column);
224 when(column.getData()).thenReturn(ImmutableMap.of("foo", "bar"));
225 when(oldEntry.getExternalIdsColumn()).thenReturn(column);
226 doNothing().when(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
227 any(OpenVSwitch.class));
228 doNothing().when(openVSwitchUpdateCommand).setNewExternalIds(
229 any(OvsdbNodeAugmentationBuilder.class), any(Map.class));
231 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
232 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
233 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setExternalIds",
234 transaction, ovsdbNodeBuilder, oldEntry, openVSwitch);
235 verify(openVSwitch, times(2)).getExternalIdsColumn();
236 verify(oldEntry, times(2)).getExternalIdsColumn();
237 verify(openVSwitchUpdateCommand).removeExternalIds(any(ReadWriteTransaction.class), any(Map.class),
238 any(OpenVSwitch.class));
242 public void testRemoveExternalIds() throws Exception {
243 ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
244 doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
246 //suppress getNodeId()
247 OpenVSwitch ovs = mock(OpenVSwitch.class);
248 doReturn(mock(NodeId.class)).when(openVSwitchUpdateCommand).getNodeId(any());
249 Whitebox.invokeMethod(openVSwitchUpdateCommand, "removeExternalIds",
250 transaction, ImmutableMap.of("OpenvswitchExternalIdKey", "OpenvswitchExternalIdValue"), ovs);
251 verify(transaction).delete(any(LogicalDatastoreType.class), any(KeyedInstanceIdentifier.class));
255 public void testSetNewExternalIds() throws Exception {
256 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
257 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setNewExternalIds", ovsdbNodeBuilder,
258 ImmutableMap.of("externalIdsKey", "externalIdsValue"));
260 final Map<OpenvswitchExternalIdsKey, OpenvswitchExternalIds> externalIdsList =
261 ovsdbNodeBuilder.getOpenvswitchExternalIds();
262 assertEquals(1, externalIdsList.size());
263 final OpenvswitchExternalIds externalId = externalIdsList.values().iterator().next();
264 assertEquals("externalIdsKey", externalId.getExternalIdKey());
265 assertEquals("externalIdsValue", externalId.getExternalIdValue());
269 @SuppressWarnings("unchecked")
270 public void testSetInterfaceTypes() throws Exception {
271 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
273 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
274 when(openVSwitch.getIfaceTypesColumn()).thenReturn(column);
275 when(column.getData()).thenReturn(ImmutableSet.of(
279 "dpdkvhostuserclient",
290 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
292 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setInterfaceTypes", ovsdbNodeBuilder, openVSwitch);
293 verify(openVSwitch).getIfaceTypesColumn();
295 Map<InterfaceTypeEntryKey, InterfaceTypeEntry> interfaceTypeEntries = ovsdbNodeBuilder.getInterfaceTypeEntry();
296 assertEquals(14, interfaceTypeEntries.size());
300 @SuppressWarnings("unchecked")
301 public void testSetDataPathTypes() throws Exception {
302 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
303 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
304 when(openVSwitch.getDatapathTypesColumn()).thenReturn(column);
305 when(column.getData()).thenReturn(ImmutableSet.of("netdev", "system"));
307 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
308 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDataPathTypes", ovsdbNodeBuilder, openVSwitch);
310 verify(openVSwitch).getDatapathTypesColumn();
311 Map<DatapathTypeEntryKey, DatapathTypeEntry> entries = ovsdbNodeBuilder.getDatapathTypeEntry();
312 assertEquals(2, entries.size());
313 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeNetdev.class)));
314 assertTrue(entries.containsKey(new DatapathTypeEntryKey(DatapathTypeSystem.class)));
318 @SuppressWarnings("unchecked")
319 public void testSetOvsVersion() throws Exception {
320 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
321 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
322 when(openVSwitch.getOvsVersionColumn()).thenReturn(column);
323 when(column.getData()).thenReturn(ImmutableSet.of("v2.3.0"));
324 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
325 when(ovsdbNodeBuilder.setOvsVersion(anyString())).thenReturn(ovsdbNodeBuilder);
327 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setOvsVersion", ovsdbNodeBuilder, openVSwitch);
328 verify(ovsdbNodeBuilder).setOvsVersion(anyString());
329 verify(openVSwitch).getOvsVersionColumn();
333 @SuppressWarnings("unchecked")
334 public void testSetDbVersion() throws Exception {
335 OpenVSwitch openVSwitch = mock(OpenVSwitch.class);
336 Column<GenericTableSchema, Set<String>> column = mock(Column.class);
337 when(openVSwitch.getDbVersionColumn()).thenReturn(column);
338 when(column.getData()).thenReturn(ImmutableSet.of("7.6.1"));
339 OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = mock(OvsdbNodeAugmentationBuilder.class);
341 Whitebox.invokeMethod(openVSwitchUpdateCommand, "setDbVersion", ovsdbNodeBuilder, openVSwitch);
342 verify(ovsdbNodeBuilder).setDbVersion(anyString());
343 verify(openVSwitch).getDbVersionColumn();