Add missing license headers
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / ingressnat / IngressNatMapperFlowsTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.ingressnat;
9
10 import org.apache.commons.lang3.ArrayUtils;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.InOrder;
14 import org.mockito.Mockito;
15 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
19 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowIdUtils;
21 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
22 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
48
49 import java.math.BigInteger;
50 import java.util.ArrayList;
51 import java.util.List;
52
53 import static org.junit.Assert.assertNotNull;
54 import static org.mockito.Mockito.*;
55 import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.*;
56
57 public class IngressNatMapperFlowsTest extends MapperUtilsTest {
58
59     private IngressNatMapperFlows flows;
60
61     @Before
62     public void init() throws Exception {
63         ctx = mock(OfContext.class);
64         ofWriter = mock(OfWriter.class);
65         endpointManager = mock(EndpointManager.class);
66         policyManager = mock(PolicyManager.class);
67         policyInfo = mock(PolicyInfo.class);
68         tableId = 1;
69         flows = new IngressNatMapperFlows(NODE_ID, tableId);
70         OrdinalFactory.resetPolicyOrdinalValue();
71     }
72
73     @Test
74     public void testBaseFlow() {
75         Flow testFlow = buildFlow(FlowIdUtils.newFlowId("gotoDestinationMapper"), tableId, 50, null,
76                 FlowUtils.gotoTableInstructions((short) 2)).build();
77
78         flows.baseFlow((short) 2, 50, ofWriter);
79         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
80     }
81
82     @Test
83     public void testNatFlow_noAugmentation() {
84         EndpointL3 testEndpointL3 = new EndpointL3Builder().build();
85
86         flows.createNatFlow((short) 3, testEndpointL3, null, 60, ofWriter);
87         verifyZeroInteractions(ctx);
88         verifyZeroInteractions(ofWriter);
89     }
90
91     @Test
92     public void testNatFlow() throws Exception {
93         EndpointL3 testEndpointL3 = buildL3Endpoint(IPV4_0, IPV4_1, MAC_0, null).build();
94         Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0).build();
95
96         when(ctx.getEndpointManager()).thenReturn(endpointManager);
97         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
98         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
99
100         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
101
102         InOrder order = inOrder(ctx);
103         order.verify(ctx, times(1)).getEndpointManager();
104         order.verify(ctx, times(1)).getCurrentPolicy();
105         verify(ctx, times(3)).getTenant(TENANT_ID);
106         assertNotNull(ordinals);
107
108         List<Instruction> instructions = new ArrayList<>();
109         Action[] ipActions = {FlowUtils.setIpv4DstAction(new Ipv4Address(IPV4_1)),
110                 FlowUtils.setDlDstAction(new MacAddress(MAC_0))};
111         Action[] ordinalsAction = {FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(1)),
112                 FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0)),
113                 FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(3)),
114                 FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(4)),
115                 FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(5)),
116                 FlowUtils.nxLoadTunIdAction(BigInteger.valueOf(ordinals.getTunnelId()), false)};
117         instructions.add(new InstructionBuilder().setOrder(0)
118                 .setInstruction(FlowUtils.applyActionIns(ArrayUtils.addAll(ipActions, ordinalsAction))).build());
119         instructions.add(new InstructionBuilder().setOrder(1).setInstruction(FlowUtils.gotoTableIns((short) 2)).build());
120         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
121         instructionsBuilder.setInstruction(instructions);
122
123         MatchBuilder matchBuilder = new MatchBuilder();
124         matchBuilder.setLayer3Match(new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(IPV4_0.getValue() +
125                 IP_PREFIX_32)).build());
126         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4));
127
128         Flow testFlow = buildFlow(new FlowId("IngressNat" + "|" + new IpAddress(new Ipv4Address(IPV4_0)).toString() +
129                         "|" + new IpAddress(new Ipv4Address(IPV4_1)).toString() + "|" + new MacAddress(MAC_0)), tableId,
130                 60, matchBuilder.build(), instructionsBuilder.build()).build();
131         flows.createNatFlow((short) 2, testEndpointL3, ordinals, 60, ofWriter);
132         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
133     }
134
135     @Test
136     public void testArpFlow_noAugmentation() {
137         EndpointL3 testEndpointL3 = new EndpointL3Builder().build();
138
139         flows.createArpFlow(getTestIndexedTenant(), testEndpointL3, 60, ofWriter);
140         verifyZeroInteractions(ctx);
141         verifyZeroInteractions(ofWriter);
142     }
143
144     @Test
145     public void testArpFlow() {
146         EndpointL3 testEndpointL3 = buildL3Endpoint(IPV4_0, IPV4_1, MAC_0, null).build();
147         List<Instruction> instructions = new ArrayList<>();
148         Action[] arpActions = {nxMoveEthSrcToEthDstAction(), setDlSrcAction(new MacAddress(MAC_0)),
149                 nxLoadArpOpAction(BigInteger.valueOf(2L)), nxMoveArpShaToArpThaAction(), nxLoadArpShaAction(new BigInteger("0")),
150                 nxMoveArpSpaToArpTpaAction(), nxLoadArpSpaAction(IPV4_0.getValue()), outputAction(new NodeConnectorId(NODE_ID.getValue() + ":INPORT"))};
151         instructions.add(new InstructionBuilder().setOrder(0)
152                 .setInstruction(FlowUtils.applyActionIns(ArrayUtils.addAll(arpActions))).build());
153         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
154         instructionsBuilder.setInstruction(instructions);
155
156         MatchBuilder matchBuilder = new MatchBuilder();
157         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.ARP));
158         matchBuilder.setLayer3Match(new ArpMatchBuilder().setArpOp(1).setArpTargetTransportAddress(new Ipv4Prefix(IPV4_0.getValue()
159                 + IP_PREFIX_32)).build());
160         Match match = matchBuilder.build();
161
162         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "outside-ip-arp", match),
163                 tableId, 60, match, instructionsBuilder.build()).build();
164
165         flows.createArpFlow(getTestIndexedTenant(), testEndpointL3, 60, ofWriter);
166         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
167     }
168
169     @Test
170     public void testIngressExternalNatFlow() throws Exception {
171         Endpoint testEndpoint = buildEndpoint(IPV4_1, MAC_0, CONNECTOR_0).build();
172
173         when(ctx.getEndpointManager()).thenReturn(endpointManager);
174         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
175         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
176
177         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
178
179         InOrder order = inOrder(ctx);
180         order.verify(ctx, times(1)).getEndpointManager();
181         order.verify(ctx, times(1)).getCurrentPolicy();
182         verify(ctx, times(3)).getTenant(TENANT_ID);
183         assertNotNull(ordinals);
184
185         List<Instruction> instructions = new ArrayList<>();
186         Action[] ordinalsAction = {FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(1)),
187                 FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0)),
188                 FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(3)),
189                 FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(4)),
190                 FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(5)),
191                 FlowUtils.nxLoadTunIdAction(BigInteger.valueOf(ordinals.getTunnelId()), false)};
192         instructions.add(new InstructionBuilder().setOrder(0)
193                 .setInstruction(FlowUtils.applyActionIns(ArrayUtils.addAll(ordinalsAction))).build());
194         instructions.add(new InstructionBuilder().setOrder(1).setInstruction(FlowUtils.gotoTableIns((short) 2)).build());
195         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
196         instructionsBuilder.setInstruction(instructions);
197
198         MatchBuilder matchBuilder = new MatchBuilder();
199         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4));
200         matchBuilder.setLayer3Match(new Ipv4MatchBuilder().setIpv4Source(new Ipv4Prefix(IPV4_1.getValue()
201                 + IP_PREFIX_32)).build());
202         Match match = matchBuilder.build();
203
204         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "inbound-external-ip", match), tableId, 50, match,
205                 instructionsBuilder.build()).build();
206
207         flows.createIngressExternalNatFlows((short) 2, testEndpoint, ordinals, 50, ofWriter);
208         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
209
210     }
211
212     @Test
213     public void testIngressExternalArpFlow() throws Exception {
214         Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0).build();
215
216         when(ctx.getEndpointManager()).thenReturn(endpointManager);
217         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
218         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
219
220         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
221
222         InOrder order = inOrder(ctx);
223         order.verify(ctx, times(1)).getEndpointManager();
224         order.verify(ctx, times(1)).getCurrentPolicy();
225         verify(ctx, times(3)).getTenant(TENANT_ID);
226         assertNotNull(ordinals);
227
228         List<Instruction> instructions = new ArrayList<>();
229         Action[] ordinalsAction = {FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(1)),
230                 FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0)),
231                 FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(3)),
232                 FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(4)),
233                 FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(5)),
234                 FlowUtils.nxLoadTunIdAction(BigInteger.valueOf(ordinals.getTunnelId()), false)};
235         instructions.add(new InstructionBuilder().setOrder(0)
236                 .setInstruction(FlowUtils.applyActionIns(ArrayUtils.addAll(ordinalsAction))).build());
237         instructions.add(new InstructionBuilder().setOrder(1).setInstruction(FlowUtils.gotoTableIns((short) 0)).build());
238         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
239         instructionsBuilder.setInstruction(instructions);
240
241         MatchBuilder matchBuilder = new MatchBuilder();
242         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(new MacAddress(MAC_0), null, FlowUtils.ARP));
243         Match match = matchBuilder.build();
244
245         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "inbound-external-arp", match), tableId, 30, match,
246                 instructionsBuilder.build()).build();
247
248         flows.createIngressExternalArpFlows((short) 0, testEndpoint, ordinals, 30, ofWriter);
249         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
250     }
251
252 }