blob: ef8780df865b305fd4e9b71b28757cae97af447f [file] [log] [blame]
Jian Li3ff327a2017-03-14 17:02:19 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li3ff327a2017-03-14 17:02:19 +09003 *
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 Lie47e21a2017-03-18 13:39:51 +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 Lie47e21a2017-03-18 13:39:51 +090022import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090023import org.onosproject.net.flow.AbstractExtension;
24
Jian Lie47e21a2017-03-18 13:39:51 +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 Lie47e21a2017-03-18 13:39:51 +090030 .ExtensionMappingAddressTypes.MULTICAST_ADDRESS;
Jian Li299bc1d2017-03-17 19:17:40 +090031
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
33 * Implementation of LISP multicast address.
Jian Li299bc1d2017-03-17 19:17:40 +090034 * The intent of this type of unicast replication is to deliver packets to
35 * multiple ETRs at receiver LISP multicast sites.
Jian Li3ff327a2017-03-14 17:02:19 +090036 */
37public class LispMulticastAddress extends AbstractExtension
Jian Lie47e21a2017-03-18 13:39:51 +090038 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 Li3ff327a2017-03-14 17:02:19 +0900122 @Override
123 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +0900124 return MULTICAST_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +0900125 }
126
127 @Override
128 public byte[] serialize() {
Jian Lie47e21a2017-03-18 13:39:51 +0900129 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 Li3ff327a2017-03-14 17:02:19 +0900138 }
139
140 @Override
141 public void deserialize(byte[] data) {
Jian Lie47e21a2017-03-18 13:39:51 +0900142 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ff327a2017-03-14 17:02:19 +0900143
Jian Lie47e21a2017-03-18 13:39:51 +0900144 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() {
Jian Lifc90a082017-03-31 23:36:14 +0900177 return toStringHelper(type().toString())
Jian Lie47e21a2017-03-18 13:39:51 +0900178 .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 Li3ff327a2017-03-14 17:02:19 +0900261 }
262}