blob: c3debba63aee76b681ebd699a88636a97f8124ac [file] [log] [blame]
Thejaswi N K53beb002015-10-13 20:21:34 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thejaswi N K53beb002015-10-13 20:21:34 +05303 *
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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053022import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.types.BgpErrorType;
24import org.onosproject.bgpio.types.BgpValueType;
Thejaswi N K53beb002015-10-13 20:21:34 +053025import 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053034public final class BgpAttrRouterIdV4 implements BgpValueType {
Thejaswi N K53beb002015-10-13 20:21:34 +053035
36 protected static final Logger log = LoggerFactory
37 .getLogger(BgpAttrRouterIdV4.class);
38
Thejaswi N K76c892a2015-11-02 16:54:31 +053039 private final short sType;
Thejaswi N K53beb002015-10-13 20:21:34 +053040
41 /* IPv4 Router-ID of Node */
Thejaswi N K76c892a2015-11-02 16:54:31 +053042 private final Ip4Address ip4RouterId;
Thejaswi N K53beb002015-10-13 20:21:34 +053043
44 /**
45 * Constructor to initialize the value.
46 *
47 * @param ip4RouterId IPV4 address of router
48 * @param sType TLV type
49 */
Thejaswi N K76c892a2015-11-02 16:54:31 +053050 private BgpAttrRouterIdV4(Ip4Address ip4RouterId, short sType) {
Thejaswi N K53beb002015-10-13 20:21:34 +053051 this.ip4RouterId = ip4RouterId;
52 this.sType = sType;
53 }
54
55 /**
Thejaswi N K76c892a2015-11-02 16:54:31 +053056 * Returns object of this class with specified values.
57 *
58 * @param ip4RouterId IPv4 address
59 * @param sType Type of this TLV
60 * @return object of BgpAttrRouterIdV4
61 */
62 public static BgpAttrRouterIdV4 of(final Ip4Address ip4RouterId,
63 final short sType) {
64 return new BgpAttrRouterIdV4(ip4RouterId, sType);
65 }
66
67 /**
Thejaswi N K53beb002015-10-13 20:21:34 +053068 * Reads the IPv4 Router-ID.
69 *
70 * @param cb ChannelBuffer
Thejaswi N K76c892a2015-11-02 16:54:31 +053071 * @param sType tag type
Thejaswi N K53beb002015-10-13 20:21:34 +053072 * @return object of BgpAttrRouterIdV4
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 * @throws BgpParseException while parsing BgpAttrRouterIdV4
Thejaswi N K53beb002015-10-13 20:21:34 +053074 */
75 public static BgpAttrRouterIdV4 read(ChannelBuffer cb, short sType)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053076 throws BgpParseException {
Thejaswi N K53beb002015-10-13 20:21:34 +053077 short lsAttrLength = cb.readShort();
78
Thejaswi N K76c892a2015-11-02 16:54:31 +053079 if ((lsAttrLength != 4) || (cb.readableBytes() < lsAttrLength)) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053080 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
81 BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Thejaswi N K53beb002015-10-13 20:21:34 +053082 lsAttrLength);
83 }
84
Thejaswi N K76c892a2015-11-02 16:54:31 +053085 byte[] ipBytes = new byte[lsAttrLength];
86 cb.readBytes(ipBytes, 0, lsAttrLength);
87 Ip4Address ip4RouterId = Ip4Address.valueOf(ipBytes);
88 return BgpAttrRouterIdV4.of(ip4RouterId, sType);
Thejaswi N K53beb002015-10-13 20:21:34 +053089 }
90
91 /**
92 * Returns the IPV4 router ID.
93 *
94 * @return Router ID
95 */
Thejaswi N K76c892a2015-11-02 16:54:31 +053096 public Ip4Address attrRouterId() {
Thejaswi N K53beb002015-10-13 20:21:34 +053097 return ip4RouterId;
98 }
99
100 @Override
101 public short getType() {
102 return sType;
103 }
104
105 @Override
106 public int hashCode() {
107 return Objects.hash(ip4RouterId);
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj) {
113 return true;
114 }
115
116 if (obj instanceof BgpAttrRouterIdV4) {
117 BgpAttrRouterIdV4 other = (BgpAttrRouterIdV4) obj;
118 return Objects.equals(ip4RouterId, other.ip4RouterId);
119 }
120 return false;
121 }
122
123 @Override
124 public int write(ChannelBuffer cb) {
Thejaswi N K76c892a2015-11-02 16:54:31 +0530125 // TODO This will be implemented in the next version
Thejaswi N K53beb002015-10-13 20:21:34 +0530126 return 0;
127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(getClass()).omitNullValues()
132 .add("ip4RouterId", ip4RouterId).toString();
133 }
Priyanka B02040732015-11-29 11:30:29 +0530134
135 @Override
136 public int compareTo(Object o) {
137 // TODO Auto-generated method stub
138 return 0;
139 }
Thejaswi N K53beb002015-10-13 20:21:34 +0530140}