blob: 7e65dc000f535edac791e3db12725f66e2d3ef30 [file] [log] [blame]
Priyanka B85843952015-10-14 11:56:30 +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;
17
18import java.net.InetAddress;
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onlab.packet.Ip4Address;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka B69dca712015-11-20 16:08:53 +053024import org.onosproject.bgpio.util.Constants;
Priyanka B85843952015-10-14 11:56:30 +053025import org.onosproject.bgpio.util.Validation;
Priyanka B85843952015-10-14 11:56:30 +053026
27import com.google.common.base.MoreObjects;
28import com.google.common.base.Preconditions;
29
30/**
31 * Implementation of NextHop BGP Path Attribute.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public class NextHop implements BgpValueType {
Priyanka B85843952015-10-14 11:56:30 +053034 public static final byte NEXTHOP_TYPE = 3;
Shashikanth VHc53a4132016-02-11 18:33:34 +053035 public static final byte FLAGS = (byte) 0x40;
Priyanka B85843952015-10-14 11:56:30 +053036 private boolean isNextHop = false;
37 private Ip4Address nextHop;
38
39 /**
40 * Constructor to initialize parameters.
41 *
42 * @param nextHop nextHop address
43 */
44 public NextHop(Ip4Address nextHop) {
45 this.nextHop = Preconditions.checkNotNull(nextHop);
46 this.isNextHop = true;
47 }
48
49 /**
Shashikanth VHc53a4132016-02-11 18:33:34 +053050 * Constructor to initialize default parameters.
51 *
52 */
53 public NextHop() {
54 this.nextHop = null;
55 }
56
57 /**
Priyanka B85843952015-10-14 11:56:30 +053058 * Returns whether next hop is present.
59 *
60 * @return whether next hop is present
61 */
62 public boolean isNextHopSet() {
63 return this.isNextHop;
64 }
65
66 /**
67 * Reads from ChannelBuffer and parses NextHop.
68 *
69 * @param cb ChannelBuffer
70 * @return object of NextHop
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 * @throws BgpParseException while parsing nexthop attribute
Priyanka B85843952015-10-14 11:56:30 +053072 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 public static NextHop read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053074 Ip4Address nextHop;
75 ChannelBuffer tempCb = cb.copy();
76 Validation parseFlags = Validation.parseAttributeHeader(cb);
77
78 if (cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053080 parseFlags.getLength());
81 }
Priyanka B69dca712015-11-20 16:08:53 +053082 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
83 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053084 ChannelBuffer data = tempCb.readBytes(len);
85 if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053086 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053087 }
88
Priyanka B69dca712015-11-20 16:08:53 +053089 InetAddress ipAddress = Validation.toInetAddress(parseFlags.getLength(), cb);
Priyanka B85843952015-10-14 11:56:30 +053090 if (ipAddress.isMulticastAddress()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053091 throw new BgpParseException("Multicast address is not supported");
Priyanka B85843952015-10-14 11:56:30 +053092 }
93
94 nextHop = Ip4Address.valueOf(ipAddress);
95 return new NextHop(nextHop);
96 }
97
98 /**
99 * Return nexthop address.
100 *
101 * @return nexthop address
102 */
103 public Ip4Address nextHop() {
104 return nextHop;
105 }
106
107 @Override
108 public short getType() {
109 return NEXTHOP_TYPE;
110 }
111
112 @Override
113 public int write(ChannelBuffer cb) {
Shashikanth VHc53a4132016-02-11 18:33:34 +0530114 int iLenStartIndex = cb.writerIndex();
115 cb.writeByte(FLAGS);
116 cb.writeByte(getType());
117 if (!isNextHopSet()) {
118 cb.writeByte(0);
119 } else {
120 cb.writeInt(nextHop.toInt());
121 }
122
123 return cb.writerIndex() - iLenStartIndex;
Priyanka B85843952015-10-14 11:56:30 +0530124 }
125
126 @Override
127 public int hashCode() {
128 return Objects.hash(nextHop);
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 if (this == obj) {
134 return true;
135 }
136 if (obj instanceof NextHop) {
137 NextHop other = (NextHop) obj;
138 return Objects.equals(nextHop, other.nextHop);
139 }
140 return false;
141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(getClass())
146 .add("nextHop", nextHop)
147 .toString();
148 }
Priyanka B02040732015-11-29 11:30:29 +0530149
150 @Override
151 public int compareTo(Object o) {
152 // TODO Auto-generated method stub
153 return 0;
154 }
Priyanka B85843952015-10-14 11:56:30 +0530155}