Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import java.net.InetAddress; |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.bgpio.exceptions.BGPParseException; |
Priyanka B | 69dca71 | 2015-11-20 16:08:53 +0530 | [diff] [blame] | 24 | import org.onosproject.bgpio.util.Constants; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 25 | import org.onosproject.bgpio.util.Validation; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 26 | |
| 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.base.Preconditions; |
| 29 | |
| 30 | /** |
| 31 | * Implementation of NextHop BGP Path Attribute. |
| 32 | */ |
| 33 | public class NextHop implements BGPValueType { |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 34 | public static final byte NEXTHOP_TYPE = 3; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 35 | |
| 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 |
| 63 | * @throws BGPParseException while parsing nexthop attribute |
| 64 | */ |
| 65 | public static NextHop read(ChannelBuffer cb) throws BGPParseException { |
| 66 | Ip4Address nextHop; |
| 67 | ChannelBuffer tempCb = cb.copy(); |
| 68 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 69 | |
| 70 | if (cb.readableBytes() < parseFlags.getLength()) { |
| 71 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 72 | parseFlags.getLength()); |
| 73 | } |
Priyanka B | 69dca71 | 2015-11-20 16:08:53 +0530 | [diff] [blame] | 74 | int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags |
| 75 | .getLength() + Constants.TYPE_AND_LEN_AS_BYTE; |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 76 | ChannelBuffer data = tempCb.readBytes(len); |
| 77 | if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
| 78 | throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
| 79 | } |
| 80 | |
Priyanka B | 69dca71 | 2015-11-20 16:08:53 +0530 | [diff] [blame] | 81 | InetAddress ipAddress = Validation.toInetAddress(parseFlags.getLength(), cb); |
Priyanka B | 8584395 | 2015-10-14 11:56:30 +0530 | [diff] [blame] | 82 | if (ipAddress.isMulticastAddress()) { |
| 83 | throw new BGPParseException("Multicast address is not supported"); |
| 84 | } |
| 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 | } |