blob: 4ea887827432cc1114546d55a8487c7f4f534aa0 [file] [log] [blame]
Jian Li3ff327a2017-03-14 17:02:19 +09001/*
2 * Copyright 2017-present 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
17package org.onosproject.drivers.lisp.extensions;
18
Jian Li3ddd5532017-03-18 02:58:23 +090019import com.google.common.collect.Maps;
Jian Li3ff327a2017-03-14 17:02:19 +090020import org.onosproject.mapping.addresses.ExtensionMappingAddress;
21import org.onosproject.mapping.addresses.ExtensionMappingAddressType;
Jian Li3ddd5532017-03-18 02:58:23 +090022import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090023import org.onosproject.net.flow.AbstractExtension;
24
Jian Li3ddd5532017-03-18 02:58:23 +090025import java.util.Map;
26import java.util.Objects;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li299bc1d2017-03-17 19:17:40 +090029import static org.onosproject.mapping.addresses.ExtensionMappingAddressType
Jian Li924995b2017-03-18 12:34:46 +090030 .ExtensionMappingAddressTypes.LIST_ADDRESS;
Jian Li299bc1d2017-03-17 19:17:40 +090031
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
33 * Implementation of LISP list address.
Jian Li299bc1d2017-03-17 19:17:40 +090034 * When header translation between IPv4 and IPv6 is desirable a LISP Canonical
35 * Address can use the AFI List Type to carry a variable number of AFIs in one
36 * LCAF AFI.
Jian Li3ff327a2017-03-14 17:02:19 +090037 */
38public class LispListAddress extends AbstractExtension
39 implements ExtensionMappingAddress {
Jian Li3ddd5532017-03-18 02:58:23 +090040
41 private static final String IPV4 = "ipv4";
42 private static final String IPV6 = "ipv6";
43
44 private MappingAddress ipv4;
45 private MappingAddress ipv6;
46
Jian Li3ddd5532017-03-18 02:58:23 +090047 /**
48 * Default constructor.
49 */
50 public LispListAddress() {
51 }
52
53 /**
54 * Creates an instance with initialized parameters.
55 *
56 * @param ipv4 IPv4 address
57 * @param ipv6 IPv6 address
58 */
59 private LispListAddress(MappingAddress ipv4, MappingAddress ipv6) {
60 this.ipv4 = ipv4;
61 this.ipv6 = ipv6;
62 }
63
64 /**
65 * Obtains IPv4 address.
66 *
67 * @return IPv4 address
68 */
69 public MappingAddress getIpv4() {
70 return ipv4;
71 }
72
73 /**
74 * Obtains IPv6 address.
75 *
76 * @return IPv6 address
77 */
78 public MappingAddress getIpv6() {
79 return ipv6;
80 }
81
Jian Li3ff327a2017-03-14 17:02:19 +090082 @Override
83 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +090084 return LIST_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +090085 }
86
87 @Override
88 public byte[] serialize() {
Jian Li3ddd5532017-03-18 02:58:23 +090089
90 Map<String, Object> parameterMap = Maps.newHashMap();
91
92 parameterMap.put(IPV4, ipv4);
93 parameterMap.put(IPV6, ipv6);
94
Jian Li924995b2017-03-18 12:34:46 +090095 return APP_KRYO.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +090096 }
97
98 @Override
99 public void deserialize(byte[] data) {
100
Jian Li924995b2017-03-18 12:34:46 +0900101 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ddd5532017-03-18 02:58:23 +0900102
103 this.ipv4 = (MappingAddress) parameterMap.get(IPV4);
104 this.ipv6 = (MappingAddress) parameterMap.get(IPV6);
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(ipv4, ipv6);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
117 if (obj instanceof LispListAddress) {
118 LispListAddress that = (LispListAddress) obj;
119 return Objects.equals(ipv4, that.ipv4) &&
120 Objects.equals(ipv6, that.ipv6);
121 }
122 return false;
123 }
124
125 @Override
126 public String toString() {
127 return toStringHelper(type().toString())
128 .add("ipv4", ipv4)
129 .add("ipv6", ipv6)
130 .toString();
131 }
132
133 /**
134 * A builder for building LispListAddress.
135 */
136 public static final class Builder {
137 private MappingAddress ipv4;
138 private MappingAddress ipv6;
139
140 /**
141 * Sets IPv4 address.
142 *
143 * @param ipv4 IPv4 address
144 * @return Builder object
145 */
146 public Builder withIpv4(MappingAddress ipv4) {
147 this.ipv4 = ipv4;
148 return this;
149 }
150
151 /**
152 * Sets IPv6 address.
153 *
154 * @param ipv6 IPv6 address
155 * @return Builder object
156 */
157 public Builder withIpv6(MappingAddress ipv6) {
158 this.ipv6 = ipv6;
159 return this;
160 }
161
162 /**
163 * Builds LispListAddress instance.
164 *
165 * @return LispListAddress instance
166 */
167 public LispListAddress build() {
168 return new LispListAddress(ipv4, ipv6);
169 }
Jian Li3ff327a2017-03-14 17:02:19 +0900170 }
171}