Bug 5617: OfOverlay Refactoring - Destination mapper
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / egressnat / EgressNatMapperFlowsTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.egressnat;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
6 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowIdUtils;
7 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
8 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
9 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import static org.mockito.Mockito.*;
32
33 public class EgressNatMapperFlowsTest extends MapperUtilsTest {
34
35     private static final String EGRESS_NAT = "EgressNat|";
36     private EgressNatMapperFlows flows;
37     private short tableId;
38
39     @Before
40     public void init() {
41         tableId = 5;
42         ofWriter = mock(OfWriter.class);
43         flows = new EgressNatMapperFlows(NODE_ID, tableId);
44     }
45
46     @Test
47     public void testDropFlow_noEthertype() {
48         Flow testFlow = buildFlow(new FlowId(DROP_ALL), tableId, 100, null,
49                 FlowUtils.dropInstructions()).build();
50
51         flows.dropFlow(100, null, ofWriter);
52         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
53     }
54
55     @Test
56     public void testDropFlow_ipV4Ethertype() {
57         MatchBuilder matchBuilder = new MatchBuilder();
58         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4));
59         Match match = matchBuilder.build();
60         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
61                 FlowUtils.dropInstructions()).build();
62
63         flows.dropFlow(100, FlowUtils.IPv4, ofWriter);
64         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
65     }
66
67     @Test
68     public void testDropFlow_ipV6Ethertype() {
69         MatchBuilder matchBuilder = new MatchBuilder();
70         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv6));
71         Match match = matchBuilder.build();
72         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
73                 FlowUtils.dropInstructions()).build();
74
75         flows.dropFlow(100, FlowUtils.IPv6, ofWriter);
76         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
77     }
78
79     @Test
80     public void testDropFlow_arpEthertype() {
81         MatchBuilder matchBuilder = new MatchBuilder();
82         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.ARP));
83         Match match = matchBuilder.build();
84         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
85                 FlowUtils.dropInstructions()).build();
86
87         flows.dropFlow(100, FlowUtils.ARP, ofWriter);
88         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
89     }
90
91     @Test
92     public void testNatFlows_noAugmentation() {
93         EndpointL3Builder endpointL3Builder = buildL3Endpoint(IPV4_1, IPV4_0, MAC_0, L2);
94         endpointL3Builder.addAugmentation(NatAddress.class, null);
95         flows.natFlows((short) 6, endpointL3Builder.build(), 100, ofWriter);
96         verifyZeroInteractions(ofWriter);
97     }
98
99     @Test
100     public void testNatFlows_ipv4() {
101         EndpointL3 endpointL3 = buildL3Endpoint(IPV4_0, IPV4_1, MAC_0, L2).build();
102         MatchBuilder matchBuilder = new MatchBuilder();
103         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4))
104                 .setLayer3Match(new Ipv4MatchBuilder().setIpv4Source(new Ipv4Prefix(IPV4_1.getValue() + IP_PREFIX_32))
105                         .build());
106         FlowUtils.addNxRegMatch(matchBuilder, FlowUtils.RegMatch.of(NxmNxReg6.class, (long) 0));
107         Match match = matchBuilder.build();
108
109         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
110         InstructionBuilder apply = new InstructionBuilder();
111         apply.setOrder(0).setInstruction(FlowUtils.applyActionIns(FlowUtils.setIpv4SrcAction(new Ipv4Address(IPV4_0))));
112         InstructionBuilder goTo = new InstructionBuilder();
113         goTo.setOrder(1).setInstruction(FlowUtils.gotoTableIns((short) 6));
114         List<Instruction> instructions = new ArrayList<>();
115         instructions.add(apply.build());
116         instructions.add(goTo.build());
117         instructionsBuilder.setInstruction(instructions);
118
119         Flow testFlow = buildFlow(new FlowId(EGRESS_NAT + new IpAddress(new Ipv4Address(IPV4_1)) + "|" +
120                 new IpAddress(new Ipv4Address(IPV4_0))), tableId, 90, match, instructionsBuilder.build()).build();
121
122         flows.natFlows((short) 6, endpointL3, 90, ofWriter);
123         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
124     }
125
126     @Test
127     public void testNatFlows_ipv6() {
128         EndpointL3 endpointL3 = buildL3Endpoint(IPV6_1, IPV6_2, MAC_0, L2).build();
129         MatchBuilder matchBuilder = new MatchBuilder();
130         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv6))
131                 .setLayer3Match(new Ipv6MatchBuilder().setIpv6Source(new Ipv6Prefix(IPV6_2.getValue() +
132                         IP_PREFIX_128)).build());
133         FlowUtils.addNxRegMatch(matchBuilder, FlowUtils.RegMatch.of(NxmNxReg6.class, (long) 0));
134         Match match = matchBuilder.build();
135
136         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
137         InstructionBuilder apply = new InstructionBuilder();
138         apply.setOrder(0).setInstruction(FlowUtils.applyActionIns(FlowUtils.setIpv6SrcAction(new Ipv6Address(IPV6_1))));
139         InstructionBuilder goTo = new InstructionBuilder();
140         goTo.setOrder(1).setInstruction(FlowUtils.gotoTableIns((short) 6));
141         List<Instruction> instructions = new ArrayList<>();
142         instructions.add(apply.build());
143         instructions.add(goTo.build());
144         instructionsBuilder.setInstruction(instructions);
145
146         Flow testFlow = buildFlow(new FlowId(EGRESS_NAT + new IpAddress(new Ipv6Address(IPV6_2)) + "|" +
147                 new IpAddress(new Ipv6Address(IPV6_1))), tableId, 80, match, instructionsBuilder.build()).build();
148
149         flows.natFlows((short) 6, endpointL3, 80, ofWriter);
150         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
151     }
152
153 }