Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [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 com.google.common.base.MoreObjects; |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | /** |
| 26 | * Provides MultiProtocolExtnCapabilityTlv. |
| 27 | */ |
| 28 | public class MultiProtocolExtnCapabilityTlv implements BgpValueType { |
| 29 | |
| 30 | /* |
| 31 | 0 7 15 23 31 |
| 32 | +-------+-------+-------+-------+ |
| 33 | | AFI | Res | SAFI | |
| 34 | +-------+-------+-------+-------+ |
| 35 | |
| 36 | Multiprotocol Extensions CAPABILITY TLV format |
| 37 | REFERENCE : RFC 4760 |
| 38 | */ |
| 39 | protected static final Logger log = LoggerFactory |
| 40 | .getLogger(MultiProtocolExtnCapabilityTlv.class); |
| 41 | |
| 42 | public static final byte TYPE = 1; |
| 43 | public static final byte LENGTH = 4; |
| 44 | |
| 45 | private final short afi; |
| 46 | private final byte res; |
| 47 | private final byte safi; |
| 48 | |
| 49 | /** |
| 50 | * Constructor to initialize variables. |
| 51 | * @param afi Address Family Identifiers |
| 52 | * @param res reserved field |
| 53 | * @param safi Subsequent Address Family Identifier |
| 54 | */ |
| 55 | public MultiProtocolExtnCapabilityTlv(short afi, byte res, byte safi) { |
| 56 | this.afi = afi; |
| 57 | this.res = res; |
| 58 | this.safi = safi; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns object of MultiProtocolExtnCapabilityTlv. |
| 63 | * @param afi Address Family Identifiers |
| 64 | * @param res reserved field |
| 65 | * @param safi Subsequent Address Family Identifier |
| 66 | * @return object of MultiProtocolExtnCapabilityTlv |
| 67 | */ |
| 68 | public static MultiProtocolExtnCapabilityTlv of(short afi, byte res, |
| 69 | byte safi) { |
| 70 | return new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns afi Address Family Identifiers value. |
| 75 | * @return afi Address Family Identifiers value |
| 76 | */ |
| 77 | public short getAfi() { |
| 78 | return afi; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns res reserved field value. |
| 83 | * @return res reserved field value |
| 84 | */ |
| 85 | public byte getRes() { |
| 86 | return res; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns safi Subsequent Address Family Identifier value. |
| 91 | * @return safi Subsequent Address Family Identifier value |
| 92 | */ |
| 93 | public byte getSafi() { |
| 94 | return safi; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public short getType() { |
| 99 | return TYPE; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hash(afi, res, safi); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object obj) { |
| 109 | if (this == obj) { |
| 110 | return true; |
| 111 | } |
| 112 | if (obj instanceof MultiProtocolExtnCapabilityTlv) { |
| 113 | MultiProtocolExtnCapabilityTlv other = (MultiProtocolExtnCapabilityTlv) obj; |
| 114 | return Objects.equals(this.afi, other.afi) |
| 115 | && Objects.equals(this.res, other.res) |
| 116 | && Objects.equals(this.safi, other.safi); |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int write(ChannelBuffer cb) { |
| 123 | int iLenStartIndex = cb.writerIndex(); |
| 124 | cb.writeByte(TYPE); |
| 125 | cb.writeByte(LENGTH); |
| 126 | |
| 127 | // write afi |
| 128 | cb.writeShort(afi); |
| 129 | |
| 130 | // write res |
| 131 | cb.writeByte(res); |
| 132 | |
| 133 | // write safi |
| 134 | cb.writeByte(safi); |
| 135 | |
| 136 | return cb.writerIndex() - iLenStartIndex; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Reads from channel buffer and returns object of MultiprotocolCapabilityTlv. |
| 141 | * @param cb of type channel buffer |
| 142 | * @return object of MultiProtocolExtnCapabilityTlv |
| 143 | */ |
| 144 | public static BgpValueType read(ChannelBuffer cb) { |
| 145 | short afi = cb.readShort(); |
| 146 | byte res = cb.readByte(); |
| 147 | byte safi = cb.readByte(); |
| 148 | return new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | return MoreObjects.toStringHelper(getClass()) |
| 154 | .add("Type", TYPE) |
| 155 | .add("Length", LENGTH) |
| 156 | .add("AFI", afi) |
| 157 | .add("Reserved", res) |
| 158 | .add("SAFI", safi).toString(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public int compareTo(Object o) { |
| 163 | // TODO Auto-generated method stub |
| 164 | return 0; |
| 165 | } |
| 166 | } |