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; |
| 24 | import org.onosproject.bgpio.util.Validation; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | import com.google.common.base.Preconditions; |
| 30 | |
| 31 | /** |
| 32 | * Implementation of NextHop BGP Path Attribute. |
| 33 | */ |
| 34 | public class NextHop implements BGPValueType { |
| 35 | private static final Logger log = LoggerFactory.getLogger(NextHop.class); |
| 36 | public static final byte NEXTHOP_TYPE = 3; |
| 37 | public static final int TYPE_AND_LEN_AS_SHORT = 4; |
| 38 | public static final int TYPE_AND_LEN_AS_BYTE = 3; |
| 39 | |
| 40 | private boolean isNextHop = false; |
| 41 | private Ip4Address nextHop; |
| 42 | |
| 43 | /** |
| 44 | * Constructor to initialize parameters. |
| 45 | * |
| 46 | * @param nextHop nextHop address |
| 47 | */ |
| 48 | public NextHop(Ip4Address nextHop) { |
| 49 | this.nextHop = Preconditions.checkNotNull(nextHop); |
| 50 | this.isNextHop = true; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns whether next hop is present. |
| 55 | * |
| 56 | * @return whether next hop is present |
| 57 | */ |
| 58 | public boolean isNextHopSet() { |
| 59 | return this.isNextHop; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reads from ChannelBuffer and parses NextHop. |
| 64 | * |
| 65 | * @param cb ChannelBuffer |
| 66 | * @return object of NextHop |
| 67 | * @throws BGPParseException while parsing nexthop attribute |
| 68 | */ |
| 69 | public static NextHop read(ChannelBuffer cb) throws BGPParseException { |
| 70 | Ip4Address nextHop; |
| 71 | ChannelBuffer tempCb = cb.copy(); |
| 72 | Validation parseFlags = Validation.parseAttributeHeader(cb); |
| 73 | |
| 74 | if (cb.readableBytes() < parseFlags.getLength()) { |
| 75 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 76 | parseFlags.getLength()); |
| 77 | } |
| 78 | int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN_AS_SHORT : parseFlags |
| 79 | .getLength() + TYPE_AND_LEN_AS_BYTE; |
| 80 | ChannelBuffer data = tempCb.readBytes(len); |
| 81 | if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) { |
| 82 | throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data); |
| 83 | } |
| 84 | |
| 85 | //TODO: use Validation.toInetAddress once Validation is merged |
| 86 | InetAddress ipAddress = (InetAddress) cb.readBytes(parseFlags.getLength()); |
| 87 | if (ipAddress.isMulticastAddress()) { |
| 88 | throw new BGPParseException("Multicast address is not supported"); |
| 89 | } |
| 90 | |
| 91 | nextHop = Ip4Address.valueOf(ipAddress); |
| 92 | return new NextHop(nextHop); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return nexthop address. |
| 97 | * |
| 98 | * @return nexthop address |
| 99 | */ |
| 100 | public Ip4Address nextHop() { |
| 101 | return nextHop; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public short getType() { |
| 106 | return NEXTHOP_TYPE; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int write(ChannelBuffer cb) { |
| 111 | //Not required to be implemented now |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public int hashCode() { |
| 117 | return Objects.hash(nextHop); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean equals(Object obj) { |
| 122 | if (this == obj) { |
| 123 | return true; |
| 124 | } |
| 125 | if (obj instanceof NextHop) { |
| 126 | NextHop other = (NextHop) obj; |
| 127 | return Objects.equals(nextHop, other.nextHop); |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(getClass()) |
| 135 | .add("nextHop", nextHop) |
| 136 | .toString(); |
| 137 | } |
| 138 | } |