blob: 1542d33e9653714d5356ba1494646b46703c20ac [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
67 @Override
68 public short getType() {
69 return TYPE;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hash(sendReceive);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82 if (obj instanceof RpdCapabilityTlv) {
83 RpdCapabilityTlv other = (RpdCapabilityTlv) obj;
84 return Objects.equals(sendReceive, other.sendReceive);
85 }
86 return false;
87 }
88
89 @Override
90 public int write(ChannelBuffer cb) {
91 int iLenStartIndex = cb.writerIndex();
92 cb.writeByte(TYPE);
93 cb.writeByte(LENGTH);
94 cb.writeShort(afi);
95 cb.writeByte(sAfi);
96 cb.writeByte(sendReceive);
97 return cb.writerIndex() - iLenStartIndex;
98 }
99
100 /**
101 * Reads the channel buffer and returns object of RpdCapabilityTlv.
102 * @param cb type of channel buffer
103 * @return object of RpdCapabilityTlv
104 */
105 public static RpdCapabilityTlv read(ChannelBuffer cb) {
106 short afi = cb.readShort();
107 byte sAfi = cb.readByte();
108 return RpdCapabilityTlv.of(cb.readByte());
109 }
110
111 @Override
112 public String toString() {
113 return MoreObjects.toStringHelper(getClass())
114 .add("Type", TYPE)
115 .add("Length", LENGTH)
116 .add("afi", afi)
117 .add("safi", sAfi)
118 .add("sendReceive", sendReceive).toString();
119 }
120
121 @Override
122 public int compareTo(Object o) {
123 // TODO Auto-generated method stub
124 return 0;
125 }
126}