Committed by
ShashikanthVH-Huawei
[ONOS-3857] BGP flow specification prefix implementation.
Change-Id: I49228f85f8efd5e972092df798a42ffc316d784d
Showing
2 changed files
with
183 additions
and
0 deletions
protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefix.java
0 → 100755
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 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. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.bgpio.protocol.flowspec; | ||
| 18 | + | ||
| 19 | +import java.util.Objects; | ||
| 20 | +import org.onlab.packet.IpPrefix; | ||
| 21 | +import org.slf4j.Logger; | ||
| 22 | +import org.slf4j.LoggerFactory; | ||
| 23 | +import java.nio.ByteBuffer; | ||
| 24 | + | ||
| 25 | +import com.google.common.base.MoreObjects; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * Provides BGP flow specification rule index. | ||
| 29 | + */ | ||
| 30 | +public class BgpFlowSpecPrefix implements Comparable<Object> { | ||
| 31 | + | ||
| 32 | + private static final Logger log = LoggerFactory.getLogger(BgpFlowSpecPrefix.class); | ||
| 33 | + | ||
| 34 | + private final IpPrefix destinationPrefix; | ||
| 35 | + private final IpPrefix sourcePrefix; | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Constructor to initialize parameters. | ||
| 39 | + * | ||
| 40 | + * @param destinationPrefix destination prefix | ||
| 41 | + * @param sourcePrefix source prefix | ||
| 42 | + */ | ||
| 43 | + public BgpFlowSpecPrefix(IpPrefix destinationPrefix, IpPrefix sourcePrefix) { | ||
| 44 | + this.destinationPrefix = destinationPrefix; | ||
| 45 | + this.sourcePrefix = sourcePrefix; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public int hashCode() { | ||
| 50 | + return Objects.hash(destinationPrefix, sourcePrefix); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Override | ||
| 54 | + public boolean equals(Object obj) { | ||
| 55 | + if (this == obj) { | ||
| 56 | + return true; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + if (obj instanceof BgpFlowSpecPrefix) { | ||
| 60 | + BgpFlowSpecPrefix other = (BgpFlowSpecPrefix) obj; | ||
| 61 | + | ||
| 62 | + if (this.destinationPrefix.equals(other.destinationPrefix)) { | ||
| 63 | + return this.sourcePrefix.equals(other.sourcePrefix); | ||
| 64 | + } | ||
| 65 | + return false; | ||
| 66 | + } | ||
| 67 | + return false; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * Returns destination prefix. | ||
| 72 | + * | ||
| 73 | + * @return destination prefix | ||
| 74 | + */ | ||
| 75 | + public IpPrefix destinationPrefix() { | ||
| 76 | + return this.destinationPrefix; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Returns source prefix. | ||
| 81 | + * | ||
| 82 | + * @return source prefix | ||
| 83 | + */ | ||
| 84 | + public IpPrefix sourcePrefix() { | ||
| 85 | + return this.sourcePrefix; | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + @Override | ||
| 89 | + public String toString() { | ||
| 90 | + return MoreObjects.toStringHelper(getClass()).omitNullValues() | ||
| 91 | + .add("destinationPrefix", destinationPrefix) | ||
| 92 | + .add("sourcePrefix", destinationPrefix) | ||
| 93 | + .toString(); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Compares this and o object. | ||
| 98 | + * | ||
| 99 | + * @param o object to be compared with this object | ||
| 100 | + * @return which object is greater | ||
| 101 | + */ | ||
| 102 | + public int compareTo(Object o) { | ||
| 103 | + if (this.equals(o)) { | ||
| 104 | + return 0; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + if (o instanceof BgpFlowSpecPrefix) { | ||
| 108 | + BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o; | ||
| 109 | + | ||
| 110 | + if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) { | ||
| 111 | + ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets()); | ||
| 112 | + ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets()); | ||
| 113 | + int cmpVal = value1.compareTo(value2); | ||
| 114 | + if (cmpVal != 0) { | ||
| 115 | + return cmpVal; | ||
| 116 | + } | ||
| 117 | + } else { | ||
| 118 | + if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) { | ||
| 119 | + return 1; | ||
| 120 | + } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) { | ||
| 121 | + return -1; | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) { | ||
| 126 | + ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets()); | ||
| 127 | + ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets()); | ||
| 128 | + return value1.compareTo(value2); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) { | ||
| 132 | + return 1; | ||
| 133 | + } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) { | ||
| 134 | + return -1; | ||
| 135 | + } | ||
| 136 | + } | ||
| 137 | + return 1; | ||
| 138 | + } | ||
| 139 | +} |
protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefixTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 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 | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 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. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.bgpio.protocol.flowspec; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | + | ||
| 20 | +import org.onlab.packet.IpPrefix; | ||
| 21 | + | ||
| 22 | +import com.google.common.testing.EqualsTester; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Test for BgpFsDestinationPrefix flow specification component. | ||
| 26 | + */ | ||
| 27 | +public class BgpFlowSpecPrefixTest { | ||
| 28 | + private IpPrefix destinationPrefix1 = IpPrefix.valueOf("21.21.21.21/16"); | ||
| 29 | + private IpPrefix sourcePrefix1 = IpPrefix.valueOf("11.11.11.11/16"); | ||
| 30 | + private IpPrefix destinationPrefix2 = IpPrefix.valueOf("42.42.42.42/16"); | ||
| 31 | + private IpPrefix sourcePrefix2 = IpPrefix.valueOf("32.32.32.32/16"); | ||
| 32 | + | ||
| 33 | + private final BgpFlowSpecPrefix tlv1 = new BgpFlowSpecPrefix(destinationPrefix1, sourcePrefix1); | ||
| 34 | + private final BgpFlowSpecPrefix sameAsTlv1 = new BgpFlowSpecPrefix(destinationPrefix1, sourcePrefix1); | ||
| 35 | + private final BgpFlowSpecPrefix tlv2 = new BgpFlowSpecPrefix(destinationPrefix2, sourcePrefix2); | ||
| 36 | + | ||
| 37 | + @Test | ||
| 38 | + public void testEquality() { | ||
| 39 | + new EqualsTester() | ||
| 40 | + .addEqualityGroup(tlv1, sameAsTlv1) | ||
| 41 | + .addEqualityGroup(tlv2) | ||
| 42 | + .testEquals(); | ||
| 43 | + } | ||
| 44 | +} |
-
Please register or login to post a comment