blob: d1bd3d2c6a14d493fa9dcd9f752b8dc5586c3ecb [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 Lia445d3d2017-03-18 20:18:30 +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 Lia445d3d2017-03-18 20:18:30 +090022import org.onosproject.mapping.addresses.MappingAddress;
Jian Li3ff327a2017-03-14 17:02:19 +090023import org.onosproject.net.flow.AbstractExtension;
24
Jian Lia445d3d2017-03-18 20:18:30 +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 Lia445d3d2017-03-18 20:18:30 +090030 .ExtensionMappingAddressTypes.SEGMENT_ADDRESS;
Jian Li299bc1d2017-03-17 19:17:40 +090031
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
33 * Implementation of LISP segment address.
Jian Li299bc1d2017-03-17 19:17:40 +090034 * When multiple organizations inside of a LISP site are using private addresses
35 * [RFC1918] as EID-prefixes, their address spaces must remain segregated due
36 * to possible address duplication.
Jian Li3ff327a2017-03-14 17:02:19 +090037 */
38public class LispSegmentAddress extends AbstractExtension
39 implements ExtensionMappingAddress {
Jian Lia445d3d2017-03-18 20:18:30 +090040
41 private static final String INSTANCE_ID = "instanceId";
42 private static final String ADDRESS = "address";
43
44 private int instanceId;
45 private MappingAddress address;
46
47 /**
48 * Default constructor.
49 */
50 public LispSegmentAddress() {
51 }
52
53 /**
54 * Creates an instance with initialized parameters.
55 *
56 * @param instanceId instance id
57 * @param address address
58 */
59 private LispSegmentAddress(int instanceId, MappingAddress address) {
60 this.instanceId = instanceId;
61 this.address = address;
62 }
63
64 /**
65 * Obtains instance identifier.
66 *
67 * @return instance identifier
68 */
69 public int getInstanceId() {
70 return instanceId;
71 }
72
73 /**
74 * Obtains address.
75 *
76 * @return address
77 */
78 public MappingAddress getAddress() {
79 return address;
80 }
81
Jian Li3ff327a2017-03-14 17:02:19 +090082 @Override
83 public ExtensionMappingAddressType type() {
Jian Lia445d3d2017-03-18 20:18:30 +090084 return SEGMENT_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +090085 }
86
87 @Override
88 public byte[] serialize() {
Jian Lia445d3d2017-03-18 20:18:30 +090089 Map<String, Object> parameterMap = Maps.newHashMap();
90
91 parameterMap.put(INSTANCE_ID, instanceId);
92 parameterMap.put(ADDRESS, address);
93
94 return APP_KRYO.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +090095 }
96
97 @Override
98 public void deserialize(byte[] data) {
Jian Lia445d3d2017-03-18 20:18:30 +090099 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ff327a2017-03-14 17:02:19 +0900100
Jian Lia445d3d2017-03-18 20:18:30 +0900101 this.instanceId = (int) parameterMap.get(INSTANCE_ID);
102 this.address = (MappingAddress) parameterMap.get(ADDRESS);
103 }
104
105 @Override
106 public int hashCode() {
107 return Objects.hash(address, instanceId);
108 }
109
110 @Override
111 public boolean equals(Object obj) {
112 if (this == obj) {
113 return true;
114 }
115
116 if (obj instanceof LispSegmentAddress) {
117 final LispSegmentAddress other = (LispSegmentAddress) obj;
118 return Objects.equals(this.address, other.address) &&
119 Objects.equals(this.instanceId, other.instanceId);
120 }
121 return false;
122 }
123
124 @Override
125 public String toString() {
Jian Lifc90a082017-03-31 23:36:14 +0900126 return toStringHelper(type().toString())
Jian Lia445d3d2017-03-18 20:18:30 +0900127 .add("address", address)
128 .add("instanceId", instanceId)
129 .toString();
130 }
131
132 /**
133 * A builder for building LispSegmentAddress.
134 */
135 public static final class Builder {
136 private int instanceId;
137 private MappingAddress address;
138
139 /**
140 * Sets instance identifer.
141 *
142 * @param instanceId instance identifier
143 * @return Builder object
144 */
145 public Builder withInstanceId(int instanceId) {
146 this.instanceId = instanceId;
147 return this;
148 }
149
150 /**
151 * Sets address.
152 *
153 * @param address mapping address
154 * @return Builder object
155 */
156 public Builder withAddress(MappingAddress address) {
157 this.address = address;
158 return this;
159 }
160
161 /**
162 * Builds LispSegmentAddress instance.
163 *
164 * @return LispSegmentAddress instance
165 */
166 public LispSegmentAddress build() {
167
168 return new LispSegmentAddress(instanceId, address);
169 }
Jian Li3ff327a2017-03-14 17:02:19 +0900170 }
171}