blob: 19f31265e70a7155d31a90543c5d2b579e3d4bc4 [file] [log] [blame]
Priyanka B85843952015-10-14 11:56:30 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Priyanka B85843952015-10-14 11:56:30 +05303 *
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 */
16package org.onosproject.bgpio.types;
17
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
Priyanka B85843952015-10-14 11:56:30 +053019import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Be9ed7292015-11-18 22:12:29 +053021import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053022import org.onosproject.bgpio.util.Validation;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Jonathan Hart51539b82015-10-29 09:53:04 -070026import java.util.ArrayList;
27import java.util.List;
28import java.util.Objects;
Priyanka B85843952015-10-14 11:56:30 +053029
30/**
31 * Provides Implementation of As4Path BGP Path Attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class As4Path implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053034 private static final Logger log = LoggerFactory.getLogger(AsPath.class);
35 public static final byte AS4PATH_TYPE = 17;
36 public static final byte ASNUM_SIZE = 4;
Shashikanth VH15de0d92016-02-08 15:11:50 +053037 public static final byte FLAGS = (byte) 0x40;
Priyanka B85843952015-10-14 11:56:30 +053038
39 private List<Integer> as4pathSet;
40 private List<Integer> as4pathSeq;
41
42 /**
43 * Initialize fields.
44 */
45 public As4Path() {
46 this.as4pathSeq = null;
47 this.as4pathSet = null;
48 }
49
50 /**
51 * Constructor to initialize parameters.
52 *
53 * @param as4pathSet AS4path Set
54 * @param as4pathSeq AS4path Sequence
55 */
56 public As4Path(List<Integer> as4pathSet, List<Integer> as4pathSeq) {
57 this.as4pathSeq = as4pathSeq;
58 this.as4pathSet = as4pathSet;
59 }
60
61 /**
62 * Reads from the channel buffer and parses As4Path.
63 *
64 * @param cb ChannelBuffer
65 * @return object of As4Path
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053066 * @throws BgpParseException while parsing As4Path
Priyanka B85843952015-10-14 11:56:30 +053067 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 public static As4Path read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053069 List<Integer> as4pathSet = new ArrayList<>();
70 List<Integer> as4pathSeq = new ArrayList<>();
71 ChannelBuffer tempCb = cb.copy();
72 Validation validation = Validation.parseAttributeHeader(cb);
73
74 if (cb.readableBytes() < validation.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053075 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053076 validation.getLength());
77 }
78 //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 +053079 int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
80 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053081 ChannelBuffer data = tempCb.readBytes(len);
82 if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053084 }
85
86 ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
87 while (tempBuf.readableBytes() > 0) {
88 byte pathSegType = tempBuf.readByte();
89 //no of ASes
90 byte pathSegLen = tempBuf.readByte();
91 //length = no of Ases * ASnum size (4 bytes)
92 int length = pathSegLen * ASNUM_SIZE;
93 if (tempBuf.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
95 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
Priyanka B85843952015-10-14 11:56:30 +053096 }
97 ChannelBuffer aspathBuf = tempBuf.readBytes(length);
98 while (aspathBuf.readableBytes() > 0) {
99 int asNum;
100 asNum = aspathBuf.readInt();
101 switch (pathSegType) {
102 case AsPath.ASPATH_SET_TYPE:
103 as4pathSet.add(asNum);
104 break;
105 case AsPath.ASPATH_SEQ_TYPE:
106 as4pathSeq.add(asNum);
107 break;
108 default: log.debug("Other type Not Supported:" + pathSegType);
109 }
110 }
111 }
112 return new As4Path(as4pathSet, as4pathSeq);
113 }
114
115 @Override
116 public short getType() {
117 return AS4PATH_TYPE;
118 }
119
120 /**
121 * Returns list of ASNum in AS4path Sequence.
122 *
123 * @return list of ASNum in AS4path Sequence
124 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700125 public List<Integer> as4PathSeq() {
Priyanka B85843952015-10-14 11:56:30 +0530126 return this.as4pathSeq;
127 }
128
129 /**
130 * Returns list of ASNum in AS4path Set.
131 *
132 * @return list of ASNum in AS4path Set
133 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700134 public List<Integer> as4PathSet() {
Priyanka B85843952015-10-14 11:56:30 +0530135 return this.as4pathSet;
136 }
137
138 @Override
139 public int hashCode() {
140 return Objects.hash(as4pathSet, as4pathSeq);
141 }
142
143 @Override
144 public boolean equals(Object obj) {
145 if (this == obj) {
146 return true;
147 }
148 if (obj instanceof As4Path) {
149 As4Path other = (As4Path) obj;
150 return Objects.equals(as4pathSet, other.as4pathSet) && Objects.equals(as4pathSeq, other.as4pathSeq);
151 }
152 return false;
153 }
154
155 @Override
156 public String toString() {
157 return MoreObjects.toStringHelper(getClass())
158 .omitNullValues()
159 .add("as4pathSet", as4pathSet)
160 .add("as4pathSeq", as4pathSeq)
161 .toString();
162 }
163
164 @Override
165 public int write(ChannelBuffer cb) {
Shashikanth VH15de0d92016-02-08 15:11:50 +0530166
167 int iLenStartIndex = cb.writerIndex();
168
169 cb.writeByte(FLAGS);
170 cb.writeByte(getType());
171 if ((as4pathSet != null) && (as4pathSeq != null)) {
172 int iAsLenIndex = cb.writerIndex();
173 cb.writeByte(0);
174 cb.writeByte(AsPath.ASPATH_SEQ_TYPE);
175 cb.writeByte(as4pathSeq.size());
176
177 for (int j = 0; j < as4pathSeq.size(); j++) {
178 cb.writeInt(as4pathSeq.get(j));
179 }
180
181 int asLen = cb.writerIndex() - iAsLenIndex;
182 cb.setByte(iAsLenIndex, (byte) (asLen - 1));
183 } else {
184 cb.writeByte(0);
185 }
186 return cb.writerIndex() - iLenStartIndex;
Priyanka B85843952015-10-14 11:56:30 +0530187 }
Priyanka B02040732015-11-29 11:30:29 +0530188
189 @Override
190 public int compareTo(Object o) {
191 // TODO Auto-generated method stub
192 return 0;
193 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700194}