de2b3f2364f39823f05c2f34ee369015ecb44489
[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.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.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                 WriteTransaction.CREATE_MISSING_PARENTS);
108     }
109
110     @Test
111     public void testTwoLogicalSwitchesAddedUpdate() throws Exception {
112         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
113         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
114         updatedData = getData(new LogicalSwitches[]{logicalSwitches[0],
115                 logicalSwitches[1], logicalSwitches[2], logicalSwitches[3]});
116         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
117         Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[2], logicalSwitches[2],
118                 WriteTransaction.CREATE_MISSING_PARENTS);
119         Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[3], logicalSwitches[3],
120                 WriteTransaction.CREATE_MISSING_PARENTS);
121     }
122
123     @Test
124     public void testLogicalSwitchDeletedUpdate() throws Exception {
125         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1], logicalSwitches[2]});
126         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1], logicalSwitches[2]});
127         updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
128         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
129         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[2]);
130     }
131
132
133     @Test
134     public void testTwoLogicalSwitchesDeletedUpdate() throws Exception {
135         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1],
136                 logicalSwitches[2], logicalSwitches[3]});
137         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1],
138                 logicalSwitches[2], logicalSwitches[3]});
139         updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
140         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
141         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[2]);
142         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[3]);
143     }
144
145     @Test
146     public void testTwoAddTwoDeletedLogicalSwitchesUpdate() throws Exception {
147         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
148         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
149         updatedData = getData(new LogicalSwitches[]{logicalSwitches[2], logicalSwitches[3]});
150         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
151         Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[2], logicalSwitches[2],
152                 WriteTransaction.CREATE_MISSING_PARENTS);
153         Mockito.verify(tx).put(LogicalDatastoreType.OPERATIONAL, ids[3], logicalSwitches[3],
154                 WriteTransaction.CREATE_MISSING_PARENTS);
155         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[0]);
156         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[1]);
157     }
158
159     @Test
160     public void testAllDeleteUpdate() throws Exception {
161         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
162         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
163         updatedData = getData(new LogicalSwitches[]{});
164         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
165         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[0]);
166         Mockito.verify(tx).delete(LogicalDatastoreType.OPERATIONAL, ids[1]);
167     }
168
169     @Test
170     public void testNoUpdate() throws Exception {
171         existingData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
172         originalData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
173         updatedData = getData(new LogicalSwitches[]{logicalSwitches[0], logicalSwitches[1]});
174         cmd.mergeOpUpdate(existingData, updatedData, originalData, haNodePath, tx);
175         Mockito.verifyNoMoreInteractions(tx);
176     } */
177
178     LogicalSwitches buildData(String name, String tunnelKey) {
179         LogicalSwitchesBuilder logicalSwitchesBuilder = new LogicalSwitchesBuilder();
180         logicalSwitchesBuilder.withKey(new LogicalSwitchesKey(new HwvtepNodeName(name)));
181         logicalSwitchesBuilder.setTunnelKey(tunnelKey);
182         logicalSwitchesBuilder.setHwvtepNodeName(new HwvtepNodeName(name));
183         return logicalSwitchesBuilder.build();
184     }
185
186     HwvtepGlobalAugmentation getData(LogicalSwitches[] elements) {
187         HwvtepGlobalAugmentationBuilder newDataBuilder = new HwvtepGlobalAugmentationBuilder();
188         List<LogicalSwitches> ls = new ArrayList<>();
189         for (LogicalSwitches ele : elements) {
190             ls.add(ele);
191         }
192         newDataBuilder.setLogicalSwitches(ls);
193         return newDataBuilder.build();
194     }
195 }