Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 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; |
| 24 | |
| 25 | /** |
| 26 | * Gateway route attribute. |
| 27 | */ |
| 28 | public final class RouteAttributeGateway extends RouteAttribute { |
| 29 | |
| 30 | public static final int VALUE_LENGTH = 4; |
| 31 | |
| 32 | private final IpAddress gateway; |
| 33 | |
| 34 | /** |
| 35 | * Class constructor. |
| 36 | * |
| 37 | * @param length length |
| 38 | * @param type type |
| 39 | * @param gateway gateway address |
| 40 | */ |
| 41 | private RouteAttributeGateway(int length, int type, IpAddress gateway) { |
| 42 | super(length, type); |
| 43 | |
| 44 | this.gateway = gateway; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the gateway address. |
| 49 | * |
| 50 | * @return gateway address |
| 51 | */ |
| 52 | public IpAddress gateway() { |
| 53 | return gateway; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String toString() { |
| 58 | return MoreObjects.toStringHelper(getClass()) |
| 59 | .add("type", type()) |
| 60 | .add("length", length()) |
| 61 | .add("gateway", gateway) |
| 62 | .toString(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a decoder for a gateway route attribute. |
| 67 | * |
| 68 | * @return gateway route attribute decoder |
| 69 | */ |
| 70 | public static RouteAttributeDecoder<RouteAttributeGateway> decoder() { |
| 71 | return (int length, int type, byte[] value) -> { |
| 72 | |
| 73 | IpAddress gateway; |
| 74 | if (value.length == Ip4Address.BYTE_LENGTH) { |
| 75 | gateway = IpAddress.valueOf(IpAddress.Version.INET, value); |
| 76 | } else if (value.length == Ip6Address.BYTE_LENGTH) { |
| 77 | gateway = IpAddress.valueOf(IpAddress.Version.INET6, value); |
| 78 | } else { |
| 79 | throw new DeserializationException("Invalid address length"); |
| 80 | } |
| 81 | |
| 82 | return new RouteAttributeGateway(length, type, gateway); |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | } |