blob: fa7f6d4f902977b0adadf8b3712942250d7cd3af [file] [log] [blame]
Shashikanth VHb58cfd52016-04-21 16:45:50 +05301/*
2 * Copyright 2016-present 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 */
16package org.onosproject.bgpio.types;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.util.Constants;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides implementation of BGP route policy distribution capability tlv.
29 */
30public class RpdCapabilityTlv implements BgpValueType {
31
32 protected static final Logger log = LoggerFactory
33 .getLogger(RpdCapabilityTlv.class);
34
35 public static final byte TYPE = (byte) 129;
36 public static final byte LENGTH = 4;
37 private short afi = Constants.AFI_FLOWSPEC_RPD_VALUE;
38 private byte sAfi = Constants.SAFI_FLOWSPEC_RPD_VALUE;
39
40 private final byte sendReceive;
41
42 /**
43 * Creates instance of route policy distribution capability.
44 * @param sendReceive value indicate wherether flow route is only for receive or send or both.
45 */
46 public RpdCapabilityTlv(byte sendReceive) {
47 this.sendReceive = sendReceive;
48 }
49
50 /**
51 * Creates instance of RpdCapabilityTlv.
52 * @param sendReceive value indicate wherether flow route is only for receive or send or both.
53 * @return object of RpdCapabilityTlv
54 */
55 public static RpdCapabilityTlv of(final byte sendReceive) {
56 return new RpdCapabilityTlv(sendReceive);
57 }
58
59 /**
60 * Returns value of send receive field of route policy distribution capability.
61 * @return send receive value of route policy distribution capability
62 */
63 public byte sendReceive() {
64 return sendReceive;
65 }
66
Shashikanth VH26fd38a2016-04-26 18:11:37 +053067 /**
68 * Returns address family identifier value.
69 * @return afi address family identifier value
70 */
71 public short getAfi() {
72 return afi;
73 }
74
75 /**
76 * Returns subsequent address family identifier value.
77 * @return safi subsequent address family identifier value
78 */
79 public byte getSafi() {
80 return sAfi;
81 }
82
Shashikanth VHb58cfd52016-04-21 16:45:50 +053083 @Override
84 public short getType() {
85 return TYPE;
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(sendReceive);
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj) {
96 return true;
97 }
98 if (obj instanceof RpdCapabilityTlv) {
99 RpdCapabilityTlv other = (RpdCapabilityTlv) obj;
100 return Objects.equals(sendReceive, other.sendReceive);
101 }
102 return false;
103 }
104
105 @Override
106 public int write(ChannelBuffer cb) {
107 int iLenStartIndex = cb.writerIndex();
108 cb.writeByte(TYPE);
109 cb.writeByte(LENGTH);
110 cb.writeShort(afi);
111 cb.writeByte(sAfi);
112 cb.writeByte(sendReceive);
113 return cb.writerIndex() - iLenStartIndex;
114 }
115
116 /**
117 * Reads the channel buffer and returns object of RpdCapabilityTlv.
118 * @param cb type of channel buffer
119 * @return object of RpdCapabilityTlv
120 */
121 public static RpdCapabilityTlv read(ChannelBuffer cb) {
122 short afi = cb.readShort();
123 byte sAfi = cb.readByte();
124 return RpdCapabilityTlv.of(cb.readByte());
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass())
130 .add("Type", TYPE)
131 .add("Length", LENGTH)
132 .add("afi", afi)
133 .add("safi", sAfi)
134 .add("sendReceive", sendReceive).toString();
135 }
136
137 @Override
138 public int compareTo(Object o) {
139 // TODO Auto-generated method stub
140 return 0;
141 }
142}