Remove ElanInstanceManager#getElanInstanceByName method
[netvirt.git] / elanmanager / impl / src / test / java / org / opendaylight / netvirt / elanmanager / tests / ElanServiceTestBase.java
1 /*
2  * Copyright © 2017 Ericsson India Global Services Pvt Ltd. 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.elanmanager.tests;
9
10 import com.google.common.collect.Lists;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.TimeUnit;
18 import javax.inject.Inject;
19 import org.apache.commons.lang3.tuple.ImmutablePair;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.awaitility.Awaitility;
22 import org.awaitility.core.ConditionFactory;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
28 import org.opendaylight.genius.mdsalutil.MDSALUtil;
29 import org.opendaylight.genius.testutils.TestInterfaceManager;
30 import org.opendaylight.genius.testutils.interfacemanager.TunnelInterfaceDetails;
31 import org.opendaylight.genius.testutils.itm.ItmRpcTestImpl;
32 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
33 import org.opendaylight.netvirt.elan.internal.ElanInstanceManager;
34 import org.opendaylight.netvirt.elan.utils.ElanUtils;
35 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
36 import org.opendaylight.netvirt.elanmanager.tests.utils.InterfaceHelper;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
66 import org.opendaylight.yangtools.yang.binding.DataObject;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68
69 public class ElanServiceTestBase {
70
71     protected  @Inject DataBroker dataBroker;
72     protected  @Inject TestInterfaceManager interfaceMgr;
73     protected  @Inject ItmRpcTestImpl itmRpc;
74     protected  @Inject ElanInstanceManager elanInstanceManager;
75     protected  @Inject ElanInstanceCache elanInstanceCache;
76     protected @Inject SingleTransactionDataBroker singleTxdataBroker;
77     public static final String ELAN1 = "34701c04-1118-4c65-9425-78a80d49a211";
78     public static final Long ELAN1_SEGMENT_ID = 100L;
79
80     protected static final BigInteger DPN1_ID = new BigInteger("1");
81     protected static final BigInteger DPN2_ID = new BigInteger("2");
82
83     protected static final String DPN1_ID_STR = "1";
84     protected static final String DPN2_ID_STR = "2";
85
86     protected static final String DPN1_TEPIP = "192.168.56.30";
87     protected static final String DPN2_TEPIP = "192.168.56.40";
88     protected static final String TOR1_TEPIP = "192.168.56.50";
89     public static final String DCGW_TEPIP = "192.168.56.60";
90
91     protected static final String DPN1MAC1 = "10:00:00:00:00:01";
92     protected static final String DPN1MAC2 = "10:00:00:00:00:02";
93
94     protected static final String DPN2MAC1 = "10:00:00:00:00:03";
95     protected static final String DPN2MAC2 = "10:00:00:00:00:04";
96
97     protected static final String TOR1MAC1 = "10:00:00:00:00:05";
98     protected static final String TOR1MAC2 = "10:00:00:00:00:06";
99
100     protected static final String DPN1TODPN2TNLMAC = "91:00:00:00:00:01";
101     protected static final String DPN1TOTOR1TNLMAC = "91:00:00:00:00:02";
102     protected static final String DPN1TODCGWTNLMAC = "91:00:00:00:00:03";
103
104     protected static final String DPN2TODPN1TNLMAC = "92:00:00:00:00:01";
105     protected static final String DPN2TOTOR1TNLMAC = "92:00:00:00:00:02";
106     protected static final String DPN2TODCGWTNLMAC = "92:00:00:00:00:03";
107
108     protected static final String DPN1IP1 = "10.0.0.11";
109     protected static final String DPN1IP2 = "10.0.0.12";
110     protected static final String DPN2IP1 = "10.0.0.13";
111     protected static final String DPN2IP2 = "10.0.0.14";
112
113     protected static final String EVPNRECVMAC1 = "10:00:00:00:00:51";
114     protected static final String EVPNRECVMAC2 = "10:00:00:00:00:52";
115
116     protected static final String EVPNRECVIP1 = "192.168.122.51";
117     protected static final String EVPNRECVIP2 = "192.168.122.52";
118
119     protected static final String TOR1NODEID = "hwvtep://uuid/34701c04-1118-4c65-9425-78a80d49a211";
120     protected static final String DCGWID = DCGW_TEPIP;
121
122     public static final String RD = "100:1";
123     public static final String EVPN1 = "evpn1";
124
125     protected static Map<String, Pair<InterfaceInfo, String>> ELAN_INTERFACES = new HashMap<>();
126     protected static Map<String, TunnelInterfaceDetails> EXTN_INTFS = new HashMap<>();
127
128
129     static {
130         //Adding elan dpn macs
131         /*ELAN1+":"+DPN1MAC1 ->
132         (vlanInterfaceInfo(String interfaceName, BigInteger dpId, int portNo, int lportTag, String mac), vmPrefix)*/
133         ELAN_INTERFACES.put(ELAN1 + ":" + DPN1MAC1 ,
134                 new ImmutablePair(InterfaceHelper.buildVlanInterfaceInfo("23701c04-1118-4c65-9425-78a80d49a211",
135                 DPN1_ID, 1, 10, DPN1MAC1), DPN1IP1));
136
137         ELAN_INTERFACES.put(ELAN1 + ":" + DPN1MAC2 ,
138                 new ImmutablePair(InterfaceHelper.buildVlanInterfaceInfo("23701c04-1218-4c65-9425-78a80d49a211",
139                 DPN1_ID, 2, 11, DPN1MAC2), DPN1IP2));
140
141         ELAN_INTERFACES.put(ELAN1 + ":" + DPN2MAC1 ,
142                 new ImmutablePair(InterfaceHelper.buildVlanInterfaceInfo("23701c04-2118-4c65-9425-78a80d49a211",
143                 DPN2_ID, 3, 12, DPN2MAC1), DPN2IP1));
144
145         ELAN_INTERFACES.put(ELAN1 + ":" + DPN2MAC2 ,
146                 new ImmutablePair(InterfaceHelper.buildVlanInterfaceInfo("23701c04-2218-4c65-9425-78a80d49a211",
147                 DPN2_ID, 4, 13, DPN2MAC2), DPN2IP2));
148
149         //Adding the external tunnel interfaces
150         EXTN_INTFS.put(DPN1_ID_STR + ":" + DPN2_ID_STR, new TunnelInterfaceDetails(DPN1_TEPIP, DPN2_TEPIP, true,
151                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-10", DPN1_ID, 5, 14, DPN1TODPN2TNLMAC)));
152
153         EXTN_INTFS.put(DPN1_ID_STR + ":" + TOR1NODEID, new TunnelInterfaceDetails(DPN1_TEPIP, TOR1_TEPIP, true,
154                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-12", DPN1_ID, 6, 15, DPN1TOTOR1TNLMAC)));
155
156         EXTN_INTFS.put(DPN2_ID_STR + ":" + DPN1_ID_STR, new TunnelInterfaceDetails(DPN2_TEPIP, DPN1_TEPIP, true,
157                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-11", DPN2_ID, 7, 16, DPN2TODPN1TNLMAC)));
158
159         EXTN_INTFS.put(DPN2_ID_STR + ":" + TOR1NODEID, new TunnelInterfaceDetails(DPN2_TEPIP, TOR1_TEPIP, true,
160                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-13", DPN2_ID, 8, 17, DPN2TOTOR1TNLMAC)));
161
162
163         EXTN_INTFS.put(DPN1_ID_STR + ":" + DCGWID, new TunnelInterfaceDetails(DPN1_TEPIP, DCGW_TEPIP, true,
164                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-14", DPN1_ID, 9, 18, DPN1TODCGWTNLMAC)));
165
166         EXTN_INTFS.put(DPN2_ID_STR + ":" + DCGWID, new TunnelInterfaceDetails(DPN2_TEPIP, DCGWID, true,
167                 InterfaceHelper.buildVxlanInterfaceInfo("tun23701c04-15", DPN2_ID, 10, 19, DPN2TODCGWTNLMAC)));
168     }
169
170     protected ConditionFactory getAwaiter() {
171         return Awaitility.await("TestableListener")
172                 .atMost(30, TimeUnit.SECONDS)//TODO constant
173                 .pollInterval(100, TimeUnit.MILLISECONDS);
174     }
175
176     void awaitForData(LogicalDatastoreType dsType, InstanceIdentifier<? extends DataObject> iid) {
177         getAwaiter().until(() -> MDSALUtil.read(dataBroker, dsType, iid).isPresent());
178     }
179
180     void awaitForDataDelete(LogicalDatastoreType dsType, InstanceIdentifier<? extends DataObject> iid) {
181         getAwaiter().until(() -> !MDSALUtil.read(dataBroker, dsType, iid).isPresent());
182     }
183
184     Flow getFlowWithoutCookie(Flow flow) {
185         FlowBuilder flowBuilder = new FlowBuilder(flow);
186         return flowBuilder.setCookie(null).build();
187     }
188
189     Flow getSortedActions(Flow flow) {
190         FlowBuilder flowBuilder = new FlowBuilder(flow);
191         Instructions instructions = flowBuilder.getInstructions();
192         InstructionsBuilder builder = new InstructionsBuilder();
193         InstructionBuilder instructionBuilder = new InstructionBuilder(instructions.getInstruction().get(0));
194         instructionBuilder.setInstruction(sortActions(instructionBuilder.getInstruction()));
195         builder.setInstruction(Lists.newArrayList(instructionBuilder.build()));
196         return flowBuilder.setInstructions(builder.build()).build();
197     }
198
199     org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
200         sortActions(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction input) {
201         if (input instanceof  ApplyActionsCase) {
202             List<Action> action = new ArrayList<>(((ApplyActionsCase)input).getApplyActions().getAction());
203             Collections.sort(action, (o1, o2) -> o1.getOrder().compareTo(o2.getOrder()));
204
205             ApplyActions actions = new ApplyActionsBuilder().setAction(action).build();
206             return new ApplyActionsCaseBuilder().setApplyActions(actions).build();
207         }
208         return null;
209     }
210
211     protected void setupItm() throws TransactionCommitFailedException {
212         /*Add tap port and tunnel ports in DPN1 and DPN2*/
213         interfaceMgr.addInterfaceInfo(ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC1).getLeft());
214         interfaceMgr.addInterfaceInfo(ELAN_INTERFACES.get(ELAN1 + ":" + DPN1MAC2).getLeft());
215         interfaceMgr.addInterfaceInfo(ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC1).getLeft());
216         interfaceMgr.addInterfaceInfo(ELAN_INTERFACES.get(ELAN1 + ":" + DPN2MAC2).getLeft());
217         interfaceMgr.addTunnelInterface(EXTN_INTFS.get(DPN1_ID_STR + ":" + DPN2_ID_STR));
218         interfaceMgr.addTunnelInterface(EXTN_INTFS.get(DPN2_ID_STR + ":" + DPN1_ID_STR));
219
220         /*Add DPN1 and DPN2 TEPs*/
221         itmRpc.addDpn(DPN1_ID, DPN1_TEPIP);
222         itmRpc.addDpn(DPN2_ID, DPN2_TEPIP);
223
224         /*add external interface*/
225         itmRpc.addInterface(DPN1_ID,
226                 DPN2_TEPIP, EXTN_INTFS.get(DPN1_ID_STR + ":" + DPN2_ID_STR).getInterfaceInfo().getInterfaceName());
227         itmRpc.addInterface(DPN2_ID,
228                 DPN1_TEPIP, EXTN_INTFS.get(DPN2_ID_STR + ":" + DPN1_ID_STR).getInterfaceInfo().getInterfaceName());
229
230         itmRpc.addExternalInterface(DPN1_ID,
231                 DCGWID, EXTN_INTFS.get(DPN1_ID_STR + ":" + DCGWID).getInterfaceInfo().getInterfaceName());
232     }
233
234     protected InstanceIdentifier<Flow> getFlowIid(short tableId, FlowId flowid, BigInteger dpnId) {
235
236         FlowKey flowKey = new FlowKey(new FlowId(flowid));
237         NodeId nodeId =
238                 new NodeId("openflow:" + dpnId);
239         Node nodeDpn =
240                 new NodeBuilder().setId(nodeId).setKey(new NodeKey(nodeId)).build();
241         return InstanceIdentifier.builder(Nodes.class)
242                 .child(Node.class,
243                         nodeDpn.getKey()).augmentation(FlowCapableNode.class)
244                 .child(Table.class, new TableKey(tableId)).child(Flow.class, flowKey).build();
245     }
246
247
248     protected void createElanInstance(String elan1, Long elan1SegmentId) {
249         ElanInstance elanInstance = ExpectedObjects.createElanInstance(elan1, elan1SegmentId);
250         MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
251                 ElanHelper.getElanInstanceConfigurationDataPath(elan1), elanInstance);
252     }
253
254     public void addElanInterface(String elanInstanceName, InterfaceInfo interfaceInfo, String prefix) {
255         ElanInstance existingElanInstance = elanInstanceCache.get(elanInstanceName).orNull();
256         String interfaceName = interfaceInfo.getInterfaceName();
257
258         if (existingElanInstance != null) {
259             ElanInterfaceBuilder elanInterfaceBuilder = new ElanInterfaceBuilder()
260                     .setElanInstanceName(elanInstanceName)
261                     .setName(interfaceName)
262                     .setKey(new ElanInterfaceKey(interfaceName));
263
264             StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
265             List<StaticMacEntries> staticMacEntries = new ArrayList<>();
266             List<PhysAddress> physAddressList = Collections.singletonList(
267                     new PhysAddress(interfaceInfo.getMacAddress()));
268             for (PhysAddress physAddress : physAddressList) {
269                 staticMacEntries.add(staticMacEntriesBuilder.setMacAddress(physAddress)
270                         .setIpPrefix(new IpAddress(new Ipv4Address(prefix))).build());
271             }
272             elanInterfaceBuilder.setStaticMacEntries(staticMacEntries);
273             ElanInterface elanInterface = elanInterfaceBuilder.build();
274
275             MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
276                     ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
277         }
278     }
279
280     public void deleteElanInterface(InterfaceInfo interfaceInfo) throws TransactionCommitFailedException {
281         String interfaceName = interfaceInfo.getInterfaceName();
282         singleTxdataBroker.syncDelete(LogicalDatastoreType.CONFIGURATION,
283                 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
284
285     }
286 }