Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.lisp.extensions; |
| 18 | |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 19 | import com.google.common.collect.Maps; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.mapping.addresses.ASMappingAddress; |
| 22 | import org.onosproject.mapping.addresses.DNMappingAddress; |
| 23 | import org.onosproject.mapping.addresses.EthMappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 24 | import org.onosproject.mapping.addresses.ExtensionMappingAddress; |
| 25 | import org.onosproject.mapping.addresses.ExtensionMappingAddressType; |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 26 | import org.onosproject.mapping.addresses.IPMappingAddress; |
| 27 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 28 | import org.onosproject.net.flow.AbstractExtension; |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 30 | |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 31 | import java.util.Map; |
| 32 | import java.util.Objects; |
| 33 | |
| 34 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 35 | import static org.onosproject.mapping.addresses.ExtensionMappingAddressType |
| 36 | .ExtensionMappingAddressTypes.LIST_ADDRESS; |
| 37 | |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 38 | /** |
| 39 | * Implementation of LISP list address. |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 40 | * 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 Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 43 | */ |
| 44 | public class LispListAddress extends AbstractExtension |
| 45 | implements ExtensionMappingAddress { |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 46 | |
| 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 Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 98 | @Override |
| 99 | public ExtensionMappingAddressType type() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 100 | return LIST_ADDRESS.type(); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public byte[] serialize() { |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 105 | |
| 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 Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void deserialize(byte[] data) { |
| 116 | |
Jian Li | 3ddd553 | 2017-03-18 02:58:23 +0900 | [diff] [blame^] | 117 | 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 Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 186 | } |
| 187 | } |