blob: 832cd5925e660dd1a6fd9ba874c69d54c78e05cf [file] [log] [blame]
Madan Jampanic27b6b22016-02-05 11:36:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampanic27b6b22016-02-05 11:36:31 -08003 *
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 */
16package org.onosproject.bgpio.types;
17
18import com.google.common.base.MoreObjects;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Shashikanth VHb650bfa2016-04-18 12:54:03 +053022import org.onosproject.bgpio.util.Constants;
Madan Jampanic27b6b22016-02-05 11:36:31 -080023
24import java.util.Objects;
25
26/**
27 * Provides MultiProtocolExtnCapabilityTlv.
28 */
29public 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 VHb650bfa2016-04-18 12:54:03 +053049 private final byte rpdSendReceive;
Madan Jampanic27b6b22016-02-05 11:36:31 -080050
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 VHb650bfa2016-04-18 12:54:03 +053061 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 Jampanic27b6b22016-02-05 11:36:31 -080079 }
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 VHb650bfa2016-04-18 12:54:03 +0530143 boolean isFsRpd = false;
Madan Jampanic27b6b22016-02-05 11:36:31 -0800144 int iLenStartIndex = cb.writerIndex();
145 cb.writeByte(TYPE);
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530146
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 Jampanic27b6b22016-02-05 11:36:31 -0800153
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 VHb650bfa2016-04-18 12:54:03 +0530163 if (isFsRpd) {
164 // write Send/Receive (1 octet)
165 cb.writeByte(rpdSendReceive);
166 }
167
Madan Jampanic27b6b22016-02-05 11:36:31 -0800168 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 VHb650bfa2016-04-18 12:54:03 +0530180
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 Jampanic27b6b22016-02-05 11:36:31 -0800184 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}