blob: 4208265b57fad96364bd67faec1a5e366f0739e3 [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 Li50b51ee2017-03-19 03:23:29 +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 Li50b51ee2017-03-19 03:23:29 +090022import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090023import org.onosproject.net.flow.AbstractExtension;
24
Jian Li50b51ee2017-03-19 03:23:29 +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
30 .ExtensionMappingAddressTypes.SOURCE_DEST_ADDRESS;
31
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
Jian Li299bc1d2017-03-17 19:17:40 +090033 * Implementation of LISP source and destination address.
34 * When both a source and destination address of a flow need consideration for
35 * different locator-sets, this 2-tuple key is used in EID fields in LISP
36 * control messages.
Jian Li3ff327a2017-03-14 17:02:19 +090037 */
38public class LispSrcDstAddress extends AbstractExtension
39 implements ExtensionMappingAddress {
Jian Li50b51ee2017-03-19 03:23:29 +090040
41 private static final String SRC_MASK_LENGTH = "srcMaskLength";
42 private static final String DST_MASK_LENGTH = "dstMaskLength";
43 private static final String SRC_PREFIX = "srcPrefix";
44 private static final String DST_PREFIX = "dstPrefix";
45
46 private byte srcMaskLength;
47 private byte dstMaskLength;
48 private MappingAddress srcPrefix;
49 private MappingAddress dstPrefix;
50
51 /**
52 * Default constructor.
53 */
54 public LispSrcDstAddress() {
55 }
56
57 /**
58 * Creates an instance with initialized parameters.
59 *
60 * @param srcMaskLength source mask length
61 * @param dstMaskLength destination mask length
62 * @param srcPrefix source address prefix
63 * @param dstPrefix destination address prefix
64 */
65 private LispSrcDstAddress(byte srcMaskLength, byte dstMaskLength,
66 MappingAddress srcPrefix, MappingAddress dstPrefix) {
67 this.srcMaskLength = srcMaskLength;
68 this.dstMaskLength = dstMaskLength;
69 this.srcPrefix = srcPrefix;
70 this.dstPrefix = dstPrefix;
71 }
72
73 /**
74 * Obtains source mask length.
75 *
76 * @return source mask length
77 */
78 public byte getSrcMaskLength() {
79 return srcMaskLength;
80 }
81
82 /**
83 * Obtains destination mask length.
84 *
85 * @return destination mask length
86 */
87 public byte getDstMaskLength() {
88 return dstMaskLength;
89 }
90
91 /**
92 * Obtains source address prefix.
93 *
94 * @return source address prefix
95 */
96 public MappingAddress getSrcPrefix() {
97 return srcPrefix;
98 }
99
100 /**
101 * Obtains destination address prefix.
102 *
103 * @return destination address prefix
104 */
105 public MappingAddress getDstPrefix() {
106 return dstPrefix;
107 }
108
Jian Li3ff327a2017-03-14 17:02:19 +0900109 @Override
110 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +0900111 return SOURCE_DEST_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +0900112 }
113
114 @Override
115 public byte[] serialize() {
Jian Li50b51ee2017-03-19 03:23:29 +0900116 Map<String, Object> parameterMap = Maps.newHashMap();
117
118 parameterMap.put(SRC_MASK_LENGTH, srcMaskLength);
119 parameterMap.put(DST_MASK_LENGTH, dstMaskLength);
120 parameterMap.put(SRC_PREFIX, srcPrefix);
121 parameterMap.put(DST_PREFIX, dstPrefix);
122
123 return APP_KRYO.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +0900124 }
125
126 @Override
127 public void deserialize(byte[] data) {
Jian Li50b51ee2017-03-19 03:23:29 +0900128 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ff327a2017-03-14 17:02:19 +0900129
Jian Li50b51ee2017-03-19 03:23:29 +0900130 this.srcMaskLength = (byte) parameterMap.get(SRC_MASK_LENGTH);
131 this.dstMaskLength = (byte) parameterMap.get(DST_MASK_LENGTH);
132 this.srcPrefix = (MappingAddress) parameterMap.get(SRC_PREFIX);
133 this.dstPrefix = (MappingAddress) parameterMap.get(DST_PREFIX);
134 }
135
136 @Override
137 public int hashCode() {
138 return Objects.hash(srcPrefix, dstPrefix, srcMaskLength, dstMaskLength);
139 }
140
141 @Override
142 public boolean equals(Object obj) {
143 if (this == obj) {
144 return true;
145 }
146
147 if (obj instanceof LispSrcDstAddress) {
148 final LispSrcDstAddress other = (LispSrcDstAddress) obj;
149 return Objects.equals(this.srcPrefix, other.srcPrefix) &&
150 Objects.equals(this.dstPrefix, other.dstPrefix) &&
151 Objects.equals(this.srcMaskLength, other.srcMaskLength) &&
152 Objects.equals(this.dstMaskLength, other.dstMaskLength);
153 }
154 return false;
155 }
156
157 @Override
158 public String toString() {
Jian Lifc90a082017-03-31 23:36:14 +0900159 return toStringHelper(type().toString())
Jian Li50b51ee2017-03-19 03:23:29 +0900160 .add("source prefix", srcPrefix)
161 .add("destination prefix", dstPrefix)
162 .add("source mask length", srcMaskLength)
163 .add("destination mask length", dstMaskLength)
164 .toString();
165 }
166
167 /**
168 * A builder for building LispSrcDstAddress.
169 */
170 public static final class Builder {
171 private byte srcMaskLength;
172 private byte dstMaskLength;
173 private MappingAddress srcPrefix;
174 private MappingAddress dstPrefix;
175
176 /**
177 * Sets source address prefix.
178 *
179 * @param srcPrefix source prefix
180 * @return Builder object
181 */
182 public Builder withSrcPrefix(MappingAddress srcPrefix) {
183 this.srcPrefix = srcPrefix;
184 return this;
185 }
186
187 /**
188 * Sets destination address prefix.
189 *
190 * @param dstPrefix destination prefix
191 * @return Builder object
192 */
193 public Builder withDstPrefix(MappingAddress dstPrefix) {
194 this.dstPrefix = dstPrefix;
195 return this;
196 }
197
198 /**
199 * Sets source mask length.
200 *
201 * @param srcMaskLength source mask length
202 * @return Builder object
203 */
204 public Builder withSrcMaskLength(byte srcMaskLength) {
205 this.srcMaskLength = srcMaskLength;
206 return this;
207 }
208
209 /**
210 * Sets destination mask length.
211 *
212 * @param dstMaskLength destination mask length
213 * @return Builder object
214 */
215 public Builder withDstMaskLength(byte dstMaskLength) {
216 this.dstMaskLength = dstMaskLength;
217 return this;
218 }
219
220 /**
221 * Builds LispSrcDstAddress instance.
222 *
223 * @return LispSrcDstAddress instance
224 */
225 public LispSrcDstAddress build() {
226
227 return new LispSrcDstAddress(srcMaskLength, dstMaskLength,
228 srcPrefix, dstPrefix);
229 }
Jian Li3ff327a2017-03-14 17:02:19 +0900230 }
231}