blob: 7d55e1e98005ec956f062cf47febb053f04b932a [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 notify message class.
30 */
Jian Lif59c0ad2016-08-02 18:11:30 +090031public final class DefaultLispMapNotify implements LispMapNotify {
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
39 /**
40 * A private constructor that protects object instantiation from external.
41 *
42 * @param nonce nonce
43 * @param keyId key identifier
44 * @param authenticationData authentication data
45 * @param recordCount record count number
46 * @param mapRecords a collection of map records
47 */
48 private DefaultLispMapNotify(long nonce, short keyId, byte[] authenticationData,
49 byte recordCount, List<LispMapRecord> mapRecords) {
50 this.nonce = nonce;
51 this.keyId = keyId;
52 this.authenticationData = authenticationData;
53 this.recordCount = recordCount;
54 this.mapRecords = mapRecords;
55 }
Jian Li451175e2016-07-19 23:22:20 +090056
57 @Override
58 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090059 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090060 }
61
62 @Override
63 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090064 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090065 }
66
67 @Override
68 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090069 return new DefaultNotifyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090070 }
Jian Li719b3bf2016-07-22 00:38:29 +090071
72 @Override
73 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090074 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090075 }
76
77 @Override
78 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090079 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090080 }
81
82 @Override
83 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +090084 return this.keyId;
Jian Li719b3bf2016-07-22 00:38:29 +090085 }
86
87 @Override
88 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +090089 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +090090 }
91
92 @Override
Jian Li10a09062016-07-26 23:58:50 +090093 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +090094 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +090095 }
96
Jian Li20850d32016-08-04 02:15:57 +090097 @Override
98 public String toString() {
99 return toStringHelper(this)
100 .add("type", getType())
101 .add("nonce", nonce)
102 .add("recordCount", recordCount)
103 .add("keyId", keyId)
104 .add("mapRecords", mapRecords).toString();
105 }
106
107 @Override
108 public boolean equals(Object o) {
109 if (this == o) {
110 return true;
111 }
112 if (o == null || getClass() != o.getClass()) {
113 return false;
114 }
115 DefaultLispMapNotify that = (DefaultLispMapNotify) o;
116 return Objects.equal(nonce, that.nonce) &&
117 Objects.equal(recordCount, that.recordCount) &&
118 Objects.equal(keyId, that.keyId) &&
119 Objects.equal(authenticationData, that.authenticationData);
120 }
121
122 @Override
123 public int hashCode() {
124 return Objects.hashCode(nonce, recordCount, keyId, authenticationData);
125 }
126
Jian Li719b3bf2016-07-22 00:38:29 +0900127 public static final class DefaultNotifyBuilder implements NotifyBuilder {
128
Jian Lif59c0ad2016-08-02 18:11:30 +0900129 private long nonce;
130 private short keyId;
131 private byte[] authenticationData;
132 private byte recordCount;
133 private List<LispMapRecord> mapRecords = Lists.newArrayList();
Jian Li719b3bf2016-07-22 00:38:29 +0900134
135 @Override
136 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900137 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900138 }
139
140 @Override
141 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900142 this.nonce = nonce;
143 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900144 }
145
146 @Override
147 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900148 this.recordCount = recordCount;
149 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900150 }
151
152 @Override
153 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900154 this.keyId = keyId;
155 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900156 }
157
158 @Override
159 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900160 this.authenticationData = authenticationData;
161 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900162 }
163
164 @Override
Jian Li10a09062016-07-26 23:58:50 +0900165 public NotifyBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900166 this.mapRecords.add(record);
167 return this;
168 }
169
170 @Override
Jian Li525fded2016-08-04 01:15:33 +0900171 public LispMapNotify build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900172 return new DefaultLispMapNotify(nonce, keyId, authenticationData,
173 recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900174 }
175 }
Jian Li451175e2016-07-19 23:22:20 +0900176}