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