2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.v3po.interfaces.acl.common;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 import java.util.function.BiFunction;
23 import javax.annotation.Nullable;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.DestinationPortRange;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160708.acl.transport.header.fields.SourcePortRange;
29 * Utility that produces cartesian product out of src and dst port ranges (used to translate ranges into
30 * list of classify sessions).
32 final class PortPair {
33 private final Integer src;
34 private final Integer dst;
36 PortPair(@Nullable final Integer src, @Nullable final Integer dst) {
50 public String toString() {
51 return "(" + src + "," + dst + ")";
55 public boolean equals(final Object o) {
59 if (o == null || getClass() != o.getClass()) {
63 final PortPair that = (PortPair) o;
64 if (!Objects.equals(src, that.src)) {
67 if (!Objects.equals(dst, that.dst)) {
74 public int hashCode() {
75 return Objects.hash(src, dst);
78 static List<PortPair> fromRange(final SourcePortRange srcRange,
79 final DestinationPortRange dstRange) {
80 final List<PortPair> result = new ArrayList<>();
81 if (srcRange == null && dstRange == null) {
82 result.add(new PortPair(null, null));
83 } else if (srcRange != null && dstRange == null) {
84 processSingleRange(result, srcRange.getLowerPort(), srcRange.getUpperPort(), PortPair::new);
85 } else if (srcRange == null && dstRange != null) {
86 processSingleRange(result, dstRange.getLowerPort(), dstRange.getUpperPort(),
87 (dst, src) -> new PortPair(src, dst));
89 processDoubleRange(result, srcRange, dstRange);
94 private static void processSingleRange(final List<PortPair> result,
95 final PortNumber lowerPort,
96 final PortNumber upperPort,
97 final BiFunction<Integer, Integer, PortPair> f) {
98 int low = lowerPort.getValue(); // mandatory
100 if (upperPort != null) {
101 hi = upperPort.getValue();
103 for (; low <= hi; ++low) {
104 result.add(f.apply(low, null));
108 private static void processDoubleRange(final List<PortPair> result, final SourcePortRange srcRange,
109 final DestinationPortRange dstRange) {
110 int srcL = srcRange.getLowerPort().getValue();
112 if (srcRange.getUpperPort() != null) {
113 srcH = srcRange.getUpperPort().getValue();
115 int dstL = dstRange.getLowerPort().getValue();
117 if (dstRange.getUpperPort() != null) {
118 dstH = dstRange.getUpperPort().getValue();
120 for (int i=srcL; i <= srcH; ++i) {
121 for (int j=dstL; j <= dstH; ++j) {
122 result.add(new PortPair(i, j));