Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [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; |
| 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; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 22 | import org.onosproject.bgpio.util.Constants; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 23 | |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | /** |
| 27 | * Provides MultiProtocolExtnCapabilityTlv. |
| 28 | */ |
| 29 | public class MultiProtocolExtnCapabilityTlv implements BgpValueType { |
| 30 | |
| 31 | /* |
| 32 | 0 7 15 23 31 |
| 33 | +-------+-------+-------+-------+ |
| 34 | | AFI | Res | SAFI | |
| 35 | +-------+-------+-------+-------+ |
| 36 | |
| 37 | Multiprotocol Extensions CAPABILITY TLV format |
| 38 | REFERENCE : RFC 4760 |
| 39 | */ |
| 40 | protected static final Logger log = LoggerFactory |
| 41 | .getLogger(MultiProtocolExtnCapabilityTlv.class); |
| 42 | |
| 43 | public static final byte TYPE = 1; |
| 44 | public static final byte LENGTH = 4; |
| 45 | |
| 46 | private final short afi; |
| 47 | private final byte res; |
| 48 | private final byte safi; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 49 | private final byte rpdSendReceive; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Constructor to initialize variables. |
| 53 | * @param afi Address Family Identifiers |
| 54 | * @param res reserved field |
| 55 | * @param safi Subsequent Address Family Identifier |
| 56 | */ |
| 57 | public MultiProtocolExtnCapabilityTlv(short afi, byte res, byte safi) { |
| 58 | this.afi = afi; |
| 59 | this.res = res; |
| 60 | this.safi = safi; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 61 | this.rpdSendReceive = Constants.RPD_CAPABILITY_SEND_VALUE; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructor to initialize variables. |
| 66 | * @param afi Address Family Identifiers |
| 67 | * @param res reserved field |
| 68 | * @param safi Subsequent Address Family Identifier |
| 69 | * @param rpdSendReceive indicates whether the sender is |
| 70 | (a) willing to receive Route Policies via BGP FLowSpec from its peer (value 1). |
| 71 | (b) would like to send Route Policies via BGP FLowSpec to its peer (value 2). |
| 72 | (c) both (value 3). |
| 73 | */ |
| 74 | public MultiProtocolExtnCapabilityTlv(short afi, byte res, byte safi, byte rpdSendReceive) { |
| 75 | this.afi = afi; |
| 76 | this.res = res; |
| 77 | this.safi = safi; |
| 78 | this.rpdSendReceive = rpdSendReceive; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns object of MultiProtocolExtnCapabilityTlv. |
| 83 | * @param afi Address Family Identifiers |
| 84 | * @param res reserved field |
| 85 | * @param safi Subsequent Address Family Identifier |
| 86 | * @return object of MultiProtocolExtnCapabilityTlv |
| 87 | */ |
| 88 | public static MultiProtocolExtnCapabilityTlv of(short afi, byte res, |
| 89 | byte safi) { |
| 90 | return new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns afi Address Family Identifiers value. |
| 95 | * @return afi Address Family Identifiers value |
| 96 | */ |
| 97 | public short getAfi() { |
| 98 | return afi; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns res reserved field value. |
| 103 | * @return res reserved field value |
| 104 | */ |
| 105 | public byte getRes() { |
| 106 | return res; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns safi Subsequent Address Family Identifier value. |
| 111 | * @return safi Subsequent Address Family Identifier value |
| 112 | */ |
| 113 | public byte getSafi() { |
| 114 | return safi; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public short getType() { |
| 119 | return TYPE; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Objects.hash(afi, res, safi); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public boolean equals(Object obj) { |
| 129 | if (this == obj) { |
| 130 | return true; |
| 131 | } |
| 132 | if (obj instanceof MultiProtocolExtnCapabilityTlv) { |
| 133 | MultiProtocolExtnCapabilityTlv other = (MultiProtocolExtnCapabilityTlv) obj; |
| 134 | return Objects.equals(this.afi, other.afi) |
| 135 | && Objects.equals(this.res, other.res) |
| 136 | && Objects.equals(this.safi, other.safi); |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int write(ChannelBuffer cb) { |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 143 | boolean isFsRpd = false; |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 144 | int iLenStartIndex = cb.writerIndex(); |
| 145 | cb.writeByte(TYPE); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 146 | |
| 147 | if ((afi == Constants.AFI_FLOWSPEC_RPD_VALUE) && (safi == Constants.SAFI_FLOWSPEC_RPD_VALUE)) { |
| 148 | cb.writeByte(LENGTH + 1); |
| 149 | isFsRpd = true; |
| 150 | } else { |
| 151 | cb.writeByte(LENGTH); |
| 152 | } |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 153 | |
| 154 | // write afi |
| 155 | cb.writeShort(afi); |
| 156 | |
| 157 | // write res |
| 158 | cb.writeByte(res); |
| 159 | |
| 160 | // write safi |
| 161 | cb.writeByte(safi); |
| 162 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 163 | if (isFsRpd) { |
| 164 | // write Send/Receive (1 octet) |
| 165 | cb.writeByte(rpdSendReceive); |
| 166 | } |
| 167 | |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 168 | return cb.writerIndex() - iLenStartIndex; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Reads from channel buffer and returns object of MultiprotocolCapabilityTlv. |
| 173 | * @param cb of type channel buffer |
| 174 | * @return object of MultiProtocolExtnCapabilityTlv |
| 175 | */ |
| 176 | public static BgpValueType read(ChannelBuffer cb) { |
| 177 | short afi = cb.readShort(); |
| 178 | byte res = cb.readByte(); |
| 179 | byte safi = cb.readByte(); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame^] | 180 | |
| 181 | if ((afi == Constants.AFI_FLOWSPEC_RPD_VALUE) && (safi == Constants.SAFI_FLOWSPEC_RPD_VALUE)) { |
| 182 | return new MultiProtocolExtnCapabilityTlv(afi, res, safi, cb.readByte()); |
| 183 | } |
Madan Jampani | c27b6b2 | 2016-02-05 11:36:31 -0800 | [diff] [blame] | 184 | return new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public String toString() { |
| 189 | return MoreObjects.toStringHelper(getClass()) |
| 190 | .add("Type", TYPE) |
| 191 | .add("Length", LENGTH) |
| 192 | .add("AFI", afi) |
| 193 | .add("Reserved", res) |
| 194 | .add("SAFI", safi).toString(); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public int compareTo(Object o) { |
| 199 | // TODO Auto-generated method stub |
| 200 | return 0; |
| 201 | } |
| 202 | } |