blob: c4b9c71f2f74b41c9ab0695d14b7a04560986ef0 [file] [log] [blame]
Priyanka B85843952015-10-14 11:56:30 +05301/*
2 * Copyright 2015 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
17package org.onosproject.bgpio.types;
18
Jonathan Hart51539b82015-10-29 09:53:04 -070019import com.google.common.base.MoreObjects;
Priyanka B85843952015-10-14 11:56:30 +053020import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Be9ed7292015-11-18 22:12:29 +053022import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053023import org.onosproject.bgpio.util.Validation;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
Jonathan Hart51539b82015-10-29 09:53:04 -070027import java.util.ArrayList;
28import java.util.List;
29import java.util.Objects;
Priyanka B85843952015-10-14 11:56:30 +053030
31/**
32 * Provides Implementation of AsPath mandatory BGP Path Attribute.
33 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034public class AsPath implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053035 /**
36 * Enum to provide AS types.
37 */
Jonathan Hart51539b82015-10-29 09:53:04 -070038 public enum AsType {
Priyanka B85843952015-10-14 11:56:30 +053039 AS_SET(1), AS_SEQUENCE(2), AS_CONFED_SEQUENCE(3), AS_CONFED_SET(4);
40 int value;
41
42 /**
43 * Assign val with the value as the AS type.
44 *
45 * @param val AS type
46 */
Jonathan Hart51539b82015-10-29 09:53:04 -070047 AsType(int val) {
Priyanka B85843952015-10-14 11:56:30 +053048 value = val;
49 }
50
51 /**
52 * Returns value of AS type.
53 *
54 * @return AS type
55 */
Priyanka Be9ed7292015-11-18 22:12:29 +053056 public byte type() {
Priyanka B85843952015-10-14 11:56:30 +053057 return (byte) value;
58 }
59 }
60
61 private static final Logger log = LoggerFactory.getLogger(AsPath.class);
62 public static final byte ASPATH_TYPE = 2;
63 public static final byte ASPATH_SET_TYPE = 1;
64 public static final byte ASPATH_SEQ_TYPE = 2;
65 public static final byte ASNUM_SIZE = 2;
Shashikanth VH15de0d92016-02-08 15:11:50 +053066 public static final byte FLAGS = (byte) 0x40;
Priyanka B85843952015-10-14 11:56:30 +053067
68 private boolean isAsPath = false;
69 private List<Short> aspathSet;
70 private List<Short> aspathSeq;
71
72 /**
73 * Initialize Fields.
74 */
75 public AsPath() {
76 this.aspathSeq = null;
77 this.aspathSet = null;
78 }
79
80 /**
81 * Constructor to initialize parameters.
82 *
83 * @param aspathSet ASpath Set type
84 * @param aspathSeq ASpath Sequence type
85 */
86 public AsPath(List<Short> aspathSet, List<Short> aspathSeq) {
87 this.aspathSeq = aspathSeq;
88 this.aspathSet = aspathSet;
89 this.isAsPath = true;
90 }
91
92 /**
93 * Reads from the channel buffer and parses AsPath.
94 *
95 * @param cb ChannelBuffer
96 * @return object of AsPath
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053097 * @throws BgpParseException while parsing AsPath
Priyanka B85843952015-10-14 11:56:30 +053098 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053099 public static AsPath read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +0530100 List<Short> aspathSet = new ArrayList<>();
101 List<Short> aspathSeq = new ArrayList<>();
102 ChannelBuffer tempCb = cb.copy();
103 Validation validation = Validation.parseAttributeHeader(cb);
104
105 if (cb.readableBytes() < validation.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530106 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +0530107 validation.getLength());
108 }
109 //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
Priyanka Be9ed7292015-11-18 22:12:29 +0530110 int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
111 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +0530112 ChannelBuffer data = tempCb.readBytes(len);
113 if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530114 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +0530115 }
116
117 ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
118 while (tempBuf.readableBytes() > 0) {
119 byte pathSegType = tempBuf.readByte();
120 //no of ASes
121 byte pathSegLen = tempBuf.readByte();
122 int length = pathSegLen * ASNUM_SIZE;
123 if (tempBuf.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530124 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
125 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
Priyanka B85843952015-10-14 11:56:30 +0530126 }
127 ChannelBuffer aspathBuf = tempBuf.readBytes(length);
128 while (aspathBuf.readableBytes() > 0) {
129 short asNum;
130 asNum = aspathBuf.readShort();
131 switch (pathSegType) {
132 case ASPATH_SET_TYPE:
133 aspathSet.add(asNum);
134 break;
135 case ASPATH_SEQ_TYPE:
136 aspathSeq.add(asNum);
137 break;
138 default: log.debug("Other type Not Supported:" + pathSegType);
139 }
140 }
141 }
142 return new AsPath(aspathSet, aspathSeq);
143 }
144
145 @Override
146 public short getType() {
147 return ASPATH_TYPE;
148 }
149
150 /**
151 * Returns whether ASpath path attribute is present.
152 *
153 * @return whether ASpath path attribute is present
154 */
155 public boolean isaspathSet() {
156 return this.isAsPath;
157 }
158
159 /**
160 * Returns list of ASNum in ASpath Sequence.
161 *
162 * @return list of ASNum in ASpath Sequence
163 */
164 public List<Short> asPathSeq() {
165 return this.aspathSeq;
166 }
167
168 /**
169 * Returns list of ASNum in ASpath SET.
170 *
171 * @return list of ASNum in ASpath SET
172 */
173 public List<Short> asPathSet() {
174 return this.aspathSet;
175 }
176
177 @Override
178 public int hashCode() {
179 return Objects.hash(aspathSet, aspathSeq);
180 }
181
182 @Override
183 public boolean equals(Object obj) {
184 if (this == obj) {
185 return true;
186 }
187 if (obj instanceof AsPath) {
188 AsPath other = (AsPath) obj;
189 return Objects.equals(aspathSet, other.aspathSet) && Objects.equals(aspathSeq, other.aspathSeq);
190 }
191 return false;
192 }
193
194 @Override
195 public String toString() {
196 return MoreObjects.toStringHelper(getClass())
197 .omitNullValues()
198 .add("aspathSet", aspathSet)
199 .add("aspathSeq", aspathSeq)
200 .toString();
201 }
202
203 @Override
204 public int write(ChannelBuffer cb) {
Shashikanth VH15de0d92016-02-08 15:11:50 +0530205 int iLenStartIndex = cb.writerIndex();
206 cb.writeByte(FLAGS);
207 cb.writeByte(getType());
208 if (isaspathSet()) {
209 int iAsLenIndex = cb.writerIndex();
210 cb.writeByte(0);
211 cb.writeByte(ASPATH_SEQ_TYPE);
212 cb.writeByte(aspathSeq.size());
213
214 for (int j = 0; j < aspathSeq.size(); j++) {
215 cb.writeShort(aspathSeq.get(j));
216 }
217
218 int asLen = cb.writerIndex() - iAsLenIndex;
219 cb.setByte(iAsLenIndex, (byte) (asLen - 1));
220 } else {
221 cb.writeByte(0);
222 }
223 return cb.writerIndex() - iLenStartIndex;
Priyanka B85843952015-10-14 11:56:30 +0530224 }
Priyanka B02040732015-11-29 11:30:29 +0530225
226 @Override
227 public int compareTo(Object o) {
228 // TODO Auto-generated method stub
229 return 0;
230 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700231}