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