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.Objects; |
| 19 | |
| 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 | 69dca71 | 2015-11-20 16:08:53 +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; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | |
| 27 | /** |
| 28 | * Provides Implementation of mandatory BGP Origin path attribute. |
| 29 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | public class Origin implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Enum to provide ORIGIN types. |
| 34 | */ |
| 35 | public enum ORIGINTYPE { |
| 36 | IGP(0), EGP(1), INCOMPLETE(2); |
| 37 | int value; |
| 38 | /** |
| 39 | * Assign val with the value as the ORIGIN type. |
| 40 | * |
| 41 | * @param val ORIGIN type |
| 42 | */ |
| 43 | ORIGINTYPE(int val) { |
| 44 | value = val; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns value of ORIGIN type. |
| 49 | * |
| 50 | * @return ORIGIN type |
| 51 | */ |
| 52 | public byte getType() { |
| 53 | return (byte) value; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static final byte ORIGIN_TYPE = 1; |
| 58 | public static final byte ORIGIN_VALUE_LEN = 1; |
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 | */ |
| 87 | public ORIGINTYPE origin() { |
| 88 | if (this.origin == 0) { |
| 89 | return ORIGINTYPE.IGP; |
| 90 | } else if (this.origin == 1) { |
| 91 | return ORIGINTYPE.EGP; |
| 92 | } else { |
| 93 | return ORIGINTYPE.INCOMPLETE; |
| 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(); |
Priyanka B | 69dca71 | 2015-11-20 16:08:53 +0530 | [diff] [blame] | 121 | if ((originValue != ORIGINTYPE.INCOMPLETE.value) && (originValue != ORIGINTYPE.IGP.value) && |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 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) { |
| 135 | //Not required to Implement as of now |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public int hashCode() { |
| 141 | return Objects.hash(origin); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean equals(Object obj) { |
| 146 | if (this == obj) { |
| 147 | return true; |
| 148 | } |
| 149 | if (obj instanceof Origin) { |
| 150 | Origin other = (Origin) obj; |
| 151 | return Objects.equals(origin, other.origin); |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public String toString() { |
| 158 | return MoreObjects.toStringHelper(getClass()) |
| 159 | .add("origin", origin) |
| 160 | .toString(); |
| 161 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 162 | |
| 163 | @Override |
| 164 | public int compareTo(Object o) { |
| 165 | // TODO Auto-generated method stub |
| 166 | return 0; |
| 167 | } |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 168 | } |