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 Med BGP Path Attribute. |
| 29 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | public class Med implements BgpValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 31 | public static final byte MED_TYPE = 4; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 32 | public static final byte MED_MAX_LEN = 4; |
Shashikanth VH | 15de0d9 | 2016-02-08 15:11:50 +0530 | [diff] [blame] | 33 | public static final byte FLAGS = (byte) 0x80; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 34 | |
| 35 | private int med; |
| 36 | |
| 37 | /** |
| 38 | * Constructor to initialize med. |
| 39 | * |
| 40 | * @param med MULTI_EXIT_DISC value |
| 41 | */ |
| 42 | public Med(int med) { |
| 43 | this.med = med; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns Med value. |
| 48 | * |
| 49 | * @return Med value |
| 50 | */ |
| 51 | public int med() { |
| 52 | return this.med; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Reads the channel buffer and returns object of Med. |
| 57 | * |
| 58 | * @param cb ChannelBuffer |
| 59 | * @return object of Med |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 60 | * @throws BgpParseException while parsing Med path 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 Med read(ChannelBuffer cb) throws BgpParseException { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 63 | int med; |
| 64 | ChannelBuffer tempCb = cb.copy(); |
| 65 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 66 | |
| 67 | if ((parseFlags.getLength() > MED_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 69 | parseFlags.getLength()); |
| 70 | } |
Priyanka B | e9ed729 | 2015-11-18 22:12:29 +0530 | [diff] [blame] | 71 | int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags |
| 72 | .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() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
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 | med = cb.readInt(); |
| 79 | return new Med(med); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public short getType() { |
| 84 | return MED_TYPE; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | return Objects.hash(med); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
| 94 | if (this == obj) { |
| 95 | return true; |
| 96 | } |
| 97 | if (obj instanceof Med) { |
| 98 | Med other = (Med) obj; |
| 99 | return Objects.equals(med, other.med); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(getClass()) |
| 107 | .add("med", med) |
| 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(med()); |
| 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 | } |