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 | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 43 | public 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 | |
| 96 | cb.writeShort(Short.reverseBytes((short) length())); |
| 97 | cb.writeShort(Short.reverseBytes((short) type())); |
| 98 | |
| 99 | ChannelBuffer buffer = ChannelBuffers.copiedBuffer(gateway.toOctets()); |
| 100 | if (length() == Ip6Address.BYTE_LENGTH + |
| 101 | RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) { |
| 102 | cb.writeBytes(buffer, Ip6Address.BYTE_LENGTH); |
| 103 | } else if (length() == Ip4Address.BYTE_LENGTH + |
| 104 | RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) { |
| 105 | cb.writeBytes(buffer, Ip4Address.BYTE_LENGTH); |
| 106 | } else { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame^] | 107 | throw new IllegalArgumentException("Gateway address length incorrect!"); |
Kalhee Kim | ba36606 | 2017-11-07 16:32:09 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 110 | } |