blob: 7879bd69ebffb37628c1db5cd23804c19a03689e [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;
Priyanka B85843952015-10-14 11:56:30 +053066
67 private boolean isAsPath = false;
68 private List<Short> aspathSet;
69 private List<Short> aspathSeq;
70
71 /**
72 * Initialize Fields.
73 */
74 public AsPath() {
75 this.aspathSeq = null;
76 this.aspathSet = null;
77 }
78
79 /**
80 * Constructor to initialize parameters.
81 *
82 * @param aspathSet ASpath Set type
83 * @param aspathSeq ASpath Sequence type
84 */
85 public AsPath(List<Short> aspathSet, List<Short> aspathSeq) {
86 this.aspathSeq = aspathSeq;
87 this.aspathSet = aspathSet;
88 this.isAsPath = true;
89 }
90
91 /**
92 * Reads from the channel buffer and parses AsPath.
93 *
94 * @param cb ChannelBuffer
95 * @return object of AsPath
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053096 * @throws BgpParseException while parsing AsPath
Priyanka B85843952015-10-14 11:56:30 +053097 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 public static AsPath read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053099 List<Short> aspathSet = new ArrayList<>();
100 List<Short> aspathSeq = new ArrayList<>();
101 ChannelBuffer tempCb = cb.copy();
102 Validation validation = Validation.parseAttributeHeader(cb);
103
104 if (cb.readableBytes() < validation.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530105 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +0530106 validation.getLength());
107 }
108 //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 +0530109 int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
110 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +0530111 ChannelBuffer data = tempCb.readBytes(len);
112 if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530113 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +0530114 }
115
116 ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
117 while (tempBuf.readableBytes() > 0) {
118 byte pathSegType = tempBuf.readByte();
119 //no of ASes
120 byte pathSegLen = tempBuf.readByte();
121 int length = pathSegLen * ASNUM_SIZE;
122 if (tempBuf.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530123 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
124 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
Priyanka B85843952015-10-14 11:56:30 +0530125 }
126 ChannelBuffer aspathBuf = tempBuf.readBytes(length);
127 while (aspathBuf.readableBytes() > 0) {
128 short asNum;
129 asNum = aspathBuf.readShort();
130 switch (pathSegType) {
131 case ASPATH_SET_TYPE:
132 aspathSet.add(asNum);
133 break;
134 case ASPATH_SEQ_TYPE:
135 aspathSeq.add(asNum);
136 break;
137 default: log.debug("Other type Not Supported:" + pathSegType);
138 }
139 }
140 }
141 return new AsPath(aspathSet, aspathSeq);
142 }
143
144 @Override
145 public short getType() {
146 return ASPATH_TYPE;
147 }
148
149 /**
150 * Returns whether ASpath path attribute is present.
151 *
152 * @return whether ASpath path attribute is present
153 */
154 public boolean isaspathSet() {
155 return this.isAsPath;
156 }
157
158 /**
159 * Returns list of ASNum in ASpath Sequence.
160 *
161 * @return list of ASNum in ASpath Sequence
162 */
163 public List<Short> asPathSeq() {
164 return this.aspathSeq;
165 }
166
167 /**
168 * Returns list of ASNum in ASpath SET.
169 *
170 * @return list of ASNum in ASpath SET
171 */
172 public List<Short> asPathSet() {
173 return this.aspathSet;
174 }
175
176 @Override
177 public int hashCode() {
178 return Objects.hash(aspathSet, aspathSeq);
179 }
180
181 @Override
182 public boolean equals(Object obj) {
183 if (this == obj) {
184 return true;
185 }
186 if (obj instanceof AsPath) {
187 AsPath other = (AsPath) obj;
188 return Objects.equals(aspathSet, other.aspathSet) && Objects.equals(aspathSeq, other.aspathSeq);
189 }
190 return false;
191 }
192
193 @Override
194 public String toString() {
195 return MoreObjects.toStringHelper(getClass())
196 .omitNullValues()
197 .add("aspathSet", aspathSet)
198 .add("aspathSeq", aspathSeq)
199 .toString();
200 }
201
202 @Override
203 public int write(ChannelBuffer cb) {
204 //Not required to Implement as of now
205 return 0;
206 }
Priyanka B02040732015-11-29 11:30:29 +0530207
208 @Override
209 public int compareTo(Object o) {
210 // TODO Auto-generated method stub
211 return 0;
212 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700213}