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