Merge "Minimize flow-programming."
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / SourceMapperTest.java
1 /*\r
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.\r
3  * \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
7  */\r
8 \r
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;\r
10 \r
11 import java.math.BigInteger;\r
12 import java.util.HashMap;\r
13 import java.util.List;\r
14 import java.util.Objects;\r
15 \r
16 import org.junit.Before;\r
17 import org.junit.Test;\r
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;\r
19 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;\r
20 import org.opendaylight.groupbasedpolicy.resolver.EgKey;\r
21 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;\r
34 import org.slf4j.Logger;\r
35 import org.slf4j.LoggerFactory;\r
36 \r
37 import com.google.common.collect.ImmutableList;\r
38 \r
39 import static org.junit.Assert.*;\r
40 \r
41 public class SourceMapperTest extends FlowTableTest {\r
42 \r
43     protected static final Logger LOG = LoggerFactory.getLogger(SourceMapperTest.class);\r
44 \r
45     @Before\r
46     public void setup() throws Exception {\r
47         initCtx();\r
48         table = new SourceMapper(ctx);\r
49         super.setup();\r
50     }\r
51 \r
52     @Test\r
53     public void testNoPolicy() throws Exception {\r
54         endpointManager.addEndpoint(localEP().build());\r
55         FlowMap fm = dosync(null);\r
56         assertEquals(1, fm.getTableForNode(nodeId, (short) 1).getFlow().size());\r
57     }\r
58 \r
59     @Test\r
60     public void testMap() throws Exception {\r
61         Endpoint ep = localEP().build();\r
62         endpointManager.addEndpoint(ep);\r
63         policyResolver.addTenant(baseTenant().build());\r
64 \r
65         FlowMap fm = dosync(null);\r
66         assertEquals(2, fm.getTableForNode(nodeId, (short) 1).getFlow().size());\r
67 \r
68         int count = 0;\r
69         HashMap<String, Flow> flowMap = new HashMap<>();\r
70         for (Flow f : fm.getTableForNode(nodeId, (short) 1).getFlow()) {\r
71             flowMap.put(f.getId().getValue(), f);\r
72             if (f.getMatch() == null) {\r
73                 assertEquals(FlowUtils.dropInstructions(), f.getInstructions());\r
74                 count += 1;\r
75             } else if (Objects.equals(ep.getMacAddress(), f.getMatch()\r
76                 .getEthernetMatch()\r
77                 .getEthernetSource()\r
78                 .getAddress())) {\r
79                 PolicyInfo pi = policyResolver.getCurrentPolicy();\r
80                 List<ConditionName> cset = endpointManager.getCondsForEndpoint(ep);\r
81                 ConditionGroup cg = pi.getEgCondGroup(new EgKey(tid, eg), cset);\r
82 \r
83                 Instruction ins = f.getInstructions().getInstruction().get(0);\r
84                 assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
85                 List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
86                 NetworkDomainId networkContainement = policyResolver.getTenant(tid)\r
87                     .getEndpointGroup(eg)\r
88                     .getNetworkDomain();\r
89                 int v = OrdinalFactory.getContextOrdinal(ep, networkContainement);\r
90                 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(v)), actions.get(0)\r
91                     .getAction());\r
92                 v = OrdinalFactory.getCondGroupOrdinal(cg);\r
93                 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(v)), actions.get(1)\r
94                     .getAction());\r
95                 v = OrdinalFactory.getContextOrdinal(tid, bd);\r
96                 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(v)), actions.get(2)\r
97                     .getAction());\r
98                 v = OrdinalFactory.getContextOrdinal(tid, fd);\r
99                 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(v)), actions.get(3)\r
100                     .getAction());\r
101                 v = OrdinalFactory.getContextOrdinal(tid, l3c);\r
102                 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(v)), actions.get(4)\r
103                     .getAction());\r
104                 count += 1;\r
105             }\r
106         }\r
107         assertEquals(2, count);\r
108         int numberOfFlows = fm.getTableForNode(nodeId, (short) 1).getFlow().size();\r
109         fm = dosync(flowMap);\r
110         assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 1).getFlow().size());\r
111     }\r
112 \r
113 }\r