blob: 3b1f5cd9c953b79390c1c0a6fc3f5f1eb0b7f70d [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart3930f632015-10-19 12:12:51 -07003 *
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
17package org.onosproject.routing.fpm.protocol;
18
19import com.google.common.base.MoreObjects;
20import org.onlab.packet.DeserializationException;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip6Address;
23import org.onlab.packet.IpAddress;
Kalhee Kimba366062017-11-07 16:32:09 +000024import org.jboss.netty.buffer.ChannelBuffer;
25import org.jboss.netty.buffer.ChannelBuffers;
Jonathan Hart3930f632015-10-19 12:12:51 -070026
27/**
28 * Gateway route attribute.
29 */
30public 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 Kimba366062017-11-07 16:32:09 +000043 public RouteAttributeGateway(int length, int type, IpAddress gateway) {
Jonathan Hart3930f632015-10-19 12:12:51 -070044 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 Kimba366062017-11-07 16:32:09 +000088 /**
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 Milkey986a47a2018-01-25 11:38:51 -0800107 throw new IllegalArgumentException("Gateway address length incorrect!");
Kalhee Kimba366062017-11-07 16:32:09 +0000108 }
109 }
Jonathan Hart3930f632015-10-19 12:12:51 -0700110}