Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.routing.fpm.protocol; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import org.onlab.packet.DeserializationException; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.Ip6Address; |
| 23 | import org.onlab.packet.IpAddress; |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 24 | import org.jboss.netty.buffer.ChannelBuffer; |
| 25 | import org.jboss.netty.buffer.ChannelBuffers; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Gateway route attribute. |
| 29 | */ |
| 30 | public final class RouteAttributeGateway extends RouteAttribute { |
| 31 | |
| 32 | public static final int VALUE_LENGTH = 4; |
| 33 | |
| 34 | private final IpAddress gateway; |
| 35 | |
| 36 | /** |
| 37 | * Class constructor. |
| 38 | * |
| 39 | * @param length length |
| 40 | * @param type type |
| 41 | * @param gateway gateway address |
| 42 | */ |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 43 | private RouteAttributeGateway(int length, int type, IpAddress gateway) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 44 | super(length, type); |
| 45 | |
| 46 | this.gateway = gateway; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the gateway address. |
| 51 | * |
| 52 | * @return gateway address |
| 53 | */ |
| 54 | public IpAddress gateway() { |
| 55 | return gateway; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String toString() { |
| 60 | return MoreObjects.toStringHelper(getClass()) |
| 61 | .add("type", type()) |
| 62 | .add("length", length()) |
| 63 | .add("gateway", gateway) |
| 64 | .toString(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns a decoder for a gateway route attribute. |
| 69 | * |
| 70 | * @return gateway route attribute decoder |
| 71 | */ |
| 72 | public static RouteAttributeDecoder<RouteAttributeGateway> decoder() { |
| 73 | return (int length, int type, byte[] value) -> { |
| 74 | |
| 75 | IpAddress gateway; |
| 76 | if (value.length == Ip4Address.BYTE_LENGTH) { |
| 77 | gateway = IpAddress.valueOf(IpAddress.Version.INET, value); |
| 78 | } else if (value.length == Ip6Address.BYTE_LENGTH) { |
| 79 | gateway = IpAddress.valueOf(IpAddress.Version.INET6, value); |
| 80 | } else { |
| 81 | throw new DeserializationException("Invalid address length"); |
| 82 | } |
| 83 | |
| 84 | return new RouteAttributeGateway(length, type, gateway); |
| 85 | }; |
| 86 | } |
| 87 | |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Encode the RouteAttributeGateway contents into the ChannelBuffer. |
| 90 | * |
| 91 | * @param cb channelbuffer to be filled in |
| 92 | */ |
| 93 | @Override |
| 94 | public void encode(ChannelBuffer cb) { |
| 95 | |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 96 | super.encode(cb); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 97 | |
| 98 | ChannelBuffer buffer = ChannelBuffers.copiedBuffer(gateway.toOctets()); |
| 99 | if (length() == Ip6Address.BYTE_LENGTH + |
| 100 | RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) { |
| 101 | cb.writeBytes(buffer, Ip6Address.BYTE_LENGTH); |
| 102 | } else if (length() == Ip4Address.BYTE_LENGTH + |
| 103 | RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) { |
| 104 | cb.writeBytes(buffer, Ip4Address.BYTE_LENGTH); |
| 105 | } else { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 106 | throw new IllegalArgumentException("Gateway address length incorrect!"); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Kalhee Kim | 715dd73 | 2018-01-23 14:39:56 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * Returns a new RouteAttributeGateway builder. |
| 112 | * |
| 113 | * @return RouteAttributeGateway builder |
| 114 | */ |
| 115 | public static Builder builder() { |
| 116 | return new Builder(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * RouteAttributeGateway Builder. |
| 121 | */ |
| 122 | public static final class Builder extends RouteAttribute.Builder<Builder> { |
| 123 | |
| 124 | private IpAddress gateway = null; |
| 125 | |
| 126 | /** |
| 127 | * Hide class constructor. |
| 128 | */ |
| 129 | private Builder() {} |
| 130 | |
| 131 | /** |
| 132 | * Override abstract method. |
| 133 | */ |
| 134 | @Override |
| 135 | public Builder getThis() { |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets gateway for RouteAttributeGateway that will be built. |
| 141 | * |
| 142 | * @param gateway to use for built RouteAttributeGateway |
| 143 | * @return this builder |
| 144 | */ |
| 145 | public Builder gateway(IpAddress gateway) { |
| 146 | this.gateway = gateway; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Builds the RouteAttributeGateway. |
| 152 | * |
| 153 | * @return RouteAttributeGateway reference |
| 154 | */ |
| 155 | public RouteAttributeGateway build() { |
| 156 | return new RouteAttributeGateway(length, type, gateway); |
| 157 | } |
| 158 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 159 | } |