2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl;
11 import java.util.List;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRangeBuilder;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRangeBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
20 abstract class AddressMapper {
22 private AccessListUtil.ACE_DIRECTION direction;
24 private static final PortNumber DHCP_67 = new PortNumber(67);
25 private static final PortNumber DHCP_68 = new PortNumber(68);
26 private static final PortNumber DHCPV6_547 = new PortNumber(547);
27 private static final PortNumber DHCPV6_548 = new PortNumber(548);
29 AddressMapper(AccessListUtil.ACE_DIRECTION direction) {
30 this.direction = direction;
33 abstract void updateRule(AddressEndpointWithLocation addrEp, GbpAceBuilder aclRuleBuilder);
35 //TODO implement for peers with no location
36 abstract List<GbpAceBuilder> updateExtRules(List<GbpAceBuilder> rules, AddressEndpointWithLocation localEp,
37 ContainmentEndpoint cEp);
39 public List<GbpAceBuilder> updateRules(List<GbpAceBuilder> rules, AddressEndpointWithLocation localEp,
40 AddressEndpointWithLocation peerEp) {
41 if (this instanceof SourceMapper) {
42 if (AccessListUtil.ACE_DIRECTION.INGRESS.equals(direction)) {
43 return updateRules(rules, localEp);
45 return updateRules(rules, peerEp);
47 if (this instanceof DestinationMapper) {
48 if (AccessListUtil.ACE_DIRECTION.INGRESS.equals(direction)) {
49 return updateRules(rules, peerEp);
51 return updateRules(rules, localEp);
56 private List<GbpAceBuilder> updateRules(List<GbpAceBuilder> rules, AddressEndpointWithLocation addrEp) {
57 for (GbpAceBuilder rule : rules) {
58 if (isInRange(rule.getSourcePortRangeBuilder(), DHCP_67)
59 && isInRange(rule.getDestinationPortRangeBuilder(), DHCP_68)) {
62 if (isInRange(rule.getSourcePortRangeBuilder(), DHCP_68)
63 && isInRange(rule.getDestinationPortRangeBuilder(), DHCP_67)) {
66 if (isInRange(rule.getSourcePortRangeBuilder(), DHCPV6_547)
67 && isInRange(rule.getDestinationPortRangeBuilder(), DHCPV6_548)) {
70 if (isInRange(rule.getSourcePortRangeBuilder(), DHCPV6_548)
71 && isInRange(rule.getDestinationPortRangeBuilder(), DHCPV6_547)) {
74 updateRule(addrEp, rule);
79 private boolean isInRange(SourcePortRangeBuilder spr, PortNumber portNumber) {
80 return spr != null && isInRange(spr.getLowerPort(), spr.getUpperPort(), portNumber);
83 private boolean isInRange(DestinationPortRangeBuilder dpr, PortNumber portNumber) {
84 return dpr != null && isInRange(dpr.getLowerPort(),dpr.getUpperPort(), portNumber);
87 private boolean isInRange(PortNumber lower, PortNumber upper, PortNumber ref) {
88 if (lower != null && upper != null) {
89 return (lower.getValue() <= ref.getValue()) && (ref.getValue() <= upper.getValue());
91 return (lower != null && lower.getValue().equals(ref.getValue()))
92 || (upper != null && upper.getValue().equals(ref.getValue()));