ebf4480d05d524c246eea9202bafefc89bba78cd
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / source / SourceMapperFlowsTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.source;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.mockito.InOrder;
6 import org.mockito.Mockito;
7 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
8 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
9 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
10 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
11 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowIdUtils;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
32
33 import java.math.BigInteger;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import static org.junit.Assert.assertNotNull;
38 import static org.mockito.Mockito.*;
39
40
41 public class SourceMapperFlowsTest extends MapperUtilsTest {
42
43     private SourceMapperFlows flows;
44
45     @Before
46     public void init() {
47         tableId = 2;
48         ctx = mock(OfContext.class);
49         endpointManager = mock(EndpointManager.class);
50         policyInfo = mock(PolicyInfo.class);
51         ofWriter = mock(OfWriter.class);
52         flows = new SourceMapperFlows(NODE_ID, tableId);
53         OrdinalFactory.resetPolicyOrdinalValue();
54     }
55
56     @Test
57     public void dropFlow_noEthertype() {
58         Flow testFlow = buildFlow(new FlowId(DROP_ALL), tableId, 100, null, FlowUtils.dropInstructions()).build();
59
60         flows.dropFlow(100, null, ofWriter);
61         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
62     }
63
64     @Test
65     public void dropFlow_ipV4Ethertype() {
66         MatchBuilder matchBuilder = new MatchBuilder();
67         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4));
68         Match match = matchBuilder.build();
69         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
70                 FlowUtils.dropInstructions()).build();
71
72         flows.dropFlow(100, FlowUtils.IPv4, ofWriter);
73         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
74     }
75
76     @Test
77     public void dropFlow_ipV6Ethertype() {
78         MatchBuilder matchBuilder = new MatchBuilder();
79         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv6));
80         Match match = matchBuilder.build();
81         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
82                 FlowUtils.dropInstructions()).build();
83
84         flows.dropFlow(100, FlowUtils.IPv6, ofWriter);
85         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
86     }
87
88     @Test
89     public void dropFlow_arpEthertype() {
90         MatchBuilder matchBuilder = new MatchBuilder();
91         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.ARP));
92         Match match = matchBuilder.build();
93         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
94                 FlowUtils.dropInstructions()).build();
95
96         flows.dropFlow(100, FlowUtils.ARP, ofWriter);
97         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
98     }
99
100     @Test
101     public void synchronizeEp() throws Exception {
102         Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1).build();
103
104         when(ctx.getEndpointManager()).thenReturn(endpointManager);
105         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
106         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
107
108         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
109
110         InOrder order = inOrder(ctx);
111         order.verify(ctx, times(1)).getEndpointManager();
112         order.verify(ctx, times(1)).getCurrentPolicy();
113         verify(ctx, times(2)).getTenant(TENANT_ID);
114         assertNotNull(ordinals);
115
116         Action reg0 = FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(1));
117         Action reg1 = FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0));
118         Action reg4 = FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(0));
119         Action reg5 = FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(0));
120         Action reg6 = FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(0));
121         Action tunnelId = FlowUtils.nxLoadTunIdAction(BigInteger.valueOf(2), false);
122         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
123         List<Instruction> instructions = new ArrayList<>();
124         InstructionBuilder ordinalsInstruction = new InstructionBuilder();
125         ordinalsInstruction.setInstruction(FlowUtils.applyActionIns(reg0, reg1, reg4, reg5, reg6,
126                 tunnelId)).setOrder(0);
127         InstructionBuilder goToInstruction = new InstructionBuilder();
128         goToInstruction.setInstruction(FlowUtils.gotoTableIns((short) 3)).setOrder(1);
129         instructions.add(ordinalsInstruction.build());
130         instructions.add(goToInstruction.build());
131         instructionsBuilder.setInstruction(instructions);
132
133         MatchBuilder matchBuilder = new MatchBuilder();
134         matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(new MacAddress(MAC_1), null, null))
135                 .setInPort(new NodeConnectorId(CONNECTOR_1));
136         Match match = matchBuilder.build();
137
138         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "ep", match), tableId, 90, match,
139                 instructionsBuilder.build()).build();
140
141         flows.synchronizeEp((short) 3, 90, ordinals, new MacAddress(MAC_1), new NodeConnectorId(CONNECTOR_1), ofWriter);
142         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
143     }
144
145     @Test
146     public void createTunnelFlow() throws Exception {
147         Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0).build();
148
149         when(ctx.getEndpointManager()).thenReturn(endpointManager);
150         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
151         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
152
153         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
154
155         InOrder order = inOrder(ctx);
156         order.verify(ctx, times(1)).getEndpointManager();
157         order.verify(ctx, times(1)).getCurrentPolicy();
158         verify(ctx, times(2)).getTenant(TENANT_ID);
159         assertNotNull(ordinals);
160
161         Action reg0 = FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(1));
162         Action reg1 = FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(0xffffff));
163         Action reg4 = FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(0));
164         Action reg5 = FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(0));
165         Action reg6 = FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(0));
166         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
167         List<Instruction> instructions = new ArrayList<>();
168         InstructionBuilder ordinalsInstruction = new InstructionBuilder();
169         ordinalsInstruction.setInstruction(FlowUtils.applyActionIns(reg0, reg1, reg4, reg5, reg6)).setOrder(0);
170         InstructionBuilder goToInstruction = new InstructionBuilder();
171         goToInstruction.setInstruction(FlowUtils.gotoTableIns((short) 3)).setOrder(1);
172         instructions.add(ordinalsInstruction.build());
173         instructions.add(goToInstruction.build());
174         instructionsBuilder.setInstruction(instructions);
175
176         MatchBuilder matchBuilder = new MatchBuilder();
177         matchBuilder.setInPort(new NodeConnectorId(CONNECTOR_0));
178         FlowUtils.addNxTunIdMatch(matchBuilder, 2);
179         Match match = matchBuilder.build();
180
181         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "tunnel", match), tableId, 80, match,
182                 instructionsBuilder.build()).build();
183
184         flows.createTunnelFlow((short) 3, 80, new NodeConnectorId(CONNECTOR_0), ordinals, ofWriter);
185         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
186     }
187
188     @Test
189     public void createBroadcastFlow() throws Exception {
190         Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_1).build();
191
192         when(ctx.getEndpointManager()).thenReturn(endpointManager);
193         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
194         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
195
196         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
197
198         InOrder order = inOrder(ctx);
199         order.verify(ctx, times(1)).getEndpointManager();
200         order.verify(ctx, times(1)).getCurrentPolicy();
201         verify(ctx, times(2)).getTenant(TENANT_ID);
202         assertNotNull(ordinals);
203
204         Action reg5 = FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(0));
205         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
206         List<Instruction> instructions = new ArrayList<>();
207         InstructionBuilder ordinalsInstruction = new InstructionBuilder();
208         ordinalsInstruction.setInstruction(FlowUtils.applyActionIns(reg5)).setOrder(0);
209         InstructionBuilder goToInstruction = new InstructionBuilder();
210         goToInstruction.setInstruction(FlowUtils.gotoTableIns((short) 3)).setOrder(1);
211         instructions.add(ordinalsInstruction.build());
212         instructions.add(goToInstruction.build());
213         instructionsBuilder.setInstruction(instructions);
214
215         MatchBuilder matchBuilder = new MatchBuilder();
216         matchBuilder.setInPort(new NodeConnectorId(CONNECTOR_1));
217         FlowUtils.addNxTunIdMatch(matchBuilder, 0);
218         Match match = matchBuilder.build();
219
220         Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "tunnelFdId", match), tableId, 80, match,
221                 instructionsBuilder.build()).build();
222
223         flows.createBroadcastFlow((short) 3, 80, new NodeConnectorId(CONNECTOR_1), ordinals, ofWriter);
224         verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
225     }
226 }