blob: bbfe3b98bff64af763b73cb8b275d67e676b991d [file] [log] [blame]
Jian Li451175e2016-07-19 23:22:20 +09001/*
2 * Copyright 2016-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 */
16package org.onosproject.lisp.msg.protocols;
17
Jian Li20850d32016-08-04 02:15:57 +090018import com.google.common.base.Objects;
Jian Lif59c0ad2016-08-02 18:11:30 +090019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Jian Li451175e2016-07-19 23:22:20 +090021import io.netty.buffer.ByteBuf;
Jian Lif59c0ad2016-08-02 18:11:30 +090022import org.onlab.util.ImmutableByteSequence;
Jian Li451175e2016-07-19 23:22:20 +090023
Jian Li719b3bf2016-07-22 00:38:29 +090024import java.util.List;
25
Jian Li20850d32016-08-04 02:15:57 +090026import static com.google.common.base.MoreObjects.toStringHelper;
27
Jian Li451175e2016-07-19 23:22:20 +090028/**
29 * Default LISP map register message class.
30 */
Jian Lif59c0ad2016-08-02 18:11:30 +090031public final class DefaultLispMapRegister implements LispMapRegister {
32
33 private final long nonce;
34 private final short keyId;
35 private final byte[] authenticationData;
36 private final byte recordCount;
37 private final List<LispMapRecord> mapRecords;
38 private final boolean proxyMapReply;
39 private final boolean wantMapNotify;
40
41 /**
42 * A private constructor that protects object instantiation from external.
43 *
44 * @param nonce nonce
45 * @param keyId key identifier
46 * @param authenticationData authentication data
47 * @param recordCount record count number
48 * @param mapRecords a collection of map records
49 * @param proxyMapReply proxy map reply flag
50 * @param wantMapNotify want map notify flag
51 */
52 private DefaultLispMapRegister(long nonce, short keyId,
53 byte[] authenticationData, byte recordCount,
54 List<LispMapRecord> mapRecords,
55 boolean proxyMapReply, boolean wantMapNotify) {
56 this.nonce = nonce;
57 this.keyId = keyId;
58 this.authenticationData = authenticationData;
59 this.recordCount = recordCount;
60 this.mapRecords = mapRecords;
61 this.proxyMapReply = proxyMapReply;
62 this.wantMapNotify = wantMapNotify;
63 }
64
Jian Li451175e2016-07-19 23:22:20 +090065 @Override
66 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090067 return LispType.LISP_MAP_REGISTER;
Jian Li451175e2016-07-19 23:22:20 +090068 }
69
70 @Override
71 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090072 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090073 }
74
75 @Override
76 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090077 return new DefaultRegisterBuilder();
Jian Li451175e2016-07-19 23:22:20 +090078 }
Jian Li719b3bf2016-07-22 00:38:29 +090079
80 @Override
81 public boolean isProxyMapReply() {
Jian Lif59c0ad2016-08-02 18:11:30 +090082 return proxyMapReply;
Jian Li719b3bf2016-07-22 00:38:29 +090083 }
84
85 @Override
86 public boolean isWantMapNotify() {
Jian Lif59c0ad2016-08-02 18:11:30 +090087 return wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +090088 }
89
90 @Override
91 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090092 return recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090093 }
94
95 @Override
96 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090097 return nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090098 }
99
100 @Override
101 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900102 return keyId;
Jian Li719b3bf2016-07-22 00:38:29 +0900103 }
104
105 @Override
106 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900107 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +0900108 }
109
110 @Override
Jian Li10a09062016-07-26 23:58:50 +0900111 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900112 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900113 }
114
Jian Li20850d32016-08-04 02:15:57 +0900115 @Override
116 public String toString() {
117 return toStringHelper(this)
118 .add("type", getType())
119 .add("nonce", nonce)
120 .add("recordCount", recordCount)
121 .add("keyId", keyId)
122 .add("mapRecords", mapRecords)
123 .add("proxyMapReply", proxyMapReply)
124 .add("wantMapNotify", wantMapNotify).toString();
125 }
126
127 @Override
128 public boolean equals(Object o) {
129 if (this == o) {
130 return true;
131 }
132 if (o == null || getClass() != o.getClass()) {
133 return false;
134 }
135
136 DefaultLispMapRegister that = (DefaultLispMapRegister) o;
137 return Objects.equal(nonce, that.nonce) &&
138 Objects.equal(recordCount, that.recordCount) &&
139 Objects.equal(keyId, that.keyId) &&
140 Objects.equal(authenticationData, that.authenticationData) &&
141 Objects.equal(proxyMapReply, that.proxyMapReply) &&
142 Objects.equal(wantMapNotify, that.wantMapNotify);
143 }
144
145 @Override
146 public int hashCode() {
147 return Objects.hashCode(nonce, recordCount, keyId, authenticationData,
148 proxyMapReply, wantMapNotify);
149 }
150
Jian Li719b3bf2016-07-22 00:38:29 +0900151 public static final class DefaultRegisterBuilder implements RegisterBuilder {
152
Jian Lif59c0ad2016-08-02 18:11:30 +0900153 private long nonce;
154 private short keyId;
155 private byte[] authenticationData;
156 private byte recordCount;
157 private final List<LispMapRecord> mapRecords = Lists.newArrayList();
158 private boolean proxyMapReply;
159 private boolean wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +0900160
161 @Override
162 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900163 return LispType.LISP_MAP_REGISTER;
Jian Li719b3bf2016-07-22 00:38:29 +0900164 }
165
166 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900167 public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) {
168 this.proxyMapReply = proxyMapReply;
169 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900170 }
171
172 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900173 public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) {
174 this.wantMapNotify = wantMapNotify;
175 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900176 }
177
178 @Override
179 public RegisterBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900180 this.recordCount = recordCount;
181 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900182 }
183
184 @Override
185 public RegisterBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900186 this.nonce = nonce;
187 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900188 }
189
190 @Override
191 public RegisterBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900192 this.keyId = keyId;
193 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900194 }
195
196 @Override
197 public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900198 this.authenticationData = authenticationData;
199 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900200 }
201
202 @Override
Jian Li10a09062016-07-26 23:58:50 +0900203 public RegisterBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900204 this.mapRecords.add(record);
205 return this;
206 }
207
208 @Override
Jian Li525fded2016-08-04 01:15:33 +0900209 public LispMapRegister build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900210 return new DefaultLispMapRegister(nonce, keyId, authenticationData,
211 recordCount, mapRecords, proxyMapReply, wantMapNotify);
Jian Li719b3bf2016-07-22 00:38:29 +0900212 }
213 }
Jian Li451175e2016-07-19 23:22:20 +0900214}