blob: 51779ccf5301267076d5a206c692b41522d93022 [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
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;
Priyanka B85843952015-10-14 11:56:30 +053037
38 private List<Integer> as4pathSet;
39 private List<Integer> as4pathSeq;
40
41 /**
42 * Initialize fields.
43 */
44 public As4Path() {
45 this.as4pathSeq = null;
46 this.as4pathSet = null;
47 }
48
49 /**
50 * Constructor to initialize parameters.
51 *
52 * @param as4pathSet AS4path Set
53 * @param as4pathSeq AS4path Sequence
54 */
55 public As4Path(List<Integer> as4pathSet, List<Integer> as4pathSeq) {
56 this.as4pathSeq = as4pathSeq;
57 this.as4pathSet = as4pathSet;
58 }
59
60 /**
61 * Reads from the channel buffer and parses As4Path.
62 *
63 * @param cb ChannelBuffer
64 * @return object of As4Path
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053065 * @throws BgpParseException while parsing As4Path
Priyanka B85843952015-10-14 11:56:30 +053066 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053067 public static As4Path read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053068 List<Integer> as4pathSet = new ArrayList<>();
69 List<Integer> as4pathSeq = new ArrayList<>();
70 ChannelBuffer tempCb = cb.copy();
71 Validation validation = Validation.parseAttributeHeader(cb);
72
73 if (cb.readableBytes() < validation.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053074 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053075 validation.getLength());
76 }
77 //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 +053078 int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
79 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053080 ChannelBuffer data = tempCb.readBytes(len);
81 if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053082 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053083 }
84
85 ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
86 while (tempBuf.readableBytes() > 0) {
87 byte pathSegType = tempBuf.readByte();
88 //no of ASes
89 byte pathSegLen = tempBuf.readByte();
90 //length = no of Ases * ASnum size (4 bytes)
91 int length = pathSegLen * ASNUM_SIZE;
92 if (tempBuf.readableBytes() < length) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053093 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
94 BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
Priyanka B85843952015-10-14 11:56:30 +053095 }
96 ChannelBuffer aspathBuf = tempBuf.readBytes(length);
97 while (aspathBuf.readableBytes() > 0) {
98 int asNum;
99 asNum = aspathBuf.readInt();
100 switch (pathSegType) {
101 case AsPath.ASPATH_SET_TYPE:
102 as4pathSet.add(asNum);
103 break;
104 case AsPath.ASPATH_SEQ_TYPE:
105 as4pathSeq.add(asNum);
106 break;
107 default: log.debug("Other type Not Supported:" + pathSegType);
108 }
109 }
110 }
111 return new As4Path(as4pathSet, as4pathSeq);
112 }
113
114 @Override
115 public short getType() {
116 return AS4PATH_TYPE;
117 }
118
119 /**
120 * Returns list of ASNum in AS4path Sequence.
121 *
122 * @return list of ASNum in AS4path Sequence
123 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700124 public List<Integer> as4PathSeq() {
Priyanka B85843952015-10-14 11:56:30 +0530125 return this.as4pathSeq;
126 }
127
128 /**
129 * Returns list of ASNum in AS4path Set.
130 *
131 * @return list of ASNum in AS4path Set
132 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700133 public List<Integer> as4PathSet() {
Priyanka B85843952015-10-14 11:56:30 +0530134 return this.as4pathSet;
135 }
136
137 @Override
138 public int hashCode() {
139 return Objects.hash(as4pathSet, as4pathSeq);
140 }
141
142 @Override
143 public boolean equals(Object obj) {
144 if (this == obj) {
145 return true;
146 }
147 if (obj instanceof As4Path) {
148 As4Path other = (As4Path) obj;
149 return Objects.equals(as4pathSet, other.as4pathSet) && Objects.equals(as4pathSeq, other.as4pathSeq);
150 }
151 return false;
152 }
153
154 @Override
155 public String toString() {
156 return MoreObjects.toStringHelper(getClass())
157 .omitNullValues()
158 .add("as4pathSet", as4pathSet)
159 .add("as4pathSeq", as4pathSeq)
160 .toString();
161 }
162
163 @Override
164 public int write(ChannelBuffer cb) {
165 //Not required to Implement as of now
166 return 0;
167 }
Priyanka B02040732015-11-29 11:30:29 +0530168
169 @Override
170 public int compareTo(Object o) {
171 // TODO Auto-generated method stub
172 return 0;
173 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700174}