blob: f7e3d3b888b850faf70b26be64c45273b0487427 [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 * Destination address route attribute.
29 */
30public final class RouteAttributeDst extends RouteAttribute {
31
32 private final IpAddress dstAddress;
33
34 /**
35 * Class constructor.
36 *
37 * @param length length
38 * @param type type
39 * @param dstAddress destination address
40 */
Kalhee Kim715dd732018-01-23 14:39:56 +000041 private RouteAttributeDst(int length, int type, IpAddress dstAddress) {
Jonathan Hart3930f632015-10-19 12:12:51 -070042 super(length, type);
43
44 this.dstAddress = dstAddress;
45 }
46
47 /**
48 * Returns the destination IP address.
49 *
50 * @return destination IP address
51 */
52 public IpAddress dstAddress() {
53 return dstAddress;
54 }
55
56 @Override
57 public String toString() {
58 return MoreObjects.toStringHelper(getClass())
59 .add("type", type())
60 .add("length", length())
61 .add("dstAddress", dstAddress)
62 .toString();
63 }
64
65 /**
66 * Returns a decoder for a destination address route attribute.
67 *
68 * @return destination address route attribute decoder
69 */
70 public static RouteAttributeDecoder<RouteAttributeDst> decoder() {
71 return (int length, int type, byte[] value) -> {
72
73 IpAddress dstAddress;
74 if (value.length == Ip4Address.BYTE_LENGTH) {
75 dstAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
76 } else if (value.length == Ip6Address.BYTE_LENGTH) {
77 dstAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
78 } else {
79 throw new DeserializationException("Invalid address length");
80 }
81
82 return new RouteAttributeDst(length, type, dstAddress);
83 };
84 }
Kalhee Kimba366062017-11-07 16:32:09 +000085
86 /**
87 * Encode the RouteAttributeDst contents into the ChannelBuffer.
88 *
89 * @param cb channelbuffer to be filled in
90 */
91 @Override
92 public void encode(ChannelBuffer cb) {
93
Kalhee Kim715dd732018-01-23 14:39:56 +000094 super.encode(cb);
Kalhee Kimba366062017-11-07 16:32:09 +000095
96 ChannelBuffer buffer = ChannelBuffers.copiedBuffer(dstAddress.toOctets());
97 if (length() == Ip6Address.BYTE_LENGTH +
98 RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
99 cb.writeBytes(buffer, Ip6Address.BYTE_LENGTH);
100 } else if (length() == Ip4Address.BYTE_LENGTH +
101 RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
102 cb.writeBytes(buffer, Ip4Address.BYTE_LENGTH);
103 } else {
Ray Milkey986a47a2018-01-25 11:38:51 -0800104 throw new IllegalArgumentException("Dst address length incorrect!");
Kalhee Kimba366062017-11-07 16:32:09 +0000105 }
106 }
Kalhee Kim715dd732018-01-23 14:39:56 +0000107
108 /**
109 * Returns a new RouteAttributeDst builder.
110 *
111 * @return RouteAttributeDst builder
112 */
113 public static Builder builder() {
114 return new Builder();
115 }
116
117 /**
118 * RouteAttributeDst Builder.
119 */
120 public static final class Builder extends RouteAttribute.Builder<Builder> {
121
122 private IpAddress dstAddress = null;
123
124 /**
125 * Hide class constructor.
126 */
127 private Builder() {}
128
129 /**
130 * Override abstract method.
131 */
132 @Override
133 public Builder getThis() {
134 return this;
135 }
136
137 /**
138 * Sets dstAddress for RouteAttributeDst that will be built.
139 *
140 * @param dstAddress to use for built RouteAttributeDst
141 * @return this builder
142 */
143 public Builder dstAddress(IpAddress dstAddress) {
144 this.dstAddress = dstAddress;
145 return this;
146 }
147
148 /**
149 * Builds the RouteAttributeDst.
150 *
151 * @return RouteAttributeDst reference
152 */
153 public RouteAttributeDst build() {
154 return new RouteAttributeDst(length, type, dstAddress);
155 }
156 }
Jonathan Hart3930f632015-10-19 12:12:51 -0700157}