2 * Copyright (c) 2016 Red Hat, Inc. 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
8 package org.opendaylight.netvirt.elan.l2gw;
10 import static org.junit.Assert.assertEquals;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
21 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
22 import org.opendaylight.netvirt.elan.l2gw.ha.commands.LogicalSwitchesCmd;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 public class LogicalSwitchesCmdTest extends AbstractConcurrentDataBrokerTest {
34 // Uncomment this to keep running this test indefinitely
35 // This is very useful to detect concurrency issues, respectively prove
36 // that the use of AbstractConcurrentDataBrokerTest instead of AbstractDataBrokerTest
37 // does NOT cause any concurrency issues and make this test flaky...
38 // public static @ClassRule RunUntilFailureClassRule classRepeater = new RunUntilFailureClassRule();
39 // public @Rule RunUntilFailureRule repeater = new RunUntilFailureRule(classRepeater);
41 DataBroker dataBroker;
42 ReadWriteTransaction tx;
43 LogicalSwitchesCmd cmd = new LogicalSwitchesCmd();
45 HwvtepGlobalAugmentationBuilder dstBuilder = new HwvtepGlobalAugmentationBuilder();
47 HwvtepGlobalAugmentation existingData = null;//nodata
48 HwvtepGlobalAugmentation srcData = null;
50 HwvtepGlobalAugmentation updatedData = null;
51 HwvtepGlobalAugmentation originalData = null;
53 InstanceIdentifier<Node> haNodePath = HwvtepHAUtil.convertToInstanceIdentifier("ha");
54 InstanceIdentifier<Node> d1NodePath = HwvtepHAUtil.convertToInstanceIdentifier("d1");
55 InstanceIdentifier<Node> d2NodePath = HwvtepHAUtil.convertToInstanceIdentifier("d2");
57 LogicalSwitches[] logicalSwitches = new LogicalSwitches[4];
58 InstanceIdentifier<LogicalSwitches>[] ids = new InstanceIdentifier[4];
60 String[][] data = new String[][] {
68 public void setupForHANode() {
69 dataBroker = getDataBroker();
70 tx = Mockito.spy(dataBroker.newReadWriteTransaction());
71 for (int i = 0 ; i < 4; i++) {
72 logicalSwitches[i] = buildData(data[i][0], data[i][1]);
73 ids[i] = haNodePath.augmentation(HwvtepGlobalAugmentation.class).child(LogicalSwitches.class,
74 new LogicalSwitchesKey(new HwvtepNodeName(data[i][0])));
79 public void teardown() {
84 public void testD1Connect() throws Exception {
85 srcData = getData(new LogicalSwitches[] {logicalSwitches[0], logicalSwitches[1]});
86 cmd.mergeOperationalData(dstBuilder, existingData, srcData, haNodePath);
87 assertEquals("should copy the logical switches ", 2, dstBuilder.getLogicalSwitches().size());
91 public void testD2Connect() throws Exception {
92 existingData = getData(new LogicalSwitches[] {logicalSwitches[0], logicalSwitches[1]});
93 srcData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1],
94 logicalSwitches[2], logicalSwitches[3]});
95 cmd.mergeOperationalData(dstBuilder, existingData, srcData, haNodePath);
96 assertEquals("should copy the logical switches ", 2, dstBuilder.getLogicalSwitches().size());
101 public void testOneLogicalSwitchAddedUpdate() throws Exception {
102 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
103 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
104 updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1], logicalSwitches[2]});
105 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
106 Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[2], logicalSwitches[2]);
110 public void testTwoLogicalSwitchesAddedUpdate() throws Exception {
111 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
112 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
113 updatedData = getData(new LogicalSwitches[]{logicalSwitches[0],
114 logicalSwitches[1], logicalSwitches[2], logicalSwitches[3]});
115 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
116 Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[2], logicalSwitches[2]);
117 Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[3], logicalSwitches[3]);
121 public void testLogicalSwitchDeletedUpdate() throws Exception {
122 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1], logicalSwitches[2]});
123 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1], logicalSwitches[2]});
124 updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
125 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
126 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[2]);
131 public void testTwoLogicalSwitchesDeletedUpdate() throws Exception {
132 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1],
133 logicalSwitches[2], logicalSwitches[3]});
134 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1],
135 logicalSwitches[2], logicalSwitches[3]});
136 updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
137 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
138 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[2]);
139 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[3]);
143 public void testTwoAddTwoDeletedLogicalSwitchesUpdate() throws Exception {
144 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
145 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
146 updatedData = getData(new LogicalSwitches[]{logicalSwitches[2], logicalSwitches[3]});
147 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
148 Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[2], logicalSwitches[2]);
149 Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[3], logicalSwitches[3]);
150 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[0]);
151 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[1]);
155 public void testAllDeleteUpdate() throws Exception {
156 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
157 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
158 updatedData = getData(new LogicalSwitches[]{});
159 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
160 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[0]);
161 Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[1]);
165 public void testNoUpdate() throws Exception {
166 existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
167 originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
168 updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
169 cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
170 Mockito.verifyNoMoreInteractions(tx);
173 LogicalSwitches buildData(String name, String tunnelKey) {
174 LogicalSwitchesBuilder logicalSwitchesBuilder = new LogicalSwitchesBuilder();
175 logicalSwitchesBuilder.withKey(new LogicalSwitchesKey(new HwvtepNodeName(name)));
176 logicalSwitchesBuilder.setTunnelKey(tunnelKey);
177 logicalSwitchesBuilder.setHwvtepNodeName(new HwvtepNodeName(name));
178 return logicalSwitchesBuilder.build();
181 HwvtepGlobalAugmentation getData(LogicalSwitches[] elements) {
182 HwvtepGlobalAugmentationBuilder newDataBuilder = new HwvtepGlobalAugmentationBuilder();
183 List<LogicalSwitches> ls = new ArrayList<>();
184 for (LogicalSwitches ele : elements) {
187 newDataBuilder.setLogicalSwitches(ls);
188 return newDataBuilder.build();