blob: 691345695c8aefa4f03a03a927983cc64d6ec72a [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
24
25/**
26 * Destination address route attribute.
27 */
28public final class RouteAttributeDst extends RouteAttribute {
29
30 private final IpAddress dstAddress;
31
32 /**
33 * Class constructor.
34 *
35 * @param length length
36 * @param type type
37 * @param dstAddress destination address
38 */
39 private RouteAttributeDst(int length, int type, IpAddress dstAddress) {
40 super(length, type);
41
42 this.dstAddress = dstAddress;
43 }
44
45 /**
46 * Returns the destination IP address.
47 *
48 * @return destination IP address
49 */
50 public IpAddress dstAddress() {
51 return dstAddress;
52 }
53
54 @Override
55 public String toString() {
56 return MoreObjects.toStringHelper(getClass())
57 .add("type", type())
58 .add("length", length())
59 .add("dstAddress", dstAddress)
60 .toString();
61 }
62
63 /**
64 * Returns a decoder for a destination address route attribute.
65 *
66 * @return destination address route attribute decoder
67 */
68 public static RouteAttributeDecoder<RouteAttributeDst> decoder() {
69 return (int length, int type, byte[] value) -> {
70
71 IpAddress dstAddress;
72 if (value.length == Ip4Address.BYTE_LENGTH) {
73 dstAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
74 } else if (value.length == Ip6Address.BYTE_LENGTH) {
75 dstAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
76 } else {
77 throw new DeserializationException("Invalid address length");
78 }
79
80 return new RouteAttributeDst(length, type, dstAddress);
81 };
82 }
83}