blob: 8363e8349ad2614c07e79cc266b4c8d21bc6709f [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 Lif59c0ad2016-08-02 18:11:30 +090018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
Jian Li451175e2016-07-19 23:22:20 +090020import io.netty.buffer.ByteBuf;
Jian Lif59c0ad2016-08-02 18:11:30 +090021import org.onlab.util.ImmutableByteSequence;
Jian Li451175e2016-07-19 23:22:20 +090022
Jian Li719b3bf2016-07-22 00:38:29 +090023import java.util.List;
24
Jian Li451175e2016-07-19 23:22:20 +090025/**
26 * Default LISP map register message class.
27 */
Jian Lif59c0ad2016-08-02 18:11:30 +090028public final class DefaultLispMapRegister implements LispMapRegister {
29
30 private final long nonce;
31 private final short keyId;
32 private final byte[] authenticationData;
33 private final byte recordCount;
34 private final List<LispMapRecord> mapRecords;
35 private final boolean proxyMapReply;
36 private final boolean wantMapNotify;
37
38 /**
39 * A private constructor that protects object instantiation from external.
40 *
41 * @param nonce nonce
42 * @param keyId key identifier
43 * @param authenticationData authentication data
44 * @param recordCount record count number
45 * @param mapRecords a collection of map records
46 * @param proxyMapReply proxy map reply flag
47 * @param wantMapNotify want map notify flag
48 */
49 private DefaultLispMapRegister(long nonce, short keyId,
50 byte[] authenticationData, byte recordCount,
51 List<LispMapRecord> mapRecords,
52 boolean proxyMapReply, boolean wantMapNotify) {
53 this.nonce = nonce;
54 this.keyId = keyId;
55 this.authenticationData = authenticationData;
56 this.recordCount = recordCount;
57 this.mapRecords = mapRecords;
58 this.proxyMapReply = proxyMapReply;
59 this.wantMapNotify = wantMapNotify;
60 }
61
Jian Li451175e2016-07-19 23:22:20 +090062 @Override
63 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090064 return LispType.LISP_MAP_REGISTER;
Jian Li451175e2016-07-19 23:22:20 +090065 }
66
67 @Override
68 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090069 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090070 }
71
72 @Override
73 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090074 return new DefaultRegisterBuilder();
Jian Li451175e2016-07-19 23:22:20 +090075 }
Jian Li719b3bf2016-07-22 00:38:29 +090076
77 @Override
78 public boolean isProxyMapReply() {
Jian Lif59c0ad2016-08-02 18:11:30 +090079 return proxyMapReply;
Jian Li719b3bf2016-07-22 00:38:29 +090080 }
81
82 @Override
83 public boolean isWantMapNotify() {
Jian Lif59c0ad2016-08-02 18:11:30 +090084 return wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +090085 }
86
87 @Override
88 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090089 return recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090090 }
91
92 @Override
93 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090094 return nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090095 }
96
97 @Override
98 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +090099 return keyId;
Jian Li719b3bf2016-07-22 00:38:29 +0900100 }
101
102 @Override
103 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900104 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +0900105 }
106
107 @Override
Jian Li10a09062016-07-26 23:58:50 +0900108 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900109 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900110 }
111
112 public static final class DefaultRegisterBuilder implements RegisterBuilder {
113
Jian Lif59c0ad2016-08-02 18:11:30 +0900114 private long nonce;
115 private short keyId;
116 private byte[] authenticationData;
117 private byte recordCount;
118 private final List<LispMapRecord> mapRecords = Lists.newArrayList();
119 private boolean proxyMapReply;
120 private boolean wantMapNotify;
Jian Li719b3bf2016-07-22 00:38:29 +0900121
122 @Override
123 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900124 return LispType.LISP_MAP_REGISTER;
Jian Li719b3bf2016-07-22 00:38:29 +0900125 }
126
127 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900128 public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) {
129 this.proxyMapReply = proxyMapReply;
130 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900131 }
132
133 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900134 public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) {
135 this.wantMapNotify = wantMapNotify;
136 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900137 }
138
139 @Override
140 public RegisterBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900141 this.recordCount = recordCount;
142 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900143 }
144
145 @Override
146 public RegisterBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900147 this.nonce = nonce;
148 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900149 }
150
151 @Override
152 public RegisterBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900153 this.keyId = keyId;
154 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900155 }
156
157 @Override
158 public RegisterBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900159 this.authenticationData = authenticationData;
160 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900161 }
162
163 @Override
Jian Li10a09062016-07-26 23:58:50 +0900164 public RegisterBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900165 this.mapRecords.add(record);
166 return this;
167 }
168
169 @Override
Jian Li525fded2016-08-04 01:15:33 +0900170 public LispMapRegister build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900171 return new DefaultLispMapRegister(nonce, keyId, authenticationData,
172 recordCount, mapRecords, proxyMapReply, wantMapNotify);
Jian Li719b3bf2016-07-22 00:38:29 +0900173 }
174 }
Jian Li451175e2016-07-19 23:22:20 +0900175}