blob: 831065a296658879b66d248bff8d4f633eb8d9d0 [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 notify message class.
31 */
Jian Lif59c0ad2016-08-02 18:11:30 +090032public final class DefaultLispMapNotify implements LispMapNotify {
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
40 /**
41 * A private constructor that protects object instantiation from external.
42 *
43 * @param nonce nonce
44 * @param keyId key identifier
45 * @param authenticationData authentication data
46 * @param recordCount record count number
47 * @param mapRecords a collection of map records
48 */
49 private DefaultLispMapNotify(long nonce, short keyId, byte[] authenticationData,
50 byte recordCount, List<LispMapRecord> mapRecords) {
51 this.nonce = nonce;
52 this.keyId = keyId;
53 this.authenticationData = authenticationData;
54 this.recordCount = recordCount;
55 this.mapRecords = mapRecords;
56 }
Jian Li451175e2016-07-19 23:22:20 +090057
58 @Override
59 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090060 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090061 }
62
63 @Override
64 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090065 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090066 }
67
68 @Override
69 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090070 return new DefaultNotifyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090071 }
Jian Li719b3bf2016-07-22 00:38:29 +090072
73 @Override
74 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090075 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090076 }
77
78 @Override
79 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090080 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090081 }
82
83 @Override
84 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +090085 return this.keyId;
Jian Li719b3bf2016-07-22 00:38:29 +090086 }
87
88 @Override
89 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +090090 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +090091 }
92
93 @Override
Jian Li10a09062016-07-26 23:58:50 +090094 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +090095 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +090096 }
97
Jian Li20850d32016-08-04 02:15:57 +090098 @Override
99 public String toString() {
100 return toStringHelper(this)
101 .add("type", getType())
102 .add("nonce", nonce)
103 .add("recordCount", recordCount)
104 .add("keyId", keyId)
105 .add("mapRecords", mapRecords).toString();
106 }
107
108 @Override
109 public boolean equals(Object o) {
110 if (this == o) {
111 return true;
112 }
113 if (o == null || getClass() != o.getClass()) {
114 return false;
115 }
116 DefaultLispMapNotify that = (DefaultLispMapNotify) o;
117 return Objects.equal(nonce, that.nonce) &&
118 Objects.equal(recordCount, that.recordCount) &&
119 Objects.equal(keyId, that.keyId) &&
120 Objects.equal(authenticationData, that.authenticationData);
121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hashCode(nonce, recordCount, keyId, authenticationData);
126 }
127
Jian Li719b3bf2016-07-22 00:38:29 +0900128 public static final class DefaultNotifyBuilder implements NotifyBuilder {
129
Jian Lif59c0ad2016-08-02 18:11:30 +0900130 private long nonce;
131 private short keyId;
132 private byte[] authenticationData;
133 private byte recordCount;
134 private List<LispMapRecord> mapRecords = Lists.newArrayList();
Jian Li719b3bf2016-07-22 00:38:29 +0900135
136 @Override
137 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900138 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900139 }
140
141 @Override
142 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900143 this.nonce = nonce;
144 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900145 }
146
147 @Override
148 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900149 this.recordCount = recordCount;
150 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900151 }
152
153 @Override
154 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900155 this.keyId = keyId;
156 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900157 }
158
159 @Override
160 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900161 this.authenticationData = authenticationData;
162 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900163 }
164
165 @Override
Jian Li10a09062016-07-26 23:58:50 +0900166 public NotifyBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900167 this.mapRecords.add(record);
168 return this;
169 }
170
171 @Override
Jian Li525fded2016-08-04 01:15:33 +0900172 public LispMapNotify build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900173 return new DefaultLispMapNotify(nonce, keyId, authenticationData,
174 recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900175 }
176 }
Jian Li26069e22016-08-10 22:00:52 +0900177
178 /**
179 * A private LISP message reader for MapNotify message.
180 */
181 private static class NotifyReader implements LispMessageReader<LispMapNotify> {
182
183 @Override
184 public LispMapNotify readFrom(ByteBuf byteBuf) throws LispParseError {
185 return null;
186 }
187 }
Jian Li451175e2016-07-19 23:22:20 +0900188}