2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
\r
11 import java.util.Collections;
\r
12 import java.util.HashMap;
\r
13 import java.util.List;
\r
14 import java.util.Map;
\r
15 import java.util.Objects;
\r
16 import java.util.Set;
\r
18 import org.junit.Before;
\r
19 import org.junit.Test;
\r
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
\r
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
\r
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
\r
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
\r
34 import org.slf4j.Logger;
\r
35 import org.slf4j.LoggerFactory;
\r
37 import com.google.common.collect.ImmutableList;
\r
38 import com.google.common.collect.ImmutableSet;
\r
40 import static org.junit.Assert.*;
\r
42 public class PortSecurityTest extends FlowTableTest {
\r
43 protected static final Logger LOG =
\r
44 LoggerFactory.getLogger(PortSecurityTest.class);
\r
48 public void setup() throws Exception {
\r
50 table = new PortSecurity(ctx);
\r
55 public void testDefaultDeny() throws Exception {
\r
56 FlowMap fm = dosync(null);
\r
58 Map<String, Flow> flowMap = new HashMap<>();
\r
59 for (Flow f : fm.getTableForNode(nodeId, (short) 0).getFlow()) {
\r
60 flowMap.put(f.getId().getValue(), f);
\r
61 Long etherType = null;
\r
62 if (f.getMatch() != null) {
\r
63 etherType = f.getMatch().getEthernetMatch().getEthernetType().getType().getValue();
\r
65 if (f.getMatch() == null || FlowUtils.ARP.equals(etherType) || FlowUtils.IPv4.equals(etherType)
\r
66 || FlowUtils.IPv6.equals(etherType)) {
\r
68 assertEquals(FlowUtils.dropInstructions(), f.getInstructions());
\r
71 assertEquals(4, count);
\r
72 int numberOfFlows = fm.getTableForNode(nodeId, (short) 0).getFlow().size();
\r
73 fm = dosync(flowMap);
\r
74 assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
78 public void testNonLocalAllow() throws Exception {
\r
80 .addSwitch(new NodeId("openflow:1"),
\r
81 new NodeConnectorId("openflow:1:1"),
\r
82 ImmutableSet.of(new NodeConnectorId("openflow:1:2")),
\r
84 FlowMap fm = dosync(null);
\r
85 assertNotEquals(0 ,fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
88 HashMap<String, Flow> flowMap = new HashMap<>();
\r
89 Set<String> ncs = ImmutableSet.of("openflow:1:1", "openflow:1:2");
\r
90 for (Flow f : fm.getTableForNode(nodeId, (short) 0).getFlow()) {
\r
91 flowMap.put(f.getId().getValue(), f);
\r
92 if (f.getMatch() != null && f.getMatch().getInPort() != null &&
\r
93 ncs.contains(f.getMatch().getInPort().getValue())) {
\r
94 assertEquals(f.getInstructions(),
\r
95 FlowUtils.gotoTableInstructions((short)(table.getTableId()+1)));
\r
99 assertEquals(2, count);
\r
100 int numberOfFlows = fm.getTableForNode(nodeId, (short) 0).getFlow().size();
\r
101 fm = dosync(flowMap);
\r
102 assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
106 public void testL2() throws Exception {
\r
107 List<L3Address> l3 = Collections.emptyList();
\r
108 Endpoint ep = localEP()
\r
112 endpointManager.addEndpoint(ep);
\r
114 FlowMap fm = dosync(null);
\r
115 assertNotEquals(0 ,fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
118 HashMap<String, Flow> flowMap = new HashMap<>();
\r
119 for (Flow f : fm.getTableForNode(nodeId, (short) 0).getFlow()) {
\r
120 flowMap.put(f.getId().getValue(), f);
\r
121 if (f.getMatch() != null &&
\r
122 f.getMatch().getEthernetMatch() != null &&
\r
123 f.getMatch().getEthernetMatch().getEthernetSource() != null &&
\r
124 Objects.equals(ep.getMacAddress(),
\r
125 f.getMatch().getEthernetMatch()
\r
126 .getEthernetSource().getAddress()) &&
\r
127 Objects.equals(ep.getAugmentation(OfOverlayContext.class).getNodeConnectorId(),
\r
128 f.getMatch().getInPort())) {
\r
130 assertEquals(FlowUtils.gotoTableInstructions((short)(table.getTableId()+1)),
\r
131 f.getInstructions());
\r
134 assertEquals(2, count);
\r
135 int numberOfFlows = fm.getTableForNode(nodeId, (short) 0).getFlow().size();
\r
136 fm = dosync(flowMap);
\r
137 assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
141 public void testL3() throws Exception {
\r
142 Endpoint ep = localEP()
\r
143 .setL3Address(ImmutableList.of(new L3AddressBuilder()
\r
144 .setIpAddress(new IpAddress(new Ipv4Address("10.10.10.10")))
\r
146 new L3AddressBuilder()
\r
147 .setIpAddress(new IpAddress(new Ipv6Address("2001:db8:85a3::8a2e:370:7334")))
\r
151 endpointManager.addEndpoint(ep);
\r
153 FlowMap fm = dosync(null);
\r
154 assertNotEquals(0 ,fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r
157 HashMap<String, Flow> flowMap = new HashMap<>();
\r
158 for (Flow f : fm.getTableForNode(nodeId, (short) 0).getFlow()) {
\r
159 flowMap.put(f.getId().getValue(), f);
\r
160 if (f.getMatch() != null &&
\r
161 Objects.equals(ep.getAugmentation(OfOverlayContext.class).getNodeConnectorId(),
\r
162 f.getMatch().getInPort()) &&
\r
163 ((f.getMatch().getLayer3Match() != null &&
\r
164 f.getMatch().getLayer3Match() instanceof Ipv4Match &&
\r
165 ((Ipv4Match)f.getMatch().getLayer3Match()).getIpv4Source() != null &&
\r
166 Objects.equals(ep.getL3Address().get(0).getIpAddress().getIpv4Address().getValue(),
\r
167 ((Ipv4Match)f.getMatch().getLayer3Match()).getIpv4Source().getValue().split("/")[0])) ||
\r
168 (f.getMatch().getLayer3Match() != null &&
\r
169 f.getMatch().getLayer3Match() instanceof Ipv4Match &&
\r
170 ((Ipv4Match)f.getMatch().getLayer3Match()).getIpv4Destination() != null &&
\r
171 Objects.equals("255.255.255.255",
\r
172 ((Ipv4Match)f.getMatch().getLayer3Match()).getIpv4Destination().getValue().split("/")[0])) ||
\r
173 (f.getMatch().getLayer3Match() != null &&
\r
174 f.getMatch().getLayer3Match() instanceof ArpMatch &&
\r
175 Objects.equals(ep.getL3Address().get(0).getIpAddress().getIpv4Address().getValue(),
\r
176 ((ArpMatch)f.getMatch().getLayer3Match()).getArpSourceTransportAddress().getValue().split("/")[0])) ||
\r
177 (f.getMatch().getLayer3Match() != null &&
\r
178 f.getMatch().getLayer3Match() instanceof Ipv6Match &&
\r
179 Objects.equals(ep.getL3Address().get(1).getIpAddress().getIpv6Address().getValue(),
\r
180 ((Ipv6Match)f.getMatch().getLayer3Match()).getIpv6Source().getValue().split("/")[0])))) {
\r
182 assertEquals(FlowUtils.gotoTableInstructions((short)(table.getTableId()+1)),
\r
183 f.getInstructions());
\r
186 assertEquals(4, count);
\r
187 int numberOfFlows = fm.getTableForNode(nodeId, (short) 0).getFlow().size();
\r
188 fm = dosync(flowMap);
\r
189 assertEquals(numberOfFlows, fm.getTableForNode(nodeId, (short) 0).getFlow().size());
\r