Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.bgpio.types; |
| 18 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 20 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.util.Constants; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 23 | import org.onosproject.bgpio.util.Validation; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.Objects; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Provides Implementation of AsPath mandatory BGP Path Attribute. |
| 33 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 34 | public class AsPath implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Enum to provide AS types. |
| 37 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 38 | public enum AsType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 39 | 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 47 | AsType(int val) { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 48 | value = val; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns value of AS type. |
| 53 | * |
| 54 | * @return AS type |
| 55 | */ |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 56 | public byte type() { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 57 | 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; |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 66 | public static final byte FLAGS = (byte) 0x40; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 67 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 97 | * @throws BgpParseException while parsing AsPath |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 98 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 99 | public static AsPath read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 100 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 106 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 107 | validation.getLength()); |
| 108 | } |
| 109 | //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 110 | int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation |
| 111 | .getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 112 | ChannelBuffer data = tempCb.readBytes(len); |
| 113 | if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 114 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 115 | } |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 124 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 125 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 126 | } |
| 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) { |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 205 | int iLenStartIndex = cb.writerIndex(); |
| 206 | cb.writeByte(FLAGS); |
| 207 | cb.writeByte(getType()); |
| 208 | if (isaspathSet()) { |
| 209 | int iAsLenIndex = cb.writerIndex(); |
| 210 | cb.writeByte(0); |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 211 | if (aspathSeq.size() != 0) { |
| 212 | cb.writeByte(ASPATH_SEQ_TYPE); |
| 213 | cb.writeByte(aspathSeq.size()); |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 214 | |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 215 | for (int j = 0; j < aspathSeq.size(); j++) { |
| 216 | cb.writeShort(aspathSeq.get(j)); |
| 217 | } |
| 218 | int asLen = cb.writerIndex() - iAsLenIndex; |
| 219 | cb.setByte(iAsLenIndex, (byte) (asLen - 1)); |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 220 | } |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 221 | } else { |
| 222 | cb.writeByte(0); |
| 223 | } |
| 224 | return cb.writerIndex() - iLenStartIndex; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 225 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 226 | |
| 227 | @Override |
| 228 | public int compareTo(Object o) { |
| 229 | // TODO Auto-generated method stub |
| 230 | return 0; |
| 231 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 232 | } |