blob: b77e5805ab817d4cb925c833c5e0cec3920605d4 [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 Li26069e22016-08-10 22:00:52 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Li451175e2016-07-19 23:22:20 +090024
Jian Li719b3bf2016-07-22 00:38:29 +090025import java.util.List;
26
Jian Li20850d32016-08-04 02:15:57 +090027import static com.google.common.base.MoreObjects.toStringHelper;
28
Jian Li451175e2016-07-19 23:22:20 +090029/**
30 * Default LISP map register message class.
31 */
Jian Lif59c0ad2016-08-02 18:11:30 +090032public final class DefaultLispMapRegister implements LispMapRegister {
33
34 private final long nonce;
35 private final short keyId;
36 private final byte[] authenticationData;
37 private final byte recordCount;
38 private final List<LispMapRecord> mapRecords;
39 private final boolean proxyMapReply;
40 private final boolean wantMapNotify;
41
42 /**
43 * A private constructor that protects object instantiation from external.
44 *
45 * @param nonce nonce
46 * @param keyId key identifier
47 * @param authenticationData authentication data
48 * @param recordCount record count number
49 * @param mapRecords a collection of map records
50 * @param proxyMapReply proxy map reply flag
51 * @param wantMapNotify want map notify flag
52 */
53 private DefaultLispMapRegister(long nonce, short keyId,
54 byte[] authenticationData, byte recordCount,
55 List<LispMapRecord> mapRecords,
56 boolean proxyMapReply, boolean wantMapNotify) {
57 this.nonce = nonce;
58 this.keyId = keyId;
59 this.authenticationData = authenticationData;
60 this.recordCount = recordCount;
61 this.mapRecords = mapRecords;
62 this.proxyMapReply = proxyMapReply;
63 this.wantMapNotify = wantMapNotify;
64 }
65
Jian Li451175e2016-07-19 23:22:20 +090066 @Override
67 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090068 return LispType.LISP_MAP_REGISTER;
Jian Li451175e2016-07-19 23:22:20 +090069 }
70
71 @Override
72 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090073 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090074 }
75
76 @Override
77 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090078 return new DefaultRegisterBuilder();
Jian Li451175e2016-07-19 23:22:20 +090079 }
Jian Li719b3bf2016-07-22 00:38:29 +090080
81 @Override
82 public boolean isProxyMapReply() {
Jian Lif59c0ad2016-08-02 18:11:30 +090083 return proxyMapReply;
Jian Li719b3bf2016-07-22 00:38:29 +090084 }
85
86 @Override
87 public boolean isWantMapNotify() {
Jian Lif59c0ad2016-08-02 18:11:30 +090088 return wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +090089 }
90
91 @Override
92 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090093 return recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090094 }
95
96 @Override
97 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090098 return nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090099 }
100
101 @Override
102 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900103 return keyId;
Jian Li719b3bf2016-07-22 00:38:29 +0900104 }
105
106 @Override
107 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900108 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +0900109 }
110
111 @Override
Jian Li10a09062016-07-26 23:58:50 +0900112 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900113 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900114 }
115
Jian Li20850d32016-08-04 02:15:57 +0900116 @Override
117 public String toString() {
118 return toStringHelper(this)
119 .add("type", getType())
120 .add("nonce", nonce)
121 .add("recordCount", recordCount)
122 .add("keyId", keyId)
123 .add("mapRecords", mapRecords)
124 .add("proxyMapReply", proxyMapReply)
125 .add("wantMapNotify", wantMapNotify).toString();
126 }
127
128 @Override
129 public boolean equals(Object o) {
130 if (this == o) {
131 return true;
132 }
133 if (o == null || getClass() != o.getClass()) {
134 return false;
135 }
136
137 DefaultLispMapRegister that = (DefaultLispMapRegister) o;
138 return Objects.equal(nonce, that.nonce) &&
139 Objects.equal(recordCount, that.recordCount) &&
140 Objects.equal(keyId, that.keyId) &&
141 Objects.equal(authenticationData, that.authenticationData) &&
142 Objects.equal(proxyMapReply, that.proxyMapReply) &&
143 Objects.equal(wantMapNotify, that.wantMapNotify);
144 }
145
146 @Override
147 public int hashCode() {
148 return Objects.hashCode(nonce, recordCount, keyId, authenticationData,
149 proxyMapReply, wantMapNotify);
150 }
151
Jian Li719b3bf2016-07-22 00:38:29 +0900152 public static final class DefaultRegisterBuilder implements RegisterBuilder {
153
Jian Lif59c0ad2016-08-02 18:11:30 +0900154 private long nonce;
155 private short keyId;
156 private byte[] authenticationData;
157 private byte recordCount;
158 private final List<LispMapRecord> mapRecords = Lists.newArrayList();
159 private boolean proxyMapReply;
160 private boolean wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +0900161
162 @Override
163 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900164 return LispType.LISP_MAP_REGISTER;
Jian Li719b3bf2016-07-22 00:38:29 +0900165 }
166
167 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900168 public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) {
169 this.proxyMapReply = proxyMapReply;
170 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900171 }
172
173 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900174 public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) {
175 this.wantMapNotify = wantMapNotify;
176 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900177 }
178
179 @Override
180 public RegisterBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900181 this.recordCount = recordCount;
182 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900183 }
184
185 @Override
186 public RegisterBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900187 this.nonce = nonce;
188 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900189 }
190
191 @Override
192 public RegisterBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900193 this.keyId = keyId;
194 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900195 }
196
197 @Override
198 public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900199 this.authenticationData = authenticationData;
200 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900201 }
202
203 @Override
Jian Li10a09062016-07-26 23:58:50 +0900204 public RegisterBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900205 this.mapRecords.add(record);
206 return this;
207 }
208
209 @Override
Jian Li525fded2016-08-04 01:15:33 +0900210 public LispMapRegister build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900211 return new DefaultLispMapRegister(nonce, keyId, authenticationData,
212 recordCount, mapRecords, proxyMapReply, wantMapNotify);
Jian Li719b3bf2016-07-22 00:38:29 +0900213 }
214 }
Jian Li26069e22016-08-10 22:00:52 +0900215
216 /**
217 * A private LISP message reader for MapRegister message.
218 */
219 private static class RegisterReader implements LispMessageReader<LispMapRegister> {
220
221 @Override
222 public LispMapRegister readFrom(ByteBuf byteBuf) throws LispParseError {
223 return null;
224 }
225 }
Jian Li451175e2016-07-19 23:22:20 +0900226}