blob: 00dffb58a311d32c93f1769650119afd2c8f3dd0 [file] [log] [blame]
Thejaswi N K53beb002015-10-13 20:21:34 +05301/*
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 */
16package org.onosproject.bgpio.types.attr;
17
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.Ip4Address;
22import org.onosproject.bgpio.exceptions.BGPParseException;
23import org.onosproject.bgpio.types.BGPErrorType;
24import org.onosproject.bgpio.types.BGPValueType;
25import org.onosproject.bgpio.util.Validation;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.google.common.base.MoreObjects;
30
31/**
32 * Implements BGP attribute node router ID.
33 */
34public class BgpAttrRouterIdV4 implements BGPValueType {
35
36 protected static final Logger log = LoggerFactory
37 .getLogger(BgpAttrRouterIdV4.class);
38
39 public short sType;
40
41 /* IPv4 Router-ID of Node */
42 private Ip4Address ip4RouterId;
43
44 /**
45 * Constructor to initialize the value.
46 *
47 * @param ip4RouterId IPV4 address of router
48 * @param sType TLV type
49 */
50 BgpAttrRouterIdV4(Ip4Address ip4RouterId, short sType) {
51 this.ip4RouterId = ip4RouterId;
52 this.sType = sType;
53 }
54
55 /**
56 * Reads the IPv4 Router-ID.
57 *
58 * @param cb ChannelBuffer
59 * @return object of BgpAttrRouterIdV4
60 * @throws BGPParseException while parsing BgpAttrNodeRouterId
61 */
62 public static BgpAttrRouterIdV4 read(ChannelBuffer cb, short sType)
63 throws BGPParseException {
64 byte[] ipBytes;
65 Ip4Address ip4RouterId;
66
67 short lsAttrLength = cb.readShort();
68
69 if (4 != lsAttrLength) {
70 Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR,
71 BGPErrorType.ATTRIBUTE_LENGTH_ERROR,
72 lsAttrLength);
73 }
74
75 ipBytes = new byte[lsAttrLength];
76 cb.readBytes(ipBytes);
77 ip4RouterId = Ip4Address.valueOf(ipBytes);
78 return new BgpAttrRouterIdV4(ip4RouterId, sType);
79 }
80
81 /**
82 * Returns the IPV4 router ID.
83 *
84 * @return Router ID
85 */
86 Ip4Address getAttrRouterId() {
87 return ip4RouterId;
88 }
89
90 @Override
91 public short getType() {
92 return sType;
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(ip4RouterId);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105
106 if (obj instanceof BgpAttrRouterIdV4) {
107 BgpAttrRouterIdV4 other = (BgpAttrRouterIdV4) obj;
108 return Objects.equals(ip4RouterId, other.ip4RouterId);
109 }
110 return false;
111 }
112
113 @Override
114 public int write(ChannelBuffer cb) {
115 // TODO Auto-generated method stub
116 return 0;
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(getClass()).omitNullValues()
122 .add("ip4RouterId", ip4RouterId).toString();
123 }
124}