1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.source;
3 import org.junit.Before;
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;
33 import java.math.BigInteger;
34 import java.util.ArrayList;
35 import java.util.List;
37 import static org.junit.Assert.assertNotNull;
38 import static org.mockito.Mockito.*;
41 public class SourceMapperFlowsTest extends MapperUtilsTest {
43 private SourceMapperFlows flows;
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();
57 public void dropFlow_noEthertype() {
58 Flow testFlow = buildFlow(new FlowId(DROP_ALL), tableId, 100, null, FlowUtils.dropInstructions()).build();
60 flows.dropFlow(100, null, ofWriter);
61 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
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();
72 flows.dropFlow(100, FlowUtils.IPv4, ofWriter);
73 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
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();
84 flows.dropFlow(100, FlowUtils.IPv6, ofWriter);
85 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
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();
96 flows.dropFlow(100, FlowUtils.ARP, ofWriter);
97 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
101 public void synchronizeEp() throws Exception {
102 Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1).build();
104 when(ctx.getEndpointManager()).thenReturn(endpointManager);
105 when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
106 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
108 OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
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);
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(ordinals.getTunnelId()), 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);
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();
138 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "ep", match), tableId, 90, match,
139 instructionsBuilder.build()).build();
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);
146 public void createTunnelFlow() throws Exception {
147 Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0).build();
149 when(ctx.getEndpointManager()).thenReturn(endpointManager);
150 when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
151 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
153 OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
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);
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);
176 MatchBuilder matchBuilder = new MatchBuilder();
177 matchBuilder.setInPort(new NodeConnectorId(CONNECTOR_0));
178 FlowUtils.addNxTunIdMatch(matchBuilder, ordinals.getTunnelId());
179 Match match = matchBuilder.build();
181 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "tunnel", match), tableId, 80, match,
182 instructionsBuilder.build()).build();
184 flows.createTunnelFlow((short) 3, 80, new NodeConnectorId(CONNECTOR_0), ordinals, ofWriter);
185 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
189 public void createBroadcastFlow() throws Exception {
190 Endpoint testEndpoint = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_1).build();
192 when(ctx.getEndpointManager()).thenReturn(endpointManager);
193 when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
194 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
196 OrdinalFactory.EndpointFwdCtxOrdinals ordinals = OrdinalFactory.getEndpointFwdCtxOrdinals(ctx, testEndpoint);
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);
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);
215 MatchBuilder matchBuilder = new MatchBuilder();
216 matchBuilder.setInPort(new NodeConnectorId(CONNECTOR_1));
217 FlowUtils.addNxTunIdMatch(matchBuilder, 0);
218 Match match = matchBuilder.build();
220 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "tunnelFdId", match), tableId, 80, match,
221 instructionsBuilder.build()).build();
223 flows.createBroadcastFlow((short) 3, 80, new NodeConnectorId(CONNECTOR_1), ordinals, ofWriter);
224 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);