blob: caf27e3f07a36a4df48f19a2b1e8ff0edac01369 [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;
Priyanka B85843952015-10-14 11:56:30 +053035
36 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 /**
50 * Returns whether next hop is present.
51 *
52 * @return whether next hop is present
53 */
54 public boolean isNextHopSet() {
55 return this.isNextHop;
56 }
57
58 /**
59 * Reads from ChannelBuffer and parses NextHop.
60 *
61 * @param cb ChannelBuffer
62 * @return object of NextHop
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053063 * @throws BgpParseException while parsing nexthop attribute
Priyanka B85843952015-10-14 11:56:30 +053064 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053065 public static NextHop read(ChannelBuffer cb) throws BgpParseException {
Priyanka B85843952015-10-14 11:56:30 +053066 Ip4Address nextHop;
67 ChannelBuffer tempCb = cb.copy();
68 Validation parseFlags = Validation.parseAttributeHeader(cb);
69
70 if (cb.readableBytes() < parseFlags.getLength()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
Priyanka B85843952015-10-14 11:56:30 +053072 parseFlags.getLength());
73 }
Priyanka B69dca712015-11-20 16:08:53 +053074 int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags
75 .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
Priyanka B85843952015-10-14 11:56:30 +053076 ChannelBuffer data = tempCb.readBytes(len);
77 if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
Priyanka B85843952015-10-14 11:56:30 +053079 }
80
Priyanka B69dca712015-11-20 16:08:53 +053081 InetAddress ipAddress = Validation.toInetAddress(parseFlags.getLength(), cb);
Priyanka B85843952015-10-14 11:56:30 +053082 if (ipAddress.isMulticastAddress()) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053083 throw new BgpParseException("Multicast address is not supported");
Priyanka B85843952015-10-14 11:56:30 +053084 }
85
86 nextHop = Ip4Address.valueOf(ipAddress);
87 return new NextHop(nextHop);
88 }
89
90 /**
91 * Return nexthop address.
92 *
93 * @return nexthop address
94 */
95 public Ip4Address nextHop() {
96 return nextHop;
97 }
98
99 @Override
100 public short getType() {
101 return NEXTHOP_TYPE;
102 }
103
104 @Override
105 public int write(ChannelBuffer cb) {
106 //Not required to be implemented now
107 return 0;
108 }
109
110 @Override
111 public int hashCode() {
112 return Objects.hash(nextHop);
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj instanceof NextHop) {
121 NextHop other = (NextHop) obj;
122 return Objects.equals(nextHop, other.nextHop);
123 }
124 return false;
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(getClass())
130 .add("nextHop", nextHop)
131 .toString();
132 }
133}