blob: bba2c17487d0dd829e9a7a665df7904bfa8fefe4 [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;
20import org.onlab.util.KryoNamespace;
21import org.onosproject.mapping.addresses.ASMappingAddress;
22import org.onosproject.mapping.addresses.DNMappingAddress;
23import org.onosproject.mapping.addresses.EthMappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090024import org.onosproject.mapping.addresses.ExtensionMappingAddress;
25import org.onosproject.mapping.addresses.ExtensionMappingAddressType;
Jian Li3ddd5532017-03-18 02:58:23 +090026import org.onosproject.mapping.addresses.IPMappingAddress;
27import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090028import org.onosproject.net.flow.AbstractExtension;
Jian Li3ddd5532017-03-18 02:58:23 +090029import org.onosproject.store.serializers.KryoNamespaces;
Jian Li3ff327a2017-03-14 17:02:19 +090030
Jian Li3ddd5532017-03-18 02:58:23 +090031import java.util.Map;
32import java.util.Objects;
33
34import static com.google.common.base.MoreObjects.toStringHelper;
Jian Li299bc1d2017-03-17 19:17:40 +090035import static org.onosproject.mapping.addresses.ExtensionMappingAddressType
36 .ExtensionMappingAddressTypes.LIST_ADDRESS;
37
Jian Li3ff327a2017-03-14 17:02:19 +090038/**
39 * Implementation of LISP list address.
Jian Li299bc1d2017-03-17 19:17:40 +090040 * When header translation between IPv4 and IPv6 is desirable a LISP Canonical
41 * Address can use the AFI List Type to carry a variable number of AFIs in one
42 * LCAF AFI.
Jian Li3ff327a2017-03-14 17:02:19 +090043 */
44public class LispListAddress extends AbstractExtension
45 implements ExtensionMappingAddress {
Jian Li3ddd5532017-03-18 02:58:23 +090046
47 private static final String IPV4 = "ipv4";
48 private static final String IPV6 = "ipv6";
49
50 private MappingAddress ipv4;
51 private MappingAddress ipv6;
52
53 private final KryoNamespace appKryo = new KryoNamespace.Builder()
54 .register(KryoNamespaces.API)
55 .register(MappingAddress.class)
56 .register(MappingAddress.Type.class)
57 .register(IPMappingAddress.class)
58 .register(ASMappingAddress.class)
59 .register(DNMappingAddress.class)
60 .register(EthMappingAddress.class)
61 .build();
62
63 /**
64 * Default constructor.
65 */
66 public LispListAddress() {
67 }
68
69 /**
70 * Creates an instance with initialized parameters.
71 *
72 * @param ipv4 IPv4 address
73 * @param ipv6 IPv6 address
74 */
75 private LispListAddress(MappingAddress ipv4, MappingAddress ipv6) {
76 this.ipv4 = ipv4;
77 this.ipv6 = ipv6;
78 }
79
80 /**
81 * Obtains IPv4 address.
82 *
83 * @return IPv4 address
84 */
85 public MappingAddress getIpv4() {
86 return ipv4;
87 }
88
89 /**
90 * Obtains IPv6 address.
91 *
92 * @return IPv6 address
93 */
94 public MappingAddress getIpv6() {
95 return ipv6;
96 }
97
Jian Li3ff327a2017-03-14 17:02:19 +090098 @Override
99 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +0900100 return LIST_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +0900101 }
102
103 @Override
104 public byte[] serialize() {
Jian Li3ddd5532017-03-18 02:58:23 +0900105
106 Map<String, Object> parameterMap = Maps.newHashMap();
107
108 parameterMap.put(IPV4, ipv4);
109 parameterMap.put(IPV6, ipv6);
110
111 return appKryo.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +0900112 }
113
114 @Override
115 public void deserialize(byte[] data) {
116
Jian Li3ddd5532017-03-18 02:58:23 +0900117 Map<String, Object> parameterMap = appKryo.deserialize(data);
118
119 this.ipv4 = (MappingAddress) parameterMap.get(IPV4);
120 this.ipv6 = (MappingAddress) parameterMap.get(IPV6);
121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hash(ipv4, ipv6);
126 }
127
128 @Override
129 public boolean equals(Object obj) {
130 if (this == obj) {
131 return true;
132 }
133 if (obj instanceof LispListAddress) {
134 LispListAddress that = (LispListAddress) obj;
135 return Objects.equals(ipv4, that.ipv4) &&
136 Objects.equals(ipv6, that.ipv6);
137 }
138 return false;
139 }
140
141 @Override
142 public String toString() {
143 return toStringHelper(type().toString())
144 .add("ipv4", ipv4)
145 .add("ipv6", ipv6)
146 .toString();
147 }
148
149 /**
150 * A builder for building LispListAddress.
151 */
152 public static final class Builder {
153 private MappingAddress ipv4;
154 private MappingAddress ipv6;
155
156 /**
157 * Sets IPv4 address.
158 *
159 * @param ipv4 IPv4 address
160 * @return Builder object
161 */
162 public Builder withIpv4(MappingAddress ipv4) {
163 this.ipv4 = ipv4;
164 return this;
165 }
166
167 /**
168 * Sets IPv6 address.
169 *
170 * @param ipv6 IPv6 address
171 * @return Builder object
172 */
173 public Builder withIpv6(MappingAddress ipv6) {
174 this.ipv6 = ipv6;
175 return this;
176 }
177
178 /**
179 * Builds LispListAddress instance.
180 *
181 * @return LispListAddress instance
182 */
183 public LispListAddress build() {
184 return new LispListAddress(ipv4, ipv6);
185 }
Jian Li3ff327a2017-03-14 17:02:19 +0900186 }
187}