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 | 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; |
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 LocalPref BGP Path Attribute. |
| 29 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | public class LocalPref implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 31 | public static final byte LOCAL_PREF_TYPE = 5; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 32 | public static final byte LOCAL_PREF_MAX_LEN = 4; |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 33 | public static final byte FLAGS = (byte) 0x40; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 34 | |
| 35 | private int localPref; |
| 36 | |
| 37 | /** |
| 38 | * Constructor to initialize LocalPref. |
| 39 | * |
| 40 | * @param localPref local preference |
| 41 | */ |
| 42 | public LocalPref(int localPref) { |
| 43 | this.localPref = localPref; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns local preference value. |
| 48 | * |
| 49 | * @return local preference value |
| 50 | */ |
| 51 | public int localPref() { |
| 52 | return this.localPref; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Reads the channel buffer and returns object of LocalPref. |
| 57 | * |
| 58 | * @param cb channelBuffer |
| 59 | * @return object of LocalPref |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 60 | * @throws BgpParseException while parsing localPref attribute |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 61 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 62 | public static LocalPref read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 63 | int localPref; |
| 64 | ChannelBuffer tempCb = cb.copy(); |
| 65 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 66 | if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 67 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 68 | parseFlags.getLength()); |
| 69 | } |
| 70 | |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 71 | int len = parseFlags.isShort() ? parseFlags.getLength() + |
| 72 | Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 73 | ChannelBuffer data = tempCb.readBytes(len); |
| 74 | if (parseFlags.getFirstBit()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 75 | throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | localPref = cb.readInt(); |
| 79 | return new LocalPref(localPref); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public short getType() { |
| 84 | return LOCAL_PREF_TYPE; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | return Objects.hash(localPref); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
| 94 | if (this == obj) { |
| 95 | return true; |
| 96 | } |
| 97 | if (obj instanceof LocalPref) { |
| 98 | LocalPref other = (LocalPref) obj; |
| 99 | return Objects.equals(localPref, other.localPref); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(getClass()) |
| 107 | .add("localPref", localPref) |
| 108 | .toString(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int write(ChannelBuffer cb) { |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 113 | int iLenStartIndex = cb.writerIndex(); |
| 114 | cb.writeByte(FLAGS); |
| 115 | cb.writeByte(getType()); |
| 116 | cb.writeByte(4); |
| 117 | cb.writeInt(localPref()); |
| 118 | return cb.writerIndex() - iLenStartIndex; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 119 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 120 | |
| 121 | @Override |
| 122 | public int compareTo(Object o) { |
| 123 | // TODO Auto-generated method stub |
| 124 | return 0; |
| 125 | } |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 126 | } |