Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | 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 | 69dca71 | 2015-11-20 16:08:53 +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; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 23 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 24 | import java.util.Objects; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Provides Implementation of mandatory BGP Origin path attribute. |
| 28 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 29 | public class Origin implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Enum to provide ORIGIN types. |
| 33 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 34 | public enum OriginType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 35 | IGP(0), EGP(1), INCOMPLETE(2); |
| 36 | int value; |
| 37 | /** |
| 38 | * Assign val with the value as the ORIGIN type. |
| 39 | * |
| 40 | * @param val ORIGIN type |
| 41 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 42 | OriginType(int val) { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 43 | value = val; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns value of ORIGIN type. |
| 48 | * |
| 49 | * @return ORIGIN type |
| 50 | */ |
| 51 | public byte getType() { |
| 52 | return (byte) value; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static final byte ORIGIN_TYPE = 1; |
| 57 | public static final byte ORIGIN_VALUE_LEN = 1; |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 58 | public static final byte FLAGS = (byte) 0x40; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 59 | |
| 60 | private boolean isOrigin = false; |
| 61 | private byte origin; |
| 62 | |
| 63 | /** |
| 64 | * Constructor to initialize parameters. |
| 65 | * |
| 66 | * @param origin origin value |
| 67 | */ |
| 68 | public Origin(byte origin) { |
| 69 | this.origin = origin; |
| 70 | this.isOrigin = true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns true if origin attribute is present otherwise false. |
| 75 | * |
| 76 | * @return whether origin is present or not |
| 77 | */ |
| 78 | public boolean isOriginSet() { |
| 79 | return this.isOrigin; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns type of Origin in Enum values. |
| 84 | * |
| 85 | * @return type of Origin in Enum values |
| 86 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 87 | public OriginType origin() { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 88 | if (this.origin == 0) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 89 | return OriginType.IGP; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 90 | } else if (this.origin == 1) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 91 | return OriginType.EGP; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 92 | } else { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 93 | return OriginType.INCOMPLETE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Reads from ChannelBuffer and parses Origin. |
| 99 | * |
| 100 | * @param cb ChannelBuffer |
| 101 | * @return object of Origin |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 102 | * @throws BgpParseException while parsing Origin path attribute |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 103 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 104 | public static Origin read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 105 | ChannelBuffer tempCb = cb.copy(); |
| 106 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 107 | |
Priyanka B | 69dca71 | 2015-11-20 16:08:53 +0530 | [diff] [blame] | 108 | int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags |
| 109 | .getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 110 | ChannelBuffer data = tempCb.readBytes(len); |
| 111 | if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (cb.readableBytes() < parseFlags.getLength())) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 112 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 113 | parseFlags.getLength()); |
| 114 | } |
| 115 | if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | byte originValue; |
| 120 | originValue = cb.readByte(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 121 | if ((originValue != OriginType.INCOMPLETE.value) && (originValue != OriginType.IGP.value) && |
| 122 | (originValue != OriginType.EGP.value)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 123 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.INVALID_ORIGIN_ATTRIBUTE, data); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 124 | } |
| 125 | return new Origin(originValue); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public short getType() { |
| 130 | return ORIGIN_TYPE; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int write(ChannelBuffer cb) { |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 135 | int iLenStartIndex = cb.writerIndex(); |
| 136 | cb.writeByte(FLAGS); |
| 137 | cb.writeByte(ORIGIN_TYPE); |
| 138 | cb.writeByte(1); |
| 139 | cb.writeByte(origin().value); |
| 140 | return cb.writerIndex() - iLenStartIndex; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int hashCode() { |
| 145 | return Objects.hash(origin); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean equals(Object obj) { |
| 150 | if (this == obj) { |
| 151 | return true; |
| 152 | } |
| 153 | if (obj instanceof Origin) { |
| 154 | Origin other = (Origin) obj; |
| 155 | return Objects.equals(origin, other.origin); |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public String toString() { |
| 162 | return MoreObjects.toStringHelper(getClass()) |
| 163 | .add("origin", origin) |
| 164 | .toString(); |
| 165 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 166 | |
| 167 | @Override |
| 168 | public int compareTo(Object o) { |
| 169 | // TODO Auto-generated method stub |
| 170 | return 0; |
| 171 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 172 | } |