2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
\r
11 import java.math.BigInteger;
\r
12 import java.util.Collections;
\r
13 import java.util.HashMap;
\r
14 import java.util.List;
\r
15 import java.util.Objects;
\r
17 import org.junit.Before;
\r
18 import org.junit.Test;
\r
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
\r
20 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
\r
21 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
\r
22 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
\r
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
\r
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;
\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
\r
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
\r
41 import org.slf4j.Logger;
\r
42 import org.slf4j.LoggerFactory;
\r
44 import com.google.common.collect.ImmutableList;
\r
46 import static org.junit.Assert.*;
\r
48 public class SourceMapperTest extends FlowTableTest {
\r
50 protected static final Logger LOG = LoggerFactory.getLogger(SourceMapperTest.class);
\r
54 public void setup() throws Exception {
\r
56 table = new SourceMapper(ctx);
\r
61 public void testNoPolicy() throws Exception {
\r
62 endpointManager.addEndpoint(localEP().build());
\r
63 FlowMap fm = dosync(null);
\r
64 assertEquals(1, fm.getTableForNode(nodeId, (short) 1).getFlow().size());
\r
68 public void testMap() throws Exception {
\r
69 switchManager.addSwitch(
\r
72 Collections.<NodeConnectorId>emptySet(),
\r
73 new OfOverlayNodeConfigBuilder().setTunnel(
\r
74 ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
\r
75 .setTunnelType(TunnelTypeVxlan.class)
\r
76 .setNodeConnectorId(tunnelId)
\r
77 .build())).build());
\r
78 Endpoint ep = localEP().build();
\r
79 endpointManager.addEndpoint(ep);
\r
80 policyResolver.addTenant(baseTenant().build());
\r
82 FlowMap fm = dosync(null);
\r
83 assertEquals(4, fm.getTableForNode(nodeId, (short) 1).getFlow().size());
\r
86 HashMap<String, Flow> flowMap = new HashMap<>();
\r
87 for (Flow f : fm.getTableForNode(nodeId, (short) 1).getFlow()) {
\r
88 flowMap.put(f.getId().getValue(), f);
\r
89 if (f.getMatch() == null) {
\r
90 assertEquals(FlowUtils.dropInstructions(), f.getInstructions());
\r
92 } else if ((f.getMatch() !=null && f.getMatch().getEthernetMatch() != null)
\r
93 && (Objects.equals(ep.getMacAddress(), f.getMatch()
\r
95 .getEthernetSource()
\r
97 PolicyInfo pi = policyResolver.getCurrentPolicy();
\r
98 List<ConditionName> cset = endpointManager.getCondsForEndpoint(ep);
\r
99 ConditionGroup cg = pi.getEgCondGroup(new EgKey(tid, eg), cset);
\r
101 Instruction ins = f.getInstructions().getInstruction().get(0);
\r
102 assertTrue(ins.getInstruction() instanceof ApplyActionsCase);
\r
103 List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();
\r
104 int v = OrdinalFactory.getContextOrdinal(ep);
\r
105 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(v)), actions.get(0)
\r
107 v = OrdinalFactory.getCondGroupOrdinal(cg);
\r
108 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(v)), actions.get(1)
\r
110 v = OrdinalFactory.getContextOrdinal(tid, bd);
\r
111 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(v)), actions.get(2)
\r
113 v = OrdinalFactory.getContextOrdinal(tid, fd);
\r
114 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(v)), actions.get(3)
\r
116 v = OrdinalFactory.getContextOrdinal(tid, l3c);
\r
117 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(v)), actions.get(4)
\r
122 assertEquals(2, count);
\r
123 int numberOfFlows = fm.getTableForNode(nodeId, (short) 1).getFlow().size();
\r
124 fm = dosync(flowMap);
\r
125 assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 1).getFlow().size());
\r