Bug 3302: fix for GroupTable
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / DestinationMapperTest.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.Collections;\r
13 import java.util.HashMap;\r
14 import java.util.List;\r
15 import java.util.Objects;\r
16 \r
17 import org.junit.Before;\r
18 import org.junit.Test;\r
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;\r
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;\r
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCase;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;\r
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;\r
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;\r
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;\r
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;\r
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;\r
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;\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
43 \r
44 import com.google.common.collect.ImmutableList;\r
45 \r
46 import static org.junit.Assert.*;\r
47 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;\r
48 \r
49 public class DestinationMapperTest extends FlowTableTest {\r
50     protected static final Logger LOG =\r
51             LoggerFactory.getLogger(DestinationMapperTest.class);\r
52 \r
53     NodeConnectorId remoteTunnelId =\r
54             new NodeConnectorId(remoteNodeId.getValue() + ":101");\r
55 \r
56     @Override\r
57     @Before\r
58     public void setup() throws Exception {\r
59         initCtx();\r
60         table = new DestinationMapper(ctx,ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER());\r
61         super.setup();\r
62     }\r
63 \r
64     @Test\r
65     public void testNoEps() throws Exception {\r
66         OfWriter fm = dosync(null);\r
67         assertEquals(1, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER()).getFlow().size());\r
68     }\r
69 \r
70     private void verifyDMap(Endpoint remoteEp,\r
71             Endpoint localEp) throws Exception {\r
72 \r
73         OfWriter fm = dosync(null);\r
74         assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER()).getFlow().size());\r
75 \r
76         // presumably counts flows that have correct matches set up\r
77         int count = 0;\r
78         HashMap<String, Flow> flowMap = new HashMap<>();\r
79         for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER()).getFlow()) {\r
80             flowMap.put(f.getId().getValue(), f);\r
81             if (f.getMatch() == null) {\r
82                 assertEquals(dropInstructions(),\r
83                         f.getInstructions());\r
84                 count += 1;\r
85             } else if (Objects.equals(ethernetMatch(null, null, ARP),\r
86                     f.getMatch().getEthernetMatch())) {\r
87                 // router ARP reply\r
88                 Instruction ins = f.getInstructions().getInstruction().get(0);\r
89                 ins = f.getInstructions().getInstruction().get(0);\r
90                 assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
91                 List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
92                 assertEquals(nxMoveEthSrcToEthDstAction(),\r
93                         actions.get(0).getAction());\r
94                 assertEquals(Integer.valueOf(0), actions.get(0).getOrder());\r
95                 assertEquals(setDlSrcAction(DestinationMapper.ROUTER_MAC),\r
96                         actions.get(1).getAction());\r
97                 assertEquals(Integer.valueOf(1), actions.get(1).getOrder());\r
98                 assertEquals(nxLoadArpOpAction(BigInteger.valueOf(2L)),\r
99                         actions.get(2).getAction());\r
100                 assertEquals(Integer.valueOf(2), actions.get(2).getOrder());\r
101                 assertEquals(nxMoveArpShaToArpThaAction(),\r
102                         actions.get(3).getAction());\r
103                 assertEquals(Integer.valueOf(3), actions.get(3).getOrder());\r
104                 assertEquals(nxLoadArpShaAction(new BigInteger(1, DestinationMapper\r
105                         .bytesFromHexString(DestinationMapper.ROUTER_MAC\r
106                                 .getValue()))),\r
107                         actions.get(4).getAction());\r
108                 assertEquals(Integer.valueOf(4), actions.get(4).getOrder());\r
109                 assertEquals(nxMoveArpSpaToArpTpaAction(),\r
110                         actions.get(5).getAction());\r
111                 assertEquals(Integer.valueOf(5), actions.get(5).getOrder());\r
112                 assertTrue(nxLoadArpSpaAction("10.0.0.1").equals(actions.get(6).getAction()) ||\r
113                         nxLoadArpSpaAction("10.0.1.1").equals(actions.get(6).getAction()) ||\r
114                         nxLoadArpSpaAction("10.0.2.1").equals(actions.get(6).getAction()));\r
115                 assertEquals(Integer.valueOf(6), actions.get(6).getOrder());\r
116                 count += 1;\r
117             } else if (Objects.equals(localEp.getMacAddress(),\r
118                     f.getMatch().getEthernetMatch()\r
119                             .getEthernetDestination().getAddress())) {\r
120                 int icount = 0;\r
121                 for (Instruction ins : f.getInstructions().getInstruction()) {\r
122                     if (ins.getInstruction() instanceof ApplyActionsCase) {\r
123                         long p = getOfPortNum(nodeConnectorId);\r
124                         List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
125                         assertEquals(nxLoadRegAction(NxmNxReg7.class,\r
126                                 BigInteger.valueOf(p)),\r
127                                 actions.get(2).getAction());\r
128                         icount += 1;\r
129                     } else if (ins.getInstruction() instanceof GoToTableCase) {\r
130                         assertEquals(gotoTableIns(ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()),\r
131                                 ins.getInstruction());\r
132                         icount += 1;\r
133                     }\r
134                 }\r
135                 assertEquals(2, icount);\r
136                 LOG.info("{}", f);\r
137                 count += 1;\r
138             } else if (Objects.equals(remoteEp.getMacAddress(),\r
139                     f.getMatch().getEthernetMatch()\r
140                             .getEthernetDestination().getAddress())) {\r
141                 int icount = 0;\r
142                 for (Instruction ins : f.getInstructions().getInstruction()) {\r
143                     if (ins.getInstruction() instanceof ApplyActionsCase) {\r
144                         long p = getOfPortNum(tunnelId);\r
145                         List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
146                         assertEquals(nxLoadRegAction(NxmNxReg7.class, BigInteger.valueOf(p)),\r
147                                 actions.get(actions.size() - 1).getAction());\r
148                         icount += 1;\r
149                     } else if (ins.getInstruction() instanceof GoToTableCase) {\r
150                         assertEquals(gotoTableIns((short) (table.getTableId() + 1)),\r
151                                 ins.getInstruction());\r
152                         icount += 1;\r
153                     }\r
154                 }\r
155                 assertEquals(2, icount);\r
156                 LOG.info("{}", f);\r
157                 count += 1;\r
158             } else if (Objects.equals(DestinationMapper.ROUTER_MAC,\r
159                     f.getMatch().getEthernetMatch()\r
160                             .getEthernetDestination()\r
161                             .getAddress())) {\r
162                 if (f.getMatch().getLayer3Match() instanceof Ipv4Match) {\r
163                     // should be local port with rewrite dlsrc and dldst plus\r
164                     // ttl decr\r
165                     Instruction ins = f.getInstructions().getInstruction().get(0);\r
166                     assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
167                     List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
168                     long p = getOfPortNum(nodeConnectorId);\r
169                     assertEquals(nxLoadRegAction(NxmNxReg7.class,\r
170                             BigInteger.valueOf(p)),\r
171                             actions.get(2).getAction());\r
172                     assertEquals(Integer.valueOf(2), actions.get(2).getOrder());\r
173                     assertEquals(Integer.valueOf(3), actions.get(3).getOrder());\r
174                     assertEquals(Integer.valueOf(4), actions.get(4).getOrder());\r
175                     assertEquals(decNwTtlAction(),\r
176                             actions.get(5).getAction());\r
177                     assertEquals(Integer.valueOf(5), actions.get(5).getOrder());\r
178                     count += 1;\r
179                 } else if (f.getMatch().getLayer3Match() instanceof Ipv6Match) {\r
180                     // should be remote port with rewrite dlsrc plus\r
181                     // ttl decr\r
182                     Instruction ins = f.getInstructions().getInstruction().get(0);\r
183                     assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
184                     List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
185                     long p = getOfPortNum(tunnelId);\r
186                     assertEquals(nxLoadRegAction(NxmNxReg7.class,\r
187                             BigInteger.valueOf(p)),\r
188                             actions.get(4).getAction());\r
189                     assertEquals(Integer.valueOf(4), actions.get(4).getOrder());\r
190                     assertEquals(setDlSrcAction(DestinationMapper.ROUTER_MAC),\r
191                             actions.get(5).getAction());\r
192                     assertEquals(Integer.valueOf(5), actions.get(5).getOrder());\r
193                     assertEquals(decNwTtlAction(),\r
194                             actions.get(6).getAction());\r
195                     assertEquals(Integer.valueOf(6), actions.get(6).getOrder());\r
196                     count += 1;\r
197                 }\r
198             } else if (Objects.equals(DestinationMapper.MULTICAST_MAC,\r
199                     f.getMatch().getEthernetMatch()\r
200                             .getEthernetDestination()\r
201                             .getAddress())) {\r
202                 // broadcast/multicast flow should output to group table\r
203                 Instruction ins = f.getInstructions().getInstruction().get(0);\r
204                 ins = f.getInstructions().getInstruction().get(0);\r
205                 assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
206                 List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
207                 assertEquals(nxMoveRegTunIdAction(NxmNxReg0.class, false),\r
208                         actions.get(0).getAction());\r
209                 assertEquals(Integer.valueOf(0), actions.get(0).getOrder());\r
210 \r
211                 Long v = Long.valueOf(OrdinalFactory.getContextOrdinal(tid, fd));\r
212                 assertEquals(groupAction(v), actions.get(1).getAction());\r
213                 assertEquals(Integer.valueOf(1), actions.get(1).getOrder());\r
214                 count += 1;\r
215             }\r
216         }\r
217 \r
218         // TODO Li alagalah: Due to subnet checking this test is no longer setup\r
219         // correct. Must address before Li.\r
220         // assertEquals(8, count);\r
221         assertEquals(fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_DESTINATION_MAPPER()).getFlow().size(), count);\r
222         int numberOfFlows = fm.getTableForNode(nodeId, (short) 2).getFlow().size();\r
223         fm = dosync(flowMap);\r
224         assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 2).getFlow().size());\r
225     }\r
226 \r
227     @Override\r
228     protected EndpointBuilder localEP() {\r
229         return super.localEP()\r
230                 .setL3Address(ImmutableList.of(new L3AddressBuilder()\r
231                         .setL3Context(l3c)\r
232                         .setIpAddress(new IpAddress(new Ipv4Address("10.0.0.1")))\r
233                         .build()));\r
234     }\r
235 \r
236     @Override\r
237     protected EndpointBuilder remoteEP(NodeId remoteNodeId) {\r
238         return super.remoteEP(remoteNodeId)\r
239                 .setL3Address(ImmutableList.of(new L3AddressBuilder()\r
240                         .setL3Context(l3c)\r
241                         .setIpAddress(new IpAddress(new Ipv6Address("::ffff:0:0:0:10.0.0.2")))\r
242                         .build()));\r
243     }\r
244 \r
245     private void addSwitches() {\r
246         switchManager.addSwitch(\r
247                 nodeId,\r
248                 tunnelId,\r
249                 Collections.<NodeConnectorId>emptySet(),\r
250                 new OfOverlayNodeConfigBuilder().setTunnel(\r
251                         ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))\r
252                             .setTunnelType(TunnelTypeVxlan.class)\r
253                             .setNodeConnectorId(tunnelId)\r
254                             .build())).build());\r
255         switchManager.addSwitch(\r
256                 remoteNodeId,\r
257                 remoteTunnelId,\r
258                 Collections.<NodeConnectorId>emptySet(),\r
259                 new OfOverlayNodeConfigBuilder().setTunnel(\r
260                         ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.5")))\r
261                             .setTunnelType(TunnelTypeVxlan.class)\r
262                             .setNodeConnectorId(tunnelId)\r
263                             .build())).build());\r
264     }\r
265 \r
266     @Test\r
267     public void testSame() throws Exception {\r
268         addSwitches();\r
269         Endpoint localEp = localEP().build();\r
270         endpointManager.addEndpoint(localEp);\r
271         Endpoint remoteEp = remoteEP(remoteNodeId).build();\r
272         endpointManager.addEndpoint(remoteEp);\r
273 \r
274 \r
275         policyResolver.addTenant(baseTenant().setContract(\r
276                 ImmutableList.<Contract> of(baseContract(null).build())).build());\r
277         verifyDMap(remoteEp, localEp);\r
278     }\r
279 \r
280     @Test\r
281     public void testDiff() throws Exception {\r
282         addSwitches();\r
283         Endpoint localEp = localEP().build();\r
284         endpointManager.addEndpoint(localEp);\r
285         Endpoint remoteEp = remoteEP(remoteNodeId)\r
286                 .setEndpointGroup(eg2)\r
287                 .build();\r
288         endpointManager.addEndpoint(remoteEp);\r
289 \r
290         policyResolver.addTenant(baseTenant().setContract(\r
291                 ImmutableList.<Contract> of(baseContract(null).build())).build());\r
292         verifyDMap(remoteEp, localEp);\r
293     }\r
294 \r
295 }\r