1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.portsecurity;
3 import org.junit.Before;
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.rev130715.IpAddress;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
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.Endpoint;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
28 import java.util.ArrayList;
29 import java.util.List;
31 import static org.mockito.Mockito.*;
33 public class PortSecurityFlowsTest extends MapperUtilsTest {
35 private static final String L3 = "L3";
36 private static final String DHCP = "dhcp";
37 private PortSecurityFlows flows;
42 ofWriter = mock(OfWriter.class);
43 flows = new PortSecurityFlows(NODE_ID, tableId);
47 public void testDropFlow_noEthertype() {
48 Flow testFlow = buildFlow(new FlowId(DROP_ALL), tableId, 100, null, FlowUtils.dropInstructions()).build();
50 flows.dropFlow(100, null, ofWriter);
51 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
55 public void testDropFlow_ipV4Ethertype() {
56 MatchBuilder matchBuilder = new MatchBuilder();
57 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv4));
58 Match match = matchBuilder.build();
59 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
60 FlowUtils.dropInstructions()).build();
62 flows.dropFlow(100, FlowUtils.IPv4, ofWriter);
63 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
67 public void testDropFlow_ipV6Ethertype() {
68 MatchBuilder matchBuilder = new MatchBuilder();
69 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.IPv6));
70 Match match = matchBuilder.build();
71 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
72 FlowUtils.dropInstructions()).build();
74 flows.dropFlow(100, FlowUtils.IPv6, ofWriter);
75 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
79 public void testDropFlow_arpEthertype() {
80 MatchBuilder matchBuilder = new MatchBuilder();
81 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(null, null, FlowUtils.ARP));
82 Match match = matchBuilder.build();
83 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DROP, match), tableId, 100, match,
84 FlowUtils.dropInstructions()).build();
86 flows.dropFlow(100, FlowUtils.ARP, ofWriter);
87 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
91 public void testFlowAllowFromTunnel_vxLan() {
92 final int VXLAN_PORT = 0;
93 MatchBuilder matchBuilder = new MatchBuilder();
94 matchBuilder.setInPort(new NodeConnectorId(String.valueOf(VXLAN_PORT)));
95 Match match = matchBuilder.build();
96 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, ALLOW, match), tableId, 300, match,
97 FlowUtils.gotoTableInstructions((short) 2)).build();
99 flows.allowFromTunnelFlow((short) 2, 300, new NodeConnectorId(CONNECTOR_0), ofWriter);
100 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
105 public void testFlowAllowFromTunnel_vxLanGpe() {
106 final int VXLAN_PORT = 1;
107 MatchBuilder matchBuilder = new MatchBuilder();
108 matchBuilder.setInPort(new NodeConnectorId(String.valueOf(VXLAN_PORT)));
109 Match match = matchBuilder.build();
110 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, ALLOW, match), tableId, 300, match,
111 FlowUtils.gotoTableInstructions((short) 2)).build();
113 flows.allowFromTunnelFlow((short) 2, 300, new NodeConnectorId(CONNECTOR_1), ofWriter);
114 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
119 public void testL3flow_ipv4() {
120 Endpoint testEp = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0).build();
122 MatchBuilder matchBuilder = new MatchBuilder();
123 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(MAC_0, null, FlowUtils.IPv4))
124 .setLayer3Match(new Ipv4MatchBuilder()
125 .setIpv4Source(new Ipv4Prefix(IPV4_0.getValue() + IP_PREFIX_32)).build())
126 .setInPort(new NodeConnectorId(CONNECTOR_0));
127 Match match = matchBuilder.build();
129 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, L3, match), tableId, 100, match,
130 FlowUtils.gotoTableInstructions((short) 2)).build();
132 flows.l3Flow((short) 2, testEp, new NodeConnectorId(CONNECTOR_0), new MacAddress(MAC_0), 100, false, ofWriter);
133 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
137 public void testL3flow_ipv4Arp() {
138 Endpoint testEp = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1).build();
140 MatchBuilder matchBuilder = new MatchBuilder();
141 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(MAC_1, null, FlowUtils.ARP))
142 .setLayer3Match(new ArpMatchBuilder().setArpSourceTransportAddress(new Ipv4Prefix(IPV4_0.getValue()
143 + IP_PREFIX_32)).build())
144 .setInPort(new NodeConnectorId(CONNECTOR_1));
145 Match match = matchBuilder.build();
147 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, L3, match), tableId, 100, match,
148 FlowUtils.gotoTableInstructions((short) 2)).build();
150 flows.l3Flow((short) 2, testEp, new NodeConnectorId(CONNECTOR_1), new MacAddress(MAC_1), 100, true, ofWriter);
151 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
155 public void testL3flow_ipv6() {
156 Endpoint testEp = buildEndpoint(IPV6_1, MAC_0, CONNECTOR_0).build();
158 MatchBuilder matchBuilder = new MatchBuilder();
159 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(MAC_0, null, FlowUtils.IPv6))
160 .setLayer3Match(new Ipv6MatchBuilder()
161 .setIpv6Source(new Ipv6Prefix(IPV6_1.getValue() + IP_PREFIX_128)).build())
162 .setInPort(new NodeConnectorId(CONNECTOR_0));
163 Match match = matchBuilder.build();
165 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, L3, match), tableId, 100, match,
166 FlowUtils.gotoTableInstructions((short) 2)).build();
168 flows.l3Flow((short) 2, testEp, new NodeConnectorId(CONNECTOR_0), new MacAddress(MAC_0), 100, false, ofWriter);
169 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
173 public void testL3flow_ipv6Arp() {
174 Endpoint testEp = buildEndpoint(IPV6_1, MAC_1, CONNECTOR_1).build();
176 flows.l3Flow((short) 2, testEp, new NodeConnectorId(CONNECTOR_1), new MacAddress(MAC_1), 100, true, ofWriter);
177 verifyZeroInteractions(ofWriter);
181 public void testL3DhcpDoraFlow() {
182 IpAddress ipAddress = new IpAddress(new Ipv4Address("255.255.255.255"));
183 MacAddress macAddress = new MacAddress(MAC_1);
184 NodeConnectorId connectorId = new NodeConnectorId(CONNECTOR_1);
186 MatchBuilder matchBuilder = new MatchBuilder();
187 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(macAddress, null, FlowUtils.IPv4))
188 .setLayer3Match(new Ipv4MatchBuilder()
189 .setIpv4Destination(new Ipv4Prefix(ipAddress.getIpv4Address().getValue() + IP_PREFIX_32)).build())
190 .setInPort(connectorId);
191 Match match = matchBuilder.build();
193 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, DHCP, match), tableId, 50, match,
194 FlowUtils.gotoTableInstructions((short) 2)).build();
196 flows.l3DhcpDoraFlow((short) 2, new NodeConnectorId(CONNECTOR_1), new MacAddress(MAC_1), 50, ofWriter);
197 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
201 public void testL2Flow() {
202 MacAddress macAddress = new MacAddress(MAC_0);
203 NodeConnectorId connectorId = new NodeConnectorId(CONNECTOR_0);
205 MatchBuilder matchBuilder = new MatchBuilder();
206 matchBuilder.setEthernetMatch(FlowUtils.ethernetMatch(macAddress, null, null))
207 .setInPort(connectorId);
208 Match match = matchBuilder.build();
210 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, L2, match), tableId, 100, match,
211 FlowUtils.gotoTableInstructions((short) 2)).build();
213 flows.l2flow((short) 2, new NodeConnectorId(CONNECTOR_0), new MacAddress(MAC_0), 100, ofWriter);
214 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
218 public void testPopVlanTagsOnExternalPortFlow() {
219 NodeConnectorId connectorId = new NodeConnectorId(CONNECTOR_0);
220 MatchBuilder matchBuilder = new MatchBuilder();
221 matchBuilder.setVlanMatch(FlowUtils.vlanMatch(1, true))
222 .setInPort(connectorId);
223 Match match = matchBuilder.build();
225 List<Instruction> instructions = new ArrayList<>();
226 instructions.add(FlowUtils.popVlanInstruction(0));
227 instructions.add(new InstructionBuilder().setOrder(1)
228 .setInstruction(FlowUtils.gotoTableIns((short) 0))
230 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
231 instructionsBuilder.setInstruction(instructions);
233 List<L2FloodDomain> l2FloodDomains = getL2FloodDomainList(false);
235 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "allowExternalPopVlan", match), tableId, 200, match,
236 instructionsBuilder.build()).build();
238 flows.popVlanTagsOnExternalPortFlows((short) 0, connectorId, l2FloodDomains, 200, ofWriter);
239 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);
243 public void testAllowFromExternalPortFlow() {
244 NodeConnectorId connectorId = new NodeConnectorId(CONNECTOR_0);
246 MatchBuilder matchBuilder = new MatchBuilder();
247 matchBuilder.setInPort(connectorId);
248 Match match = matchBuilder.build();
250 Flow testFlow = buildFlow(FlowIdUtils.newFlowId(tableId, "allowExternal", match), tableId, 250, match,
251 FlowUtils.gotoTableInstructions((short) 2)).build();
252 flows.allowFromExternalPortFlow((short) 2, connectorId, 250, ofWriter);
253 verify(ofWriter, times(1)).writeFlow(NODE_ID, tableId, testFlow);