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 | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 20 | import org.onosproject.mapping.addresses.ExtensionMappingAddress; |
| 21 | import org.onosproject.mapping.addresses.ExtensionMappingAddressType; |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 22 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 23 | import org.onosproject.net.flow.AbstractExtension; |
| 24 | |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 29 | import static org.onosproject.mapping.addresses.ExtensionMappingAddressType |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 30 | .ExtensionMappingAddressTypes.MULTICAST_ADDRESS; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 31 | |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 32 | /** |
| 33 | * Implementation of LISP multicast address. |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 34 | * The intent of this type of unicast replication is to deliver packets to |
| 35 | * multiple ETRs at receiver LISP multicast sites. |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 36 | */ |
| 37 | public class LispMulticastAddress extends AbstractExtension |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 38 | implements ExtensionMappingAddress { |
| 39 | |
| 40 | private static final String INSTANCE_ID = "instanceId"; |
| 41 | private static final String SRC_MASK_LENGTH = "srcMaskLength"; |
| 42 | private static final String GRP_MASK_LENGTH = "grpMaskLength"; |
| 43 | private static final String SRC_ADDRESS = "srcAddress"; |
| 44 | private static final String GRP_ADDRESS = "grpAddress"; |
| 45 | |
| 46 | private int instanceId; |
| 47 | private byte srcMaskLength; |
| 48 | private byte grpMaskLength; |
| 49 | private MappingAddress srcAddress; |
| 50 | private MappingAddress grpAddress; |
| 51 | |
| 52 | /** |
| 53 | * Default constructor. |
| 54 | */ |
| 55 | public LispMulticastAddress() { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates an instance with initialized parameters. |
| 60 | * |
| 61 | * @param instanceId instance identifier |
| 62 | * @param srcMaskLength source mask length |
| 63 | * @param grpMaskLength group mask length |
| 64 | * @param srcAddress source address |
| 65 | * @param grpAddress group address |
| 66 | */ |
| 67 | private LispMulticastAddress(int instanceId, byte srcMaskLength, |
| 68 | byte grpMaskLength, MappingAddress srcAddress, |
| 69 | MappingAddress grpAddress) { |
| 70 | this.instanceId = instanceId; |
| 71 | this.srcMaskLength = srcMaskLength; |
| 72 | this.grpMaskLength = grpMaskLength; |
| 73 | this.srcAddress = srcAddress; |
| 74 | this.grpAddress = grpAddress; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Obtains instance identifier. |
| 79 | * |
| 80 | * @return instance identifier |
| 81 | */ |
| 82 | public int getInstanceId() { |
| 83 | return instanceId; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Obtains source mask length. |
| 88 | * |
| 89 | * @return source mask length |
| 90 | */ |
| 91 | public byte getSrcMaskLength() { |
| 92 | return srcMaskLength; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Obtains group mask length. |
| 97 | * |
| 98 | * @return group mask length |
| 99 | */ |
| 100 | public byte getGrpMaskLength() { |
| 101 | return grpMaskLength; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Obtains source address. |
| 106 | * |
| 107 | * @return source address |
| 108 | */ |
| 109 | public MappingAddress getSrcAddress() { |
| 110 | return srcAddress; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Obtains group address. |
| 115 | * |
| 116 | * @return group address |
| 117 | */ |
| 118 | public MappingAddress getGrpAddress() { |
| 119 | return grpAddress; |
| 120 | } |
| 121 | |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 122 | @Override |
| 123 | public ExtensionMappingAddressType type() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 124 | return MULTICAST_ADDRESS.type(); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public byte[] serialize() { |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 129 | Map<String, Object> parameterMap = Maps.newHashMap(); |
| 130 | |
| 131 | parameterMap.put(INSTANCE_ID, instanceId); |
| 132 | parameterMap.put(SRC_MASK_LENGTH, srcMaskLength); |
| 133 | parameterMap.put(GRP_MASK_LENGTH, grpMaskLength); |
| 134 | parameterMap.put(SRC_ADDRESS, srcAddress); |
| 135 | parameterMap.put(GRP_ADDRESS, grpAddress); |
| 136 | |
| 137 | return APP_KRYO.serialize(parameterMap); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void deserialize(byte[] data) { |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 142 | Map<String, Object> parameterMap = APP_KRYO.deserialize(data); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 143 | |
Jian Li | e47e21a | 2017-03-18 13:39:51 +0900 | [diff] [blame] | 144 | this.instanceId = (int) parameterMap.get(INSTANCE_ID); |
| 145 | this.srcMaskLength = (byte) parameterMap.get(SRC_MASK_LENGTH); |
| 146 | this.grpMaskLength = (byte) parameterMap.get(GRP_MASK_LENGTH); |
| 147 | this.srcAddress = (MappingAddress) parameterMap.get(SRC_ADDRESS); |
| 148 | this.grpAddress = (MappingAddress) parameterMap.get(GRP_ADDRESS); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public int hashCode() { |
| 153 | return Objects.hash(instanceId, srcMaskLength, grpMaskLength, |
| 154 | srcAddress, grpAddress); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean equals(Object obj) { |
| 159 | if (this == obj) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | if (obj instanceof LispMulticastAddress) { |
| 164 | final LispMulticastAddress other = (LispMulticastAddress) obj; |
| 165 | return Objects.equals(this.instanceId, other.instanceId) && |
| 166 | Objects.equals(this.srcMaskLength, other.srcMaskLength) && |
| 167 | Objects.equals(this.grpMaskLength, other.grpMaskLength) && |
| 168 | Objects.equals(this.srcAddress, other.srcAddress) && |
| 169 | Objects.equals(this.grpAddress, other.grpAddress); |
| 170 | |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public String toString() { |
| 177 | return toStringHelper(this) |
| 178 | .add("instance ID", instanceId) |
| 179 | .add("source mask length", srcMaskLength) |
| 180 | .add("group mask length", grpMaskLength) |
| 181 | .add("source address", srcAddress) |
| 182 | .add("group address", grpAddress) |
| 183 | .toString(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * A builder for building LispMulticastAddress. |
| 188 | */ |
| 189 | public static final class Builder { |
| 190 | private int instanceId; |
| 191 | private byte srcMaskLength; |
| 192 | private byte grpMaskLength; |
| 193 | private MappingAddress srcAddress; |
| 194 | private MappingAddress grpAddress; |
| 195 | |
| 196 | /** |
| 197 | * Sets instance identifier. |
| 198 | * |
| 199 | * @param instanceId instance identifier |
| 200 | * @return Builder object |
| 201 | */ |
| 202 | public Builder withInstanceId(int instanceId) { |
| 203 | this.instanceId = instanceId; |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Sets source mask length. |
| 209 | * |
| 210 | * @param srcMaskLength source mask length |
| 211 | * @return Builder object |
| 212 | */ |
| 213 | public Builder withSrcMaskLength(byte srcMaskLength) { |
| 214 | this.srcMaskLength = srcMaskLength; |
| 215 | return this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets group mask length. |
| 220 | * |
| 221 | * @param grpMaskLength group mask length |
| 222 | * @return Builder object |
| 223 | */ |
| 224 | public Builder withGrpMaskLength(byte grpMaskLength) { |
| 225 | this.grpMaskLength = grpMaskLength; |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Sets source address. |
| 231 | * |
| 232 | * @param srcAddress source address |
| 233 | * @return Builder object |
| 234 | */ |
| 235 | public Builder withSrcAddress(MappingAddress srcAddress) { |
| 236 | this.srcAddress = srcAddress; |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Sets group address. |
| 242 | * |
| 243 | * @param grpAddress group address |
| 244 | * @return Builder object |
| 245 | */ |
| 246 | public Builder withGrpAddress(MappingAddress grpAddress) { |
| 247 | this.grpAddress = grpAddress; |
| 248 | return this; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Builds LispMulticastAddress instance. |
| 253 | * |
| 254 | * @return LispMulticastAddress instance |
| 255 | */ |
| 256 | public LispMulticastAddress build() { |
| 257 | |
| 258 | return new LispMulticastAddress(instanceId, srcMaskLength, |
| 259 | grpMaskLength, srcAddress, grpAddress); |
| 260 | } |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 261 | } |
| 262 | } |