blob: 204f551e0946681c6d55563f5ceb67a36025b83d [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.NONCE_ADDRESS;
Jian Li299bc1d2017-03-17 19:17:40 +090031
Jian Li3ff327a2017-03-14 17:02:19 +090032/**
33 * Implementation of LISP nonce address.
Jian Li299bc1d2017-03-17 19:17:40 +090034 * When a public PETR device wants to verify who is encapsulating to it, it can
35 * check for a specific nonce value in the LISP encapsulated packet.
Jian Li3ff327a2017-03-14 17:02:19 +090036 */
37public class LispNonceAddress extends AbstractExtension
Jian Lia445d3d2017-03-18 20:18:30 +090038 implements ExtensionMappingAddress {
39
40 private static final String NONCE = "nonce";
41 private static final String ADDRESS = "address";
42
43 private int nonce;
44 private MappingAddress address;
45
46 /**
47 * Default constructor.
48 */
49 public LispNonceAddress() {
50 }
51
52 /**
53 * Creates an instance with initialized parameters.
54 *
55 * @param nonce nonce
56 * @param address address
57 */
58 private LispNonceAddress(int nonce, MappingAddress address) {
59 this.nonce = nonce;
60 this.address = address;
61 }
62
63 /**
64 * Obtains nonce.
65 *
66 * @return nonce
67 */
68 public int getNonce() {
69 return nonce;
70 }
71
72 /**
73 * Obtains address.
74 *
75 * @return address
76 */
77 public MappingAddress getAddress() {
78 return address;
79 }
80
Jian Li3ff327a2017-03-14 17:02:19 +090081 @Override
82 public ExtensionMappingAddressType type() {
Jian Li299bc1d2017-03-17 19:17:40 +090083 return NONCE_ADDRESS.type();
Jian Li3ff327a2017-03-14 17:02:19 +090084 }
85
86 @Override
87 public byte[] serialize() {
Jian Lia445d3d2017-03-18 20:18:30 +090088 Map<String, Object> parameterMap = Maps.newHashMap();
89
90 parameterMap.put(NONCE, nonce);
91 parameterMap.put(ADDRESS, address);
92
93 return APP_KRYO.serialize(parameterMap);
Jian Li3ff327a2017-03-14 17:02:19 +090094 }
95
96 @Override
97 public void deserialize(byte[] data) {
Jian Lia445d3d2017-03-18 20:18:30 +090098 Map<String, Object> parameterMap = APP_KRYO.deserialize(data);
Jian Li3ff327a2017-03-14 17:02:19 +090099
Jian Lia445d3d2017-03-18 20:18:30 +0900100 this.nonce = (int) parameterMap.get(NONCE);
101 this.address = (MappingAddress) parameterMap.get(ADDRESS);
102 }
103
104 @Override
105 public int hashCode() {
106 return Objects.hash(nonce, address);
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114
115 if (obj instanceof LispNonceAddress) {
116 final LispNonceAddress other = (LispNonceAddress) obj;
117 return Objects.equals(this.nonce, other.nonce) &&
118 Objects.equals(this.address, other.address);
119 }
120 return false;
121 }
122
123 @Override
124 public String toString() {
Jian Lifc90a082017-03-31 23:36:14 +0900125 return toStringHelper(type().toString())
Jian Lia445d3d2017-03-18 20:18:30 +0900126 .add("nonce", nonce)
127 .add("address", address)
128 .toString();
129 }
130
131 /**
132 * A builder for building LispNonceAddress.
133 */
134 public static final class Builder {
135 private int nonce;
136 private MappingAddress address;
137
138 /**
139 * Sets nonce.
140 *
141 * @param nonce nonce
142 * @return Builder object
143 */
144 public Builder withNonce(int nonce) {
145 this.nonce = nonce;
146 return this;
147 }
148
149 /**
150 * Sets address.
151 *
152 * @param address address
153 * @return Builder object
154 */
155 public Builder withAddress(MappingAddress address) {
156 this.address = address;
157 return this;
158 }
159
160 /**
161 * Builds LispNonceAddress instance.
162 *
163 * @return LispNonceAddress instance
164 */
165 public LispNonceAddress build() {
166
167 return new LispNonceAddress(nonce, address);
168 }
Jian Li3ff327a2017-03-14 17:02:19 +0900169 }
170}