Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.bgpio.types; |
| 17 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 19 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 20 | import org.onosproject.bgpio.exceptions.BgpParseException; |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.util.Constants; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.util.Validation; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | import java.util.Objects; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Provides Implementation of As4Path BGP Path Attribute. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class As4Path implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 34 | 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 B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 37 | |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 65 | * @throws BgpParseException while parsing As4Path |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 66 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 67 | public static As4Path read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 68 | 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 75 | validation.getLength()); |
| 76 | } |
| 77 | //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] | 78 | int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation |
| 79 | .getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 80 | ChannelBuffer data = tempCb.readBytes(len); |
| 81 | if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 82 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 83 | } |
| 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 VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 93 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 94 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 95 | } |
| 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 124 | public List<Integer> as4PathSeq() { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 125 | 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 Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 133 | public List<Integer> as4PathSet() { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 134 | 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 B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 168 | |
| 169 | @Override |
| 170 | public int compareTo(Object o) { |
| 171 | // TODO Auto-generated method stub |
| 172 | return 0; |
| 173 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 174 | } |