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