blob: ecf36d3690fba003219abe51ce71eb37a6110db2 [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 Kim715dd732018-01-23 14:39:56 +000043 private 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
Kalhee Kim715dd732018-01-23 14:39:56 +000096 super.encode(cb);
Kalhee Kimba366062017-11-07 16:32:09 +000097
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 Milkey986a47a2018-01-25 11:38:51 -0800106 throw new IllegalArgumentException("Gateway address length incorrect!");
Kalhee Kimba366062017-11-07 16:32:09 +0000107 }
108 }
Kalhee Kim715dd732018-01-23 14:39:56 +0000109
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 Hart3930f632015-10-19 12:12:51 -0700159}