Shashikanth VH
Committed by Gerrit Code Review

[ONOS-3856] BGP flow specification extended community path attribute parsing.

Change-Id: I9ea9db5565e91598328fdb703186c3d6577e2dc7
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.types;
18 +
19 +import com.google.common.base.MoreObjects;
20 +import org.jboss.netty.buffer.ChannelBuffer;
21 +import org.onosproject.bgpio.exceptions.BgpParseException;
22 +import org.onosproject.bgpio.util.Constants;
23 +import org.onosproject.bgpio.util.Validation;
24 +import org.slf4j.Logger;
25 +import org.slf4j.LoggerFactory;
26 +import java.util.Objects;
27 +
28 +/**
29 + * Provides implementation of extended community BGP Path Attribute.
30 + */
31 +public class BgpExtendedCommunity implements BgpValueType {
32 +
33 + private static final Logger log = LoggerFactory.getLogger(BgpExtendedCommunity.class);
34 + public static final short TYPE = Constants.BGP_EXTENDED_COMMUNITY;
35 + public static final byte FLAGS = (byte) 0xC0;
36 + private BgpValueType fsActionTlv;
37 +
38 + /**
39 + * Constructor to initialize the value.
40 + *
41 + * @param fsActionTlv flow specification action type
42 + */
43 + public BgpExtendedCommunity(BgpValueType fsActionTlv) {
44 + this.fsActionTlv = fsActionTlv;
45 + }
46 +
47 + /**
48 + * Returns extended community type.
49 + *
50 + * @return extended community
51 + */
52 + public BgpValueType fsActionTlv() {
53 + return this.fsActionTlv;
54 + }
55 +
56 + /**
57 + * Reads from the channel buffer and parses extended community.
58 + *
59 + * @param cb ChannelBuffer
60 + * @return object of BgpExtendedCommunity
61 + * @throws BgpParseException while parsing extended community
62 + */
63 + public static BgpExtendedCommunity read(ChannelBuffer cb) throws BgpParseException {
64 +
65 + ChannelBuffer tempCb = cb.copy();
66 + Validation validation = Validation.parseAttributeHeader(cb);
67 + BgpValueType fsActionTlv = null;
68 +
69 + if (cb.readableBytes() < validation.getLength()) {
70 + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
71 + validation.getLength());
72 + }
73 + //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
74 + int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
75 + .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
76 + ChannelBuffer data = tempCb.readBytes(len);
77 + if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
78 + throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
79 + }
80 +
81 + ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
82 + if (tempBuf.readableBytes() > 0) {
83 + ChannelBuffer actionBuf = tempBuf.readBytes(validation.getLength());
84 + short actionType = actionBuf.readShort();
85 + short length = (short) validation.getLength();
86 + switch (actionType) {
87 + case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION:
88 + fsActionTlv = BgpFsActionTrafficAction.read(actionBuf);
89 + break;
90 + case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING:
91 + fsActionTlv = BgpFsActionTrafficMarking.read(actionBuf);
92 + break;
93 + case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE:
94 + fsActionTlv = BgpFsActionTrafficRate.read(actionBuf);
95 + break;
96 + case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT:
97 + fsActionTlv = BgpFsActionReDirect.read(actionBuf);
98 + break;
99 + default: log.debug("Other type Not Supported:" + actionType);
100 + break;
101 + }
102 + }
103 + return new BgpExtendedCommunity(fsActionTlv);
104 + }
105 +
106 + @Override
107 + public short getType() {
108 + return TYPE;
109 + }
110 +
111 + @Override
112 + public int hashCode() {
113 + return Objects.hash(fsActionTlv);
114 + }
115 +
116 + @Override
117 + public boolean equals(Object obj) {
118 + if (this == obj) {
119 + return true;
120 + }
121 + if (obj instanceof BgpExtendedCommunity) {
122 + BgpExtendedCommunity other = (BgpExtendedCommunity) obj;
123 + return Objects.equals(fsActionTlv, other.fsActionTlv);
124 + }
125 + return false;
126 + }
127 +
128 + @Override
129 + public String toString() {
130 + return MoreObjects.toStringHelper(getClass())
131 + .omitNullValues()
132 + .add("fsActionTlv", fsActionTlv)
133 + .toString();
134 + }
135 +
136 + @Override
137 + public int write(ChannelBuffer cb) {
138 + int iLenStartIndex = cb.writerIndex();
139 + cb.writeByte(FLAGS);
140 + cb.writeByte(getType());
141 +
142 + int iActionLenIndex = cb.writerIndex();
143 + cb.writeByte(0);
144 +
145 + if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION) {
146 + BgpFsActionTrafficAction trafficAction = (BgpFsActionTrafficAction) fsActionTlv;
147 + trafficAction.write(cb);
148 + } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING) {
149 + BgpFsActionTrafficMarking trafficMarking = (BgpFsActionTrafficMarking) fsActionTlv;
150 + trafficMarking.write(cb);
151 + } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE) {
152 + BgpFsActionTrafficRate trafficRate = (BgpFsActionTrafficRate) fsActionTlv;
153 + trafficRate.write(cb);
154 + } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT) {
155 + BgpFsActionReDirect trafficRedirect = (BgpFsActionReDirect) fsActionTlv;
156 + trafficRedirect.write(cb);
157 + }
158 +
159 + int fsActionLen = cb.writerIndex() - iActionLenIndex;
160 + cb.setByte(iActionLenIndex, (byte) (fsActionLen - 1));
161 +
162 + return cb.writerIndex() - iLenStartIndex;
163 + }
164 +
165 + @Override
166 + public int compareTo(Object o) {
167 + // TODO Auto-generated method stub
168 + return 0;
169 + }
170 +}
...@@ -77,12 +77,15 @@ public class BgpFsActionReDirect implements BgpValueType { ...@@ -77,12 +77,15 @@ public class BgpFsActionReDirect implements BgpValueType {
77 * Reads the channel buffer and returns object. 77 * Reads the channel buffer and returns object.
78 * 78 *
79 * @param cb channelBuffer 79 * @param cb channelBuffer
80 - * @param type address type
81 * @return object of flow spec action redirect 80 * @return object of flow spec action redirect
82 * @throws BgpParseException while parsing BgpFsActionReDirect 81 * @throws BgpParseException while parsing BgpFsActionReDirect
83 */ 82 */
84 - public static BgpFsActionReDirect read(ChannelBuffer cb, short type) throws BgpParseException { 83 + public static BgpFsActionReDirect read(ChannelBuffer cb) throws BgpParseException {
85 - return null; 84 + byte[] routeTarget;
85 + ChannelBuffer tempCb = cb.copy();
86 +
87 + routeTarget = tempCb.readBytes(tempCb.readableBytes()).array();
88 + return new BgpFsActionReDirect(routeTarget);
86 } 89 }
87 90
88 @Override 91 @Override
......
...@@ -77,12 +77,15 @@ public class BgpFsActionTrafficAction implements BgpValueType { ...@@ -77,12 +77,15 @@ public class BgpFsActionTrafficAction implements BgpValueType {
77 * Reads the channel buffer and returns object. 77 * Reads the channel buffer and returns object.
78 * 78 *
79 * @param cb channelBuffer 79 * @param cb channelBuffer
80 - * @param type address type
81 * @return object of flow spec action traffic rate 80 * @return object of flow spec action traffic rate
82 * @throws BgpParseException while parsing BgpFsActionTrafficAction 81 * @throws BgpParseException while parsing BgpFsActionTrafficAction
83 */ 82 */
84 - public static BgpFsActionTrafficAction read(ChannelBuffer cb, short type) throws BgpParseException { 83 + public static BgpFsActionTrafficAction read(ChannelBuffer cb) throws BgpParseException {
85 - return null; 84 + byte[] bitMask;
85 + ChannelBuffer tempCb = cb.copy();
86 +
87 + bitMask = tempCb.readBytes(tempCb.readableBytes()).array();
88 + return new BgpFsActionTrafficAction(bitMask);
86 } 89 }
87 90
88 @Override 91 @Override
......
...@@ -77,12 +77,15 @@ public class BgpFsActionTrafficMarking implements BgpValueType { ...@@ -77,12 +77,15 @@ public class BgpFsActionTrafficMarking implements BgpValueType {
77 * Reads the channel buffer and returns object. 77 * Reads the channel buffer and returns object.
78 * 78 *
79 * @param cb channelBuffer 79 * @param cb channelBuffer
80 - * @param type address type
81 * @return object of flow spec action traffic marking 80 * @return object of flow spec action traffic marking
82 * @throws BgpParseException while parsing BgpFsActionTrafficMarking 81 * @throws BgpParseException while parsing BgpFsActionTrafficMarking
83 */ 82 */
84 - public static BgpFsActionTrafficMarking read(ChannelBuffer cb, short type) throws BgpParseException { 83 + public static BgpFsActionTrafficMarking read(ChannelBuffer cb) throws BgpParseException {
85 - return null; 84 + byte[] dscpValue;
85 + ChannelBuffer tempCb = cb.copy();
86 +
87 + dscpValue = tempCb.readBytes(tempCb.readableBytes()).array();
88 + return new BgpFsActionTrafficMarking(dscpValue);
86 } 89 }
87 90
88 @Override 91 @Override
......
...@@ -80,12 +80,17 @@ public class BgpFsActionTrafficRate implements BgpValueType { ...@@ -80,12 +80,17 @@ public class BgpFsActionTrafficRate implements BgpValueType {
80 * Reads the channel buffer and returns object. 80 * Reads the channel buffer and returns object.
81 * 81 *
82 * @param cb channelBuffer 82 * @param cb channelBuffer
83 - * @param type address type
84 * @return object of flow spec action traffic rate 83 * @return object of flow spec action traffic rate
85 * @throws BgpParseException while parsing BgpFsActionTrafficRate 84 * @throws BgpParseException while parsing BgpFsActionTrafficRate
86 */ 85 */
87 - public static BgpFsActionTrafficRate read(ChannelBuffer cb, short type) throws BgpParseException { 86 + public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException {
88 - return null; 87 + short asn;
88 + float rate;
89 + ChannelBuffer tempCb = cb.copy();
90 +
91 + asn = tempCb.readShort();
92 + rate = tempCb.readFloat();
93 + return new BgpFsActionTrafficRate(asn, rate);
89 } 94 }
90 95
91 @Override 96 @Override
......
...@@ -48,6 +48,8 @@ public final class Constants { ...@@ -48,6 +48,8 @@ public final class Constants {
48 public static final int ENHANCED = 0x20; 48 public static final int ENHANCED = 0x20;
49 public static final int RESERVED = 0x40; 49 public static final int RESERVED = 0x40;
50 50
51 + public static final byte BGP_EXTENDED_COMMUNITY = 0x10;
52 +
51 public static final byte BGP_FLOWSPEC_DST_PREFIX = 0x01; 53 public static final byte BGP_FLOWSPEC_DST_PREFIX = 0x01;
52 public static final byte BGP_FLOWSPEC_SRC_PREFIX = 0x02; 54 public static final byte BGP_FLOWSPEC_SRC_PREFIX = 0x02;
53 public static final byte BGP_FLOWSPEC_IP_PROTO = 0x03; 55 public static final byte BGP_FLOWSPEC_IP_PROTO = 0x03;
......