blob: c4409f4188ea03e57ba9236c9603c5f7883a8b60 [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 */
16package org.onosproject.bgpio.types;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Be9ed7292015-11-18 22:12:29 +053024import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053025import org.onosproject.bgpio.util.Validation;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.google.common.base.MoreObjects;
30
31/**
32 * Provides Implementation of As4Path BGP Path Attribute.
33 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034public class As4Path implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053035 private static final Logger log = LoggerFactory.getLogger(AsPath.class);
36 public static final byte AS4PATH_TYPE = 17;
37 public static final byte ASNUM_SIZE = 4;
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 */
125 public List<Integer> as4PathSEQ() {
126 return this.as4pathSeq;
127 }
128
129 /**
130 * Returns list of ASNum in AS4path Set.
131 *
132 * @return list of ASNum in AS4path Set
133 */
134 public List<Integer> as4PathSET() {
135 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) {
166 //Not required to Implement as of now
167 return 0;
168 }
169}