Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 20 | import io.netty.buffer.ByteBuf; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 21 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 22 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 23 | import java.util.List; |
| 24 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 25 | /** |
| 26 | * Default LISP map register message class. |
| 27 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 28 | public 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 Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 62 | @Override |
| 63 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 64 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 69 | // TODO: serialize LispMapRegister message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame^] | 74 | return new DefaultRegisterBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 75 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public boolean isProxyMapReply() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 79 | return proxyMapReply; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean isWantMapNotify() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 84 | return wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 89 | return recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 94 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public short getKeyId() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 99 | return keyId; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public byte[] getAuthenticationData() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 104 | return ImmutableByteSequence.copyFrom(this.authenticationData).asArray(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 108 | public List<LispMapRecord> getLispRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 109 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public static final class DefaultRegisterBuilder implements RegisterBuilder { |
| 113 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 114 | 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 Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 124 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 128 | public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) { |
| 129 | this.proxyMapReply = proxyMapReply; |
| 130 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 134 | public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) { |
| 135 | this.wantMapNotify = wantMapNotify; |
| 136 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public RegisterBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 141 | this.recordCount = recordCount; |
| 142 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public RegisterBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 147 | this.nonce = nonce; |
| 148 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public RegisterBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 153 | this.keyId = keyId; |
| 154 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public RegisterBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 159 | this.authenticationData = authenticationData; |
| 160 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 164 | public RegisterBuilder addRecord(LispMapRecord record) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 165 | this.mapRecords.add(record); |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame^] | 170 | public LispMapRegister build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 171 | return new DefaultLispMapRegister(nonce, keyId, authenticationData, |
| 172 | recordCount, mapRecords, proxyMapReply, wantMapNotify); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 173 | } |
| 174 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 175 | } |