blob: 2a050c441dbc93370da10c59a39663cc4ef0f349 [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
19import java.util.ArrayList;
20import java.util.List;
21import java.util.Objects;
22
23import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053024import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Be9ed7292015-11-18 22:12:29 +053025import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053026import org.onosproject.bgpio.util.Validation;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import com.google.common.base.MoreObjects;
31
32/**
33 * Provides Implementation of AsPath mandatory BGP Path Attribute.
34 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class AsPath implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053036 /**
37 * Enum to provide AS types.
38 */
39 public enum ASTYPE {
40 AS_SET(1), AS_SEQUENCE(2), AS_CONFED_SEQUENCE(3), AS_CONFED_SET(4);
41 int value;
42
43 /**
44 * Assign val with the value as the AS type.
45 *
46 * @param val AS type
47 */
48 ASTYPE(int val) {
49 value = val;
50 }
51
52 /**
53 * Returns value of AS type.
54 *
55 * @return AS type
56 */
Priyanka Be9ed7292015-11-18 22:12:29 +053057 public byte type() {
Priyanka B85843952015-10-14 11:56:30 +053058 return (byte) value;
59 }
60 }
61
62 private static final Logger log = LoggerFactory.getLogger(AsPath.class);
63 public static final byte ASPATH_TYPE = 2;
64 public static final byte ASPATH_SET_TYPE = 1;
65 public static final byte ASPATH_SEQ_TYPE = 2;
66 public static final byte ASNUM_SIZE = 2;
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) {
205 //Not required to Implement as of now
206 return 0;
207 }
Priyanka B02040732015-11-29 11:30:29 +0530208
209 @Override
210 public int compareTo(Object o) {
211 // TODO Auto-generated method stub
212 return 0;
213 }
Priyanka B85843952015-10-14 11:56:30 +0530214}