Shashikanth VH
Committed by Gerrit Code Review

[ONOS-4241]Codec for wide community and flow spec

Change-Id: I2dfd6b88c4ae14a02d258a2cdc9ee35b9ff08292
Showing 13 changed files with 257 additions and 396 deletions
...@@ -19,9 +19,10 @@ import org.jboss.netty.channel.Channel; ...@@ -19,9 +19,10 @@ import org.jboss.netty.channel.Channel;
19 import org.onosproject.bgpio.exceptions.BgpParseException; 19 import org.onosproject.bgpio.exceptions.BgpParseException;
20 import org.onosproject.bgpio.protocol.BgpFactory; 20 import org.onosproject.bgpio.protocol.BgpFactory;
21 import org.onosproject.bgpio.protocol.BgpMessage; 21 import org.onosproject.bgpio.protocol.BgpMessage;
22 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails; 22 +import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri;
23 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix; 23 +import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecRouteKey;
24 import org.onosproject.bgpio.types.BgpValueType; 24 import org.onosproject.bgpio.types.BgpValueType;
25 +import org.onosproject.bgpio.types.attr.WideCommunity;
25 26
26 /** 27 /**
27 * Represents the peer side of an BGP peer. 28 * Represents the peer side of an BGP peer.
...@@ -124,14 +125,6 @@ public interface BgpPeer { ...@@ -124,14 +125,6 @@ public interface BgpPeer {
124 void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException; 125 void buildAdjRibIn(List<BgpValueType> pathAttr) throws BgpParseException;
125 126
126 /** 127 /**
127 - * Update flow specification RIB for each peer.
128 - *
129 - * @param pathAttr list of Bgp path attributes
130 - * @throws BgpParseException while building flow spec RIB
131 - */
132 - void buildFlowSpecRib(List<BgpValueType> pathAttr) throws BgpParseException;
133 -
134 - /**
135 * Return the BGP session info. 128 * Return the BGP session info.
136 * 129 *
137 * @return sessionInfo bgp session info 130 * @return sessionInfo bgp session info
...@@ -142,9 +135,10 @@ public interface BgpPeer { ...@@ -142,9 +135,10 @@ public interface BgpPeer {
142 * Updates flow specification rule. 135 * Updates flow specification rule.
143 * 136 *
144 * @param operType operation type add or delete or update 137 * @param operType operation type add or delete or update
145 - * @param prefix prefix for the flow rule 138 + * @param routeKey flow route key for the flow rule
146 * @param flowSpec BGP flow specification components 139 * @param flowSpec BGP flow specification components
140 + * @param wideCommunity for route policy
147 */ 141 */
148 - void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecPrefix prefix, 142 + void updateFlowSpec(FlowSpecOperation operType, BgpFlowSpecRouteKey routeKey,
149 - BgpFlowSpecDetails flowSpec); 143 + BgpFlowSpecNlri flowSpec, WideCommunity wideCommunity);
150 } 144 }
......
...@@ -17,25 +17,44 @@ package org.onosproject.bgpio.protocol.flowspec; ...@@ -17,25 +17,44 @@ package org.onosproject.bgpio.protocol.flowspec;
17 17
18 import java.util.Iterator; 18 import java.util.Iterator;
19 import java.util.List; 19 import java.util.List;
20 +import java.util.ListIterator;
20 import java.util.Objects; 21 import java.util.Objects;
22 +
23 +import org.jboss.netty.buffer.ChannelBuffer;
24 +import org.jboss.netty.buffer.ChannelBuffers;
25 +import org.onosproject.bgpio.types.BgpFsDestinationPortNum;
26 +import org.onosproject.bgpio.types.BgpFsDestinationPrefix;
27 +import org.onosproject.bgpio.types.BgpFsDscpValue;
28 +import org.onosproject.bgpio.types.BgpFsFragment;
29 +import org.onosproject.bgpio.types.BgpFsIcmpCode;
30 +import org.onosproject.bgpio.types.BgpFsIcmpType;
31 +import org.onosproject.bgpio.types.BgpFsIpProtocol;
32 +import org.onosproject.bgpio.types.BgpFsPacketLength;
33 +import org.onosproject.bgpio.types.BgpFsPortNum;
34 +import org.onosproject.bgpio.types.BgpFsSourcePortNum;
35 +import org.onosproject.bgpio.types.BgpFsSourcePrefix;
36 +import org.onosproject.bgpio.types.BgpFsTcpFlags;
21 import org.onosproject.bgpio.types.BgpValueType; 37 import org.onosproject.bgpio.types.BgpValueType;
22 import org.onosproject.bgpio.types.RouteDistinguisher; 38 import org.onosproject.bgpio.types.RouteDistinguisher;
39 +import org.onosproject.bgpio.util.Constants;
40 +
23 import com.google.common.base.MoreObjects; 41 import com.google.common.base.MoreObjects;
24 42
25 /** 43 /**
26 * This Class stores flow specification components and action. 44 * This Class stores flow specification components and action.
27 */ 45 */
28 -public class BgpFlowSpecDetails { 46 +public class BgpFlowSpecNlri {
29 private List<BgpValueType> flowSpecComponents; 47 private List<BgpValueType> flowSpecComponents;
30 private List<BgpValueType> fsActionTlv; 48 private List<BgpValueType> fsActionTlv;
31 private RouteDistinguisher routeDistinguisher; 49 private RouteDistinguisher routeDistinguisher;
50 + public static final short FLOW_SPEC_LEN = 240;
32 51
33 /** 52 /**
34 * Flow specification details object constructor with the parameter. 53 * Flow specification details object constructor with the parameter.
35 * 54 *
36 * @param flowSpecComponents flow specification components 55 * @param flowSpecComponents flow specification components
37 */ 56 */
38 - public BgpFlowSpecDetails(List<BgpValueType> flowSpecComponents) { 57 + public BgpFlowSpecNlri(List<BgpValueType> flowSpecComponents) {
39 this.flowSpecComponents = flowSpecComponents; 58 this.flowSpecComponents = flowSpecComponents;
40 } 59 }
41 60
...@@ -43,7 +62,7 @@ public class BgpFlowSpecDetails { ...@@ -43,7 +62,7 @@ public class BgpFlowSpecDetails {
43 * Flow specification details object constructor. 62 * Flow specification details object constructor.
44 * 63 *
45 */ 64 */
46 - public BgpFlowSpecDetails() { 65 + public BgpFlowSpecNlri() {
47 66
48 } 67 }
49 68
...@@ -112,11 +131,11 @@ public class BgpFlowSpecDetails { ...@@ -112,11 +131,11 @@ public class BgpFlowSpecDetails {
112 return true; 131 return true;
113 } 132 }
114 133
115 - if (obj instanceof BgpFlowSpecDetails) { 134 + if (obj instanceof BgpFlowSpecNlri) {
116 int countObjSubTlv = 0; 135 int countObjSubTlv = 0;
117 int countOtherSubTlv = 0; 136 int countOtherSubTlv = 0;
118 boolean isCommonSubTlv = true; 137 boolean isCommonSubTlv = true;
119 - BgpFlowSpecDetails other = (BgpFlowSpecDetails) obj; 138 + BgpFlowSpecNlri other = (BgpFlowSpecNlri) obj;
120 Iterator<BgpValueType> objListIterator = other.flowSpecComponents.iterator(); 139 Iterator<BgpValueType> objListIterator = other.flowSpecComponents.iterator();
121 countOtherSubTlv = other.flowSpecComponents.size(); 140 countOtherSubTlv = other.flowSpecComponents.size();
122 countObjSubTlv = flowSpecComponents.size(); 141 countObjSubTlv = flowSpecComponents.size();
...@@ -138,6 +157,133 @@ public class BgpFlowSpecDetails { ...@@ -138,6 +157,133 @@ public class BgpFlowSpecDetails {
138 return false; 157 return false;
139 } 158 }
140 159
160 + /**
161 + * Write flow type to channel buffer.
162 + *
163 + * @param tlv flow type
164 + * @param cb channel buffer
165 + */
166 + public static void writeFlowType(BgpValueType tlv, ChannelBuffer cb) {
167 +
168 + switch (tlv.getType()) {
169 + case Constants.BGP_FLOWSPEC_DST_PREFIX:
170 + BgpFsDestinationPrefix fsDstPrefix = (BgpFsDestinationPrefix) tlv;
171 + fsDstPrefix.write(cb);
172 + break;
173 + case Constants.BGP_FLOWSPEC_SRC_PREFIX:
174 + BgpFsSourcePrefix fsSrcPrefix = (BgpFsSourcePrefix) tlv;
175 + fsSrcPrefix.write(cb);
176 + break;
177 + case Constants.BGP_FLOWSPEC_IP_PROTO:
178 + BgpFsIpProtocol fsIpProtocol = (BgpFsIpProtocol) tlv;
179 + fsIpProtocol.write(cb);
180 + break;
181 + case Constants.BGP_FLOWSPEC_PORT:
182 + BgpFsPortNum fsPortNum = (BgpFsPortNum) tlv;
183 + fsPortNum.write(cb);
184 + break;
185 + case Constants.BGP_FLOWSPEC_DST_PORT:
186 + BgpFsDestinationPortNum fsDstPortNum = (BgpFsDestinationPortNum) tlv;
187 + fsDstPortNum.write(cb);
188 + break;
189 + case Constants.BGP_FLOWSPEC_SRC_PORT:
190 + BgpFsSourcePortNum fsSrcPortNum = (BgpFsSourcePortNum) tlv;
191 + fsSrcPortNum.write(cb);
192 + break;
193 + case Constants.BGP_FLOWSPEC_ICMP_TP:
194 + BgpFsIcmpType fsIcmpType = (BgpFsIcmpType) tlv;
195 + fsIcmpType.write(cb);
196 + break;
197 + case Constants.BGP_FLOWSPEC_ICMP_CD:
198 + BgpFsIcmpCode fsIcmpCode = (BgpFsIcmpCode) tlv;
199 + fsIcmpCode.write(cb);
200 + break;
201 + case Constants.BGP_FLOWSPEC_TCP_FLAGS:
202 + BgpFsTcpFlags fsTcpFlags = (BgpFsTcpFlags) tlv;
203 + fsTcpFlags.write(cb);
204 + break;
205 + case Constants.BGP_FLOWSPEC_PCK_LEN:
206 + BgpFsPacketLength fsPacketLen = (BgpFsPacketLength) tlv;
207 + fsPacketLen.write(cb);
208 + break;
209 + case Constants.BGP_FLOWSPEC_DSCP:
210 + BgpFsDscpValue fsDscpVal = (BgpFsDscpValue) tlv;
211 + fsDscpVal.write(cb);
212 + break;
213 + case Constants.BGP_FLOWSPEC_FRAGMENT:
214 + BgpFsFragment fsFragment = (BgpFsFragment) tlv;
215 + fsFragment.write(cb);
216 + break;
217 + default:
218 + break;
219 + }
220 + return;
221 + }
222 +
223 + /**
224 + * Update buffer with identical flow types.
225 + *
226 + * @param cb channel buffer
227 + */
228 + public static void updateBufferIdenticalFlowTypes(ChannelBuffer cb, BgpFlowSpecNlri bgpFlowSpecNlri) {
229 +
230 + List<BgpValueType> flowSpec = bgpFlowSpecNlri.flowSpecComponents();
231 + ListIterator<BgpValueType> listIterator = flowSpec.listIterator();
232 +
233 + while (listIterator.hasNext()) {
234 + ChannelBuffer flowSpecTmpBuff = ChannelBuffers.dynamicBuffer();
235 + int tmpBuffStartIndx = flowSpecTmpBuff.writerIndex();
236 +
237 + BgpValueType tlv = listIterator.next();
238 + writeFlowType(tlv, flowSpecTmpBuff);
239 +
240 + /* RFC 5575: section 4, If the NLRI length value is smaller than 240 (0xf0 hex), the length
241 + field can be encoded as a single octet. Otherwise, it is encoded as
242 + an extended-length 2-octet values */
243 + int len = flowSpecTmpBuff.writerIndex() - tmpBuffStartIndx;
244 + if (len >= FLOW_SPEC_LEN) {
245 + cb.writeShort(len);
246 + } else {
247 + cb.writeByte(len);
248 + }
249 + //Copy from bynamic buffer to channel buffer
250 + cb.writeBytes(flowSpecTmpBuff);
251 + }
252 + return;
253 + }
254 +
255 + /**
256 + * Update buffer with non-identical flow types.
257 + *
258 + * @param cb channel buffer
259 + */
260 + public static void updateBufferNonIdenticalFlowTypes(ChannelBuffer cb, BgpFlowSpecNlri bgpFlowSpecNlri) {
261 + ChannelBuffer flowSpecTmpBuff = ChannelBuffers.dynamicBuffer();
262 + List<BgpValueType> flowSpec = bgpFlowSpecNlri.flowSpecComponents();
263 + ListIterator<BgpValueType> listIterator = flowSpec.listIterator();
264 + int tmpBuffStartIndx = flowSpecTmpBuff.writerIndex();
265 +
266 + flowSpec = bgpFlowSpecNlri.flowSpecComponents();
267 + listIterator = flowSpec.listIterator();
268 +
269 + while (listIterator.hasNext()) {
270 + BgpValueType tlv = listIterator.next();
271 + writeFlowType(tlv, flowSpecTmpBuff);
272 + }
273 +
274 + /* RFC 5575: section 4, If the NLRI length value is smaller than 240 (0xf0 hex), the length
275 + field can be encoded as a single octet. Otherwise, it is encoded as
276 + an extended-length 2-octet values */
277 + int len = flowSpecTmpBuff.writerIndex() - tmpBuffStartIndx;
278 + if (len >= FLOW_SPEC_LEN) {
279 + cb.writeShort(len);
280 + } else {
281 + cb.writeByte(len);
282 + }
283 + //Copy from bynamic buffer to channel buffer
284 + cb.writeBytes(flowSpecTmpBuff);
285 + }
286 +
141 @Override 287 @Override
142 public String toString() { 288 public String toString() {
143 return MoreObjects.toStringHelper(getClass()) 289 return MoreObjects.toStringHelper(getClass())
......
...@@ -17,45 +17,41 @@ ...@@ -17,45 +17,41 @@
17 package org.onosproject.bgpio.protocol.flowspec; 17 package org.onosproject.bgpio.protocol.flowspec;
18 18
19 import java.util.Objects; 19 import java.util.Objects;
20 -import org.onlab.packet.IpPrefix;
21 import org.slf4j.Logger; 20 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory; 21 import org.slf4j.LoggerFactory;
23 -import java.nio.ByteBuffer;
24 22
25 import com.google.common.base.MoreObjects; 23 import com.google.common.base.MoreObjects;
26 24
27 /** 25 /**
28 - * Provides BGP flow specification rule index. 26 + * Provides BGP flow specification route index.
29 */ 27 */
30 -public class BgpFlowSpecPrefix implements Comparable<Object> { 28 +public class BgpFlowSpecRouteKey implements Comparable<Object> {
31 29
32 - private static final Logger log = LoggerFactory.getLogger(BgpFlowSpecPrefix.class); 30 + private static final Logger log = LoggerFactory.getLogger(BgpFlowSpecRouteKey.class);
33 31
34 - private final IpPrefix destinationPrefix; 32 + private final String routeKey;
35 - private final IpPrefix sourcePrefix;
36 33
37 /** 34 /**
38 * Constructor to initialize parameters. 35 * Constructor to initialize parameters.
39 * 36 *
40 - * @param destinationPrefix destination prefix 37 + * @param routeKey route key
41 - * @param sourcePrefix source prefix
42 */ 38 */
43 - public BgpFlowSpecPrefix(IpPrefix destinationPrefix, IpPrefix sourcePrefix) { 39 + public BgpFlowSpecRouteKey(String routeKey) {
44 - if (destinationPrefix == null) { 40 + this.routeKey = routeKey;
45 - destinationPrefix = IpPrefix.valueOf(0, 0); 41 + }
46 - }
47 -
48 - if (sourcePrefix == null) {
49 - sourcePrefix = IpPrefix.valueOf(0, 0);
50 - }
51 42
52 - this.destinationPrefix = destinationPrefix; 43 + /**
53 - this.sourcePrefix = sourcePrefix; 44 + * Returns route key.
45 + *
46 + * @return route key
47 + */
48 + public String routeKey() {
49 + return this.routeKey;
54 } 50 }
55 51
56 @Override 52 @Override
57 public int hashCode() { 53 public int hashCode() {
58 - return Objects.hash(destinationPrefix, sourcePrefix); 54 + return Objects.hashCode(routeKey);
59 } 55 }
60 56
61 @Override 57 @Override
...@@ -63,93 +59,33 @@ public class BgpFlowSpecPrefix implements Comparable<Object> { ...@@ -63,93 +59,33 @@ public class BgpFlowSpecPrefix implements Comparable<Object> {
63 if (this == obj) { 59 if (this == obj) {
64 return true; 60 return true;
65 } 61 }
66 - 62 + if (obj instanceof BgpFlowSpecRouteKey) {
67 - if (obj instanceof BgpFlowSpecPrefix) { 63 + BgpFlowSpecRouteKey other = (BgpFlowSpecRouteKey) obj;
68 - BgpFlowSpecPrefix other = (BgpFlowSpecPrefix) obj; 64 + return this.routeKey.equals(other.routeKey);
69 -
70 - if ((this.destinationPrefix != null) && (this.sourcePrefix != null)
71 - && (this.destinationPrefix.equals(other.destinationPrefix))) {
72 - return this.sourcePrefix.equals(other.sourcePrefix);
73 - } else if (this.destinationPrefix != null) {
74 - return this.destinationPrefix.equals(other.destinationPrefix);
75 - } else if (this.sourcePrefix != null) {
76 - return this.sourcePrefix.equals(other.sourcePrefix);
77 - }
78 - return false;
79 } 65 }
80 return false; 66 return false;
81 } 67 }
82 68
83 - /**
84 - * Returns destination prefix.
85 - *
86 - * @return destination prefix
87 - */
88 - public IpPrefix destinationPrefix() {
89 - return this.destinationPrefix;
90 - }
91 -
92 - /**
93 - * Returns source prefix.
94 - *
95 - * @return source prefix
96 - */
97 - public IpPrefix sourcePrefix() {
98 - return this.sourcePrefix;
99 - }
100 -
101 @Override 69 @Override
102 - public String toString() {
103 - return MoreObjects.toStringHelper(getClass()).omitNullValues()
104 - .add("destinationPrefix", destinationPrefix)
105 - .add("sourcePrefix", destinationPrefix)
106 - .toString();
107 - }
108 -
109 - /**
110 - * Compares this and o object.
111 - *
112 - * @param o object to be compared with this object
113 - * @return which object is greater
114 - */
115 public int compareTo(Object o) { 70 public int compareTo(Object o) {
116 if (this.equals(o)) { 71 if (this.equals(o)) {
117 return 0; 72 return 0;
118 } 73 }
119 74
120 - if (o instanceof BgpFlowSpecPrefix) { 75 + if (o instanceof BgpFlowSpecRouteKey) {
121 - BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o; 76 + BgpFlowSpecRouteKey other = (BgpFlowSpecRouteKey) o;
122 - if (this.destinationPrefix() != null) { 77 + if (this.routeKey.compareTo(other.routeKey) != 0) {
123 - if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) { 78 + return this.routeKey.compareTo(other.routeKey);
124 - ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
125 - ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
126 - int cmpVal = value1.compareTo(value2);
127 - if (cmpVal != 0) {
128 - return cmpVal;
129 - }
130 - } else {
131 - if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
132 - return 1;
133 - } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
134 - return -1;
135 - }
136 - }
137 - }
138 - if (this.sourcePrefix() != null) {
139 - if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
140 - ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
141 - ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
142 - return value1.compareTo(value2);
143 - }
144 -
145 - if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
146 - return 1;
147 - } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
148 - return -1;
149 - }
150 } 79 }
151 return 0; 80 return 0;
152 } 81 }
153 return 1; 82 return 1;
154 } 83 }
84 +
85 + @Override
86 + public String toString() {
87 + return MoreObjects.toStringHelper(getClass()).omitNullValues()
88 + .add("routeKey", routeKey)
89 + .toString();
90 + }
155 } 91 }
......
...@@ -64,6 +64,22 @@ public class RpdCapabilityTlv implements BgpValueType { ...@@ -64,6 +64,22 @@ public class RpdCapabilityTlv implements BgpValueType {
64 return sendReceive; 64 return sendReceive;
65 } 65 }
66 66
67 + /**
68 + * Returns address family identifier value.
69 + * @return afi address family identifier value
70 + */
71 + public short getAfi() {
72 + return afi;
73 + }
74 +
75 + /**
76 + * Returns subsequent address family identifier value.
77 + * @return safi subsequent address family identifier value
78 + */
79 + public byte getSafi() {
80 + return sAfi;
81 + }
82 +
67 @Override 83 @Override
68 public short getType() { 84 public short getType() {
69 return TYPE; 85 return TYPE;
......
...@@ -28,23 +28,23 @@ import org.onosproject.bgpio.types.BgpValueType; ...@@ -28,23 +28,23 @@ import org.onosproject.bgpio.types.BgpValueType;
28 import com.google.common.testing.EqualsTester; 28 import com.google.common.testing.EqualsTester;
29 29
30 /** 30 /**
31 - * Test for BgpFlowSpecDetails flow specification. 31 + * Test for BgpFlowSpecNlri flow specification.
32 */ 32 */
33 -public class BgpFlowSpecDetailsTest { 33 +public class BgpFlowSpecNlriTest {
34 34
35 List<BgpValueType> flowSpecComponents1 = new LinkedList<>(); 35 List<BgpValueType> flowSpecComponents1 = new LinkedList<>();
36 private List<BgpFsOperatorValue> operatorValue1 = new ArrayList<>(); 36 private List<BgpFsOperatorValue> operatorValue1 = new ArrayList<>();
37 - BgpFlowSpecDetails flowSpecDetails1 = new BgpFlowSpecDetails(flowSpecComponents1); 37 + BgpFlowSpecNlri flowSpecDetails1 = new BgpFlowSpecNlri(flowSpecComponents1);
38 BgpFsPortNum portNum1 = new BgpFsPortNum(operatorValue1); 38 BgpFsPortNum portNum1 = new BgpFsPortNum(operatorValue1);
39 39
40 List<BgpValueType> flowSpecComponents = new LinkedList<>(); 40 List<BgpValueType> flowSpecComponents = new LinkedList<>();
41 private List<BgpFsOperatorValue> operatorValue = new ArrayList<>(); 41 private List<BgpFsOperatorValue> operatorValue = new ArrayList<>();
42 - BgpFlowSpecDetails flowSpecDetails = new BgpFlowSpecDetails(flowSpecComponents); 42 + BgpFlowSpecNlri flowSpecDetails = new BgpFlowSpecNlri(flowSpecComponents);
43 BgpFsPortNum portNum = new BgpFsPortNum(operatorValue); 43 BgpFsPortNum portNum = new BgpFsPortNum(operatorValue);
44 44
45 List<BgpValueType> flowSpecComponents2 = new LinkedList<>(); 45 List<BgpValueType> flowSpecComponents2 = new LinkedList<>();
46 private List<BgpFsOperatorValue> operatorValue2 = new ArrayList<>(); 46 private List<BgpFsOperatorValue> operatorValue2 = new ArrayList<>();
47 - BgpFlowSpecDetails flowSpecDetails2 = new BgpFlowSpecDetails(flowSpecComponents2); 47 + BgpFlowSpecNlri flowSpecDetails2 = new BgpFlowSpecNlri(flowSpecComponents2);
48 BgpFsPortNum portNum2 = new BgpFsPortNum(operatorValue2); 48 BgpFsPortNum portNum2 = new BgpFsPortNum(operatorValue2);
49 49
50 @Test 50 @Test
......
...@@ -16,23 +16,18 @@ ...@@ -16,23 +16,18 @@
16 package org.onosproject.bgpio.protocol.flowspec; 16 package org.onosproject.bgpio.protocol.flowspec;
17 17
18 import org.junit.Test; 18 import org.junit.Test;
19 -
20 -import org.onlab.packet.IpPrefix;
21 -
22 import com.google.common.testing.EqualsTester; 19 import com.google.common.testing.EqualsTester;
23 20
24 /** 21 /**
25 * Test for BgpFsDestinationPrefix flow specification component. 22 * Test for BgpFsDestinationPrefix flow specification component.
26 */ 23 */
27 public class BgpFlowSpecPrefixTest { 24 public class BgpFlowSpecPrefixTest {
28 - private IpPrefix destinationPrefix1 = IpPrefix.valueOf("21.21.21.21/16"); 25 + private String routeKey1 = "flowRoute1";
29 - private IpPrefix sourcePrefix1 = IpPrefix.valueOf("11.11.11.11/16"); 26 + private String routeKey2 = "flowRoute2";
30 - private IpPrefix destinationPrefix2 = IpPrefix.valueOf("42.42.42.42/16");
31 - private IpPrefix sourcePrefix2 = IpPrefix.valueOf("32.32.32.32/16");
32 27
33 - private final BgpFlowSpecPrefix tlv1 = new BgpFlowSpecPrefix(destinationPrefix1, sourcePrefix1); 28 + private final BgpFlowSpecRouteKey tlv1 = new BgpFlowSpecRouteKey(routeKey1);
34 - private final BgpFlowSpecPrefix sameAsTlv1 = new BgpFlowSpecPrefix(destinationPrefix1, sourcePrefix1); 29 + private final BgpFlowSpecRouteKey sameAsTlv1 = new BgpFlowSpecRouteKey(routeKey1);
35 - private final BgpFlowSpecPrefix tlv2 = new BgpFlowSpecPrefix(destinationPrefix2, sourcePrefix2); 30 + private final BgpFlowSpecRouteKey tlv2 = new BgpFlowSpecRouteKey(routeKey2);
36 31
37 @Test 32 @Test
38 public void testEquality() { 33 public void testEquality() {
......
1 +/*
2 + * Copyright 2016-present 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 com.google.common.testing.EqualsTester;
21 +
22 +/**
23 + * Test for BgpFlowSpecRouteKeyTest flow specification component.
24 + */
25 +public class BgpFlowSpecRouteKeyTest {
26 + private static String routeKey1 = "flowRoute1";
27 + private static String routeKey2 = "flowRoute2";
28 +
29 + private final BgpFlowSpecRouteKey tlv1 = new BgpFlowSpecRouteKey(routeKey1);
30 + private final BgpFlowSpecRouteKey sameAsTlv1 = new BgpFlowSpecRouteKey(routeKey1);
31 + private final BgpFlowSpecRouteKey tlv2 = new BgpFlowSpecRouteKey(routeKey2);
32 +
33 + @Test
34 + public void testEquality() {
35 + new EqualsTester()
36 + .addEqualityGroup(tlv1, sameAsTlv1)
37 + .addEqualityGroup(tlv2)
38 + .testEquals();
39 + }
40 +}
...@@ -133,29 +133,23 @@ public class BgpControllerImpl implements BgpController { ...@@ -133,29 +133,23 @@ public class BgpControllerImpl implements BgpController {
133 } 133 }
134 Iterator<BgpValueType> listIterator = pathAttr.iterator(); 134 Iterator<BgpValueType> listIterator = pathAttr.iterator();
135 boolean isLinkstate = false; 135 boolean isLinkstate = false;
136 - boolean isFlowSpec = false; 136 +
137 while (listIterator.hasNext()) { 137 while (listIterator.hasNext()) {
138 BgpValueType attr = listIterator.next(); 138 BgpValueType attr = listIterator.next();
139 if (attr instanceof MpReachNlri) { 139 if (attr instanceof MpReachNlri) {
140 MpReachNlri mpReach = (MpReachNlri) attr; 140 MpReachNlri mpReach = (MpReachNlri) attr;
141 - if (mpReach.bgpFlowSpecInfo() == null) { 141 + if (mpReach.bgpFlowSpecNlri() == null) {
142 isLinkstate = true; 142 isLinkstate = true;
143 - } else {
144 - isFlowSpec = true;
145 } 143 }
146 } else if (attr instanceof MpUnReachNlri) { 144 } else if (attr instanceof MpUnReachNlri) {
147 MpUnReachNlri mpUnReach = (MpUnReachNlri) attr; 145 MpUnReachNlri mpUnReach = (MpUnReachNlri) attr;
148 - if (mpUnReach.bgpFlowSpecInfo() == null) { 146 + if (mpUnReach.bgpFlowSpecNlri() == null) {
149 isLinkstate = true; 147 isLinkstate = true;
150 - } else {
151 - isFlowSpec = true;
152 } 148 }
153 } 149 }
154 } 150 }
155 if (isLinkstate) { 151 if (isLinkstate) {
156 peer.buildAdjRibIn(pathAttr); 152 peer.buildAdjRibIn(pathAttr);
157 - } else if (isFlowSpec) {
158 - peer.buildFlowSpecRib(pathAttr);
159 } 153 }
160 break; 154 break;
161 default: 155 default:
......
1 -/*
2 - * Copyright 2016-present 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.bgp.controller.impl;
18 -
19 -import java.util.Map;
20 -import java.util.TreeMap;
21 -
22 -import org.onosproject.bgpio.types.RouteDistinguisher;
23 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
24 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix;
25 -
26 -import com.google.common.base.MoreObjects;
27 -
28 -/**
29 - * Implementation of BGP flow specification RIB.
30 - */
31 -public class BgpFlowSpecRib {
32 - private Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> flowSpecTree = new TreeMap<>();
33 - private Map<RouteDistinguisher, Map<BgpFlowSpecPrefix, BgpFlowSpecDetails>> vpnFlowSpecTree = new TreeMap<>();
34 -
35 - /**
36 - * Returns the BGP flow spec info.
37 - *
38 - * @return BGP flow spec tree
39 - */
40 - public Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> flowSpecTree() {
41 - return flowSpecTree;
42 - }
43 -
44 - /**
45 - * Gets VPN flowspec tree.
46 - *
47 - * @return vpn flow spec tree
48 - */
49 - public Map<RouteDistinguisher, Map<BgpFlowSpecPrefix, BgpFlowSpecDetails>> vpnFlowSpecTree() {
50 - return vpnFlowSpecTree;
51 - }
52 -
53 -
54 - /**
55 - * Update BGP flow spec details.
56 - *
57 - * @param prefix prefix Info
58 - * @param flowSpec BGP flow specifications details
59 - */
60 - public void add(BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
61 - if (flowSpecTree.containsKey(prefix)) {
62 - flowSpecTree.replace(prefix, flowSpec);
63 - } else {
64 - flowSpecTree.put(prefix, flowSpec);
65 - }
66 - }
67 -
68 - /**
69 - * Removes flow spec.
70 - *
71 - * @param flowSpec BGP flow specification
72 - */
73 - public void delete(BgpFlowSpecPrefix flowSpec) {
74 - if (flowSpecTree.containsKey(flowSpec)) {
75 - flowSpecTree.remove(flowSpec);
76 - }
77 - }
78 -
79 - /**
80 - * Update BGP flow spec details with routedistinguisher.
81 - *
82 - * @param routeDistinguisher route distinguisher
83 - * @param prefix prefix Info
84 - * @param flowSpec BGP flow specifications details
85 - */
86 - public void add(RouteDistinguisher routeDistinguisher, BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
87 - Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree;
88 - if (!vpnFlowSpecTree.containsKey(routeDistinguisher)) {
89 -
90 - fsTree = new TreeMap<>();
91 -
92 - vpnFlowSpecTree.put(routeDistinguisher, fsTree);
93 - } else {
94 - fsTree = vpnFlowSpecTree().get(routeDistinguisher);
95 - }
96 - if (fsTree.containsKey(prefix)) {
97 - fsTree.replace(prefix, flowSpec);
98 - } else {
99 - fsTree.put(prefix, flowSpec);
100 - }
101 - }
102 -
103 - /**
104 - * Removes flow spec.
105 - *
106 - * @param routeDistinguisher route distinguisher
107 - * @param flowSpecPrefix BGP flow specification prefix
108 - */
109 - public void delete(RouteDistinguisher routeDistinguisher, BgpFlowSpecPrefix flowSpecPrefix) {
110 - if (vpnFlowSpecTree.containsKey(routeDistinguisher)) {
111 - Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree = vpnFlowSpecTree().get(routeDistinguisher);
112 - fsTree.remove(flowSpecPrefix);
113 - if (fsTree.size() == 0) {
114 - vpnFlowSpecTree.remove(routeDistinguisher);
115 - }
116 - }
117 - }
118 -
119 - @Override
120 - public String toString() {
121 - return MoreObjects.toStringHelper(getClass())
122 - .omitNullValues()
123 - .add("flowSpecTree", flowSpecTree)
124 - .add("vpnFlowSpecTree", vpnFlowSpecTree)
125 - .toString();
126 - }
127 -}
1 -/*
2 - * Copyright 2016-present 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.controller.impl;
17 -
18 -import org.junit.Test;
19 -import org.onlab.packet.IpAddress;
20 -import org.onlab.packet.IpPrefix;
21 -import org.onosproject.bgp.controller.impl.BgpFlowSpecRib;
22 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
23 -import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix;
24 -import org.onosproject.bgpio.types.BgpFsOperatorValue;
25 -import org.onosproject.bgpio.types.BgpFsPortNum;
26 -import org.onosproject.bgpio.types.BgpValueType;
27 -import org.onosproject.bgpio.types.RouteDistinguisher;
28 -
29 -import java.util.ArrayList;
30 -import java.util.LinkedList;
31 -import java.util.List;
32 -
33 -import static org.hamcrest.MatcherAssert.assertThat;
34 -import static org.hamcrest.core.Is.is;
35 -
36 -/**
37 - * Test cases for BGP Selection Algorithm.
38 - */
39 -public class BgpFlowSpecRibTest {
40 -
41 - /**
42 - * Add flow specification to rib.
43 - */
44 - @Test
45 - public void bgpFlowSpecRibTests1() {
46 - List<BgpValueType> flowSpecComponents = new LinkedList<>();
47 - List<BgpFsOperatorValue> operatorValue = new ArrayList<>();
48 - BgpFlowSpecRib rib = new BgpFlowSpecRib();
49 -
50 - IpPrefix destinationPrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.1"), 32);
51 - IpPrefix sourcePrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.2"), 32);
52 -
53 - BgpFlowSpecPrefix prefix = new BgpFlowSpecPrefix(destinationPrefix, sourcePrefix);
54 -
55 - byte[] port = new byte[] {(byte) 0x1 };
56 - operatorValue.add(new BgpFsOperatorValue((byte) 0x81, port));
57 -
58 - BgpFsPortNum portNum = new BgpFsPortNum(operatorValue);
59 - flowSpecComponents.add(portNum);
60 -
61 - BgpFlowSpecDetails flowSpec = new BgpFlowSpecDetails(flowSpecComponents);
62 -
63 - rib.add(prefix, flowSpec);
64 -
65 - boolean isPresent = rib.flowSpecTree().containsKey(prefix);
66 - assertThat(isPresent, is(true));
67 - }
68 -
69 - /**
70 - * Add and delete flow specification to rib.
71 - */
72 - @Test
73 - public void bgpFlowSpecRibTest2() {
74 - List<BgpValueType> flowSpecComponents = new LinkedList<>();
75 - List<BgpFsOperatorValue> operatorValue = new ArrayList<>();
76 - BgpFlowSpecRib rib = new BgpFlowSpecRib();
77 -
78 - IpPrefix destinationPrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.1"), 32);
79 - IpPrefix sourcePrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.2"), 32);
80 -
81 - BgpFlowSpecPrefix prefix = new BgpFlowSpecPrefix(destinationPrefix, sourcePrefix);
82 - byte[] port = new byte[] {(byte) 0x1 };
83 - operatorValue.add(new BgpFsOperatorValue((byte) 0x81, port));
84 -
85 - BgpFsPortNum portNum = new BgpFsPortNum(operatorValue);
86 - flowSpecComponents.add(portNum);
87 -
88 - BgpFlowSpecDetails flowSpec = new BgpFlowSpecDetails(flowSpecComponents);
89 -
90 - rib.add(prefix, flowSpec);
91 -
92 - boolean isPresent = rib.flowSpecTree().containsKey(prefix);
93 - assertThat(isPresent, is(true));
94 -
95 - rib.delete(prefix);
96 - isPresent = rib.flowSpecTree().containsKey(prefix);
97 - assertThat(isPresent, is(false));
98 -
99 - }
100 -
101 - /**
102 - * Add and delete flow specification with a specific VPN to rib.
103 - */
104 - @Test
105 - public void bgpFlowSpecRibTest3() {
106 - List<BgpValueType> flowSpecComponents = new LinkedList<>();
107 - List<BgpFsOperatorValue> operatorValue = new ArrayList<>();
108 - RouteDistinguisher routeDistinguisher = new RouteDistinguisher(1);
109 - BgpFlowSpecRib rib = new BgpFlowSpecRib();
110 -
111 - IpPrefix destinationPrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.1"), 32);
112 - IpPrefix sourcePrefix = IpPrefix.valueOf(IpAddress.valueOf("10.0.1.2"), 32);
113 -
114 - BgpFlowSpecPrefix prefix = new BgpFlowSpecPrefix(destinationPrefix, sourcePrefix);
115 -
116 - byte[] port = new byte[] {(byte) 0x1 };
117 - operatorValue.add(new BgpFsOperatorValue((byte) 0x81, port));
118 -
119 - BgpFsPortNum portNum = new BgpFsPortNum(operatorValue);
120 - flowSpecComponents.add(portNum);
121 -
122 - BgpFlowSpecDetails flowSpec = new BgpFlowSpecDetails(flowSpecComponents);
123 -
124 - rib.add(routeDistinguisher, prefix, flowSpec);
125 -
126 - boolean isPresent = rib.vpnFlowSpecTree().containsKey(routeDistinguisher);
127 - assertThat(isPresent, is(true));
128 -
129 - rib.delete(routeDistinguisher, prefix);
130 - isPresent = rib.vpnFlowSpecTree().containsKey(routeDistinguisher);
131 - assertThat(isPresent, is(false));
132 - }
133 -}