blob: 261b863c22d0e85f86cda1633b9160b68f1302ac [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 notify message class.
27 */
Jian Lif59c0ad2016-08-02 18:11:30 +090028public final class DefaultLispMapNotify implements LispMapNotify {
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
36 /**
37 * A private constructor that protects object instantiation from external.
38 *
39 * @param nonce nonce
40 * @param keyId key identifier
41 * @param authenticationData authentication data
42 * @param recordCount record count number
43 * @param mapRecords a collection of map records
44 */
45 private DefaultLispMapNotify(long nonce, short keyId, byte[] authenticationData,
46 byte recordCount, List<LispMapRecord> mapRecords) {
47 this.nonce = nonce;
48 this.keyId = keyId;
49 this.authenticationData = authenticationData;
50 this.recordCount = recordCount;
51 this.mapRecords = mapRecords;
52 }
Jian Li451175e2016-07-19 23:22:20 +090053
54 @Override
55 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090056 return LispType.LISP_MAP_NOTIFY;
Jian Li451175e2016-07-19 23:22:20 +090057 }
58
59 @Override
60 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090061 // TODO: serialize LispMapRegister message
Jian Li451175e2016-07-19 23:22:20 +090062 }
63
64 @Override
65 public Builder createBuilder() {
66 return null;
67 }
Jian Li719b3bf2016-07-22 00:38:29 +090068
69 @Override
70 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090071 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090072 }
73
74 @Override
75 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090076 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090077 }
78
79 @Override
80 public short getKeyId() {
Jian Lif59c0ad2016-08-02 18:11:30 +090081 return this.keyId;
Jian Li719b3bf2016-07-22 00:38:29 +090082 }
83
84 @Override
85 public byte[] getAuthenticationData() {
Jian Lif59c0ad2016-08-02 18:11:30 +090086 return ImmutableByteSequence.copyFrom(this.authenticationData).asArray();
Jian Li719b3bf2016-07-22 00:38:29 +090087 }
88
89 @Override
Jian Li10a09062016-07-26 23:58:50 +090090 public List<LispMapRecord> getLispRecords() {
Jian Lif59c0ad2016-08-02 18:11:30 +090091 return ImmutableList.copyOf(mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +090092 }
93
94 public static final class DefaultNotifyBuilder implements NotifyBuilder {
95
Jian Lif59c0ad2016-08-02 18:11:30 +090096 private long nonce;
97 private short keyId;
98 private byte[] authenticationData;
99 private byte recordCount;
100 private List<LispMapRecord> mapRecords = Lists.newArrayList();
Jian Li719b3bf2016-07-22 00:38:29 +0900101
102 @Override
103 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900104 return LispType.LISP_MAP_NOTIFY;
Jian Li719b3bf2016-07-22 00:38:29 +0900105 }
106
107 @Override
108 public NotifyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900109 this.nonce = nonce;
110 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900111 }
112
113 @Override
114 public NotifyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900115 this.recordCount = recordCount;
116 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900117 }
118
119 @Override
120 public NotifyBuilder withKeyId(short keyId) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900121 this.keyId = keyId;
122 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900123 }
124
125 @Override
126 public NotifyBuilder withAuthenticationData(byte[] authenticationData) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900127 this.authenticationData = authenticationData;
128 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900129 }
130
131 @Override
Jian Li10a09062016-07-26 23:58:50 +0900132 public NotifyBuilder addRecord(LispMapRecord record) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900133 this.mapRecords.add(record);
134 return this;
135 }
136
137 @Override
138 public LispMessage build() {
139 return new DefaultLispMapNotify(nonce, keyId, authenticationData,
140 recordCount, mapRecords);
Jian Li719b3bf2016-07-22 00:38:29 +0900141 }
142 }
Jian Li451175e2016-07-19 23:22:20 +0900143}