Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +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.attr; |
| 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; |
| 22 | import org.onosproject.bgpio.types.BgpErrorType; |
| 23 | import org.onosproject.bgpio.types.BgpValueType; |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.util.Validation; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Implements BGP link state Default TE metric link attribute. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public class BgpLinkAttrTeDefaultMetric implements BgpValueType { |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 34 | |
| 35 | protected static final Logger log = LoggerFactory |
| 36 | .getLogger(BgpLinkAttrTeDefaultMetric.class); |
| 37 | |
| 38 | public static final int ATTRLINK_TEDEFAULTMETRIC = 1092; |
| 39 | public static final int TE_DATA_LEN = 4; |
| 40 | |
| 41 | /* TE Default Metric */ |
| 42 | private int linkTeMetric; |
| 43 | |
| 44 | /** |
| 45 | * Constructor to initialize the value. |
| 46 | * |
| 47 | * @param linkTeMetric TE default metric |
| 48 | * |
| 49 | */ |
| 50 | public BgpLinkAttrTeDefaultMetric(int linkTeMetric) { |
| 51 | this.linkTeMetric = linkTeMetric; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns object of this class with specified values. |
| 56 | * |
| 57 | * @param linkTeMetric TE default metric |
| 58 | * @return object of BgpLinkAttrTeDefaultMetric |
| 59 | */ |
| 60 | public static BgpLinkAttrTeDefaultMetric of(final int linkTeMetric) { |
| 61 | return new BgpLinkAttrTeDefaultMetric(linkTeMetric); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Reads the BGP link attributes of TE default metric. |
| 66 | * |
| 67 | * @param cb Channel buffer |
| 68 | * @return object of type BgpLinkAttrTeDefaultMetric |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 69 | * @throws BgpParseException while parsing BgpLinkAttrTeDefaultMetric |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 70 | */ |
| 71 | public static BgpLinkAttrTeDefaultMetric read(ChannelBuffer cb) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | throws BgpParseException { |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 73 | int linkTeMetric; |
| 74 | |
| 75 | short lsAttrLength = cb.readShort(); |
| 76 | |
| 77 | if ((lsAttrLength != TE_DATA_LEN) |
| 78 | || (cb.readableBytes() < lsAttrLength)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 79 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, |
| 80 | BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 81 | lsAttrLength); |
| 82 | } |
| 83 | |
| 84 | linkTeMetric = cb.readInt(); |
| 85 | |
| 86 | return new BgpLinkAttrTeDefaultMetric(linkTeMetric); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the TE default metrics. |
| 91 | * |
| 92 | * @return link default metric |
| 93 | */ |
| 94 | public int attrLinkDefTeMetric() { |
| 95 | return linkTeMetric; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public short getType() { |
| 100 | return ATTRLINK_TEDEFAULTMETRIC; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int hashCode() { |
| 105 | return Objects.hash(linkTeMetric); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean equals(Object obj) { |
| 110 | if (this == obj) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | if (obj instanceof BgpLinkAttrTeDefaultMetric) { |
| 115 | BgpLinkAttrTeDefaultMetric other = (BgpLinkAttrTeDefaultMetric) obj; |
| 116 | return Objects.equals(linkTeMetric, other.linkTeMetric); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int write(ChannelBuffer cb) { |
| 123 | // TODO This will be implemented in the next version |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return MoreObjects.toStringHelper(getClass()) |
| 130 | .add("linkTEMetric", linkTeMetric).toString(); |
| 131 | } |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 132 | |
| 133 | @Override |
| 134 | public int compareTo(Object o) { |
| 135 | // TODO Auto-generated method stub |
| 136 | return 0; |
| 137 | } |
Thejaswi N K | 0fc6134 | 2015-11-11 23:47:25 +0530 | [diff] [blame] | 138 | } |