MRI version bumpup for Aluminium
[netvirt.git] / elanmanager / impl / src / test / java / org / opendaylight / netvirt / elan / l2gw / LogicalSwitchesCmdTest.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. 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 package org.opendaylight.netvirt.elan.l2gw;
9
10 import static org.junit.Assert.assertEquals;
11
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;
31
32 public class LogicalSwitchesCmdTest extends AbstractConcurrentDataBrokerTest {
33
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);
40
41     DataBroker dataBroker;
42     ReadWriteTransaction tx;
43     LogicalSwitchesCmd cmd = new LogicalSwitchesCmd();
44
45     HwvtepGlobalAugmentationBuilder dstBuilder = new HwvtepGlobalAugmentationBuilder();
46
47     HwvtepGlobalAugmentation existingData = null;//nodata
48     HwvtepGlobalAugmentation srcData = null;
49
50     HwvtepGlobalAugmentation updatedData = null;
51     HwvtepGlobalAugmentation originalData = null;
52
53     InstanceIdentifier<Node> haNodePath = HwvtepHAUtil.convertToInstanceIdentifier("ha");
54     InstanceIdentifier<Node> d1NodePath = HwvtepHAUtil.convertToInstanceIdentifier("d1");
55     InstanceIdentifier<Node> d2NodePath = HwvtepHAUtil.convertToInstanceIdentifier("d2");
56
57     LogicalSwitches[] logicalSwitches = new LogicalSwitches[4];
58     InstanceIdentifier<LogicalSwitches>[] ids = new InstanceIdentifier[4];
59
60     String[][] data = new String[][] {
61             {"ls1", "100"},
62             {"ls2", "200"},
63             {"ls3", "300"},
64             {"ls4", "400"}
65     };
66
67     @Before
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])));
75         }
76     }
77
78     @After
79     public void teardown() {
80     }
81
82
83     @Test
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());
88     }
89
90     @Test
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());
97     }
98
99     /*
100     @Test
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]);
107     }
108
109     @Test
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]);
118     }
119
120     @Test
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]);
127     }
128
129
130     @Test
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]);
140     }
141
142     @Test
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]);
152     }
153
154     @Test
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]);
162     }
163
164     @Test
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);
171     } */
172
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();
179     }
180
181     HwvtepGlobalAugmentation getData(LogicalSwitches[] elements) {
182         HwvtepGlobalAugmentationBuilder newDataBuilder = new HwvtepGlobalAugmentationBuilder();
183         List<LogicalSwitches> ls = new ArrayList<>();
184         for (LogicalSwitches ele : elements) {
185             ls.add(ele);
186         }
187         newDataBuilder.setLogicalSwitches(ls);
188         return newDataBuilder.build();
189     }
190 }