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 | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 18 | import com.google.common.base.Objects; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 22 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame^] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 24 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 25 | import java.util.List; |
| 26 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 29 | /** |
| 30 | * Default LISP map register message class. |
| 31 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 32 | public final class DefaultLispMapRegister implements LispMapRegister { |
| 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 | private final boolean proxyMapReply; |
| 40 | private final boolean wantMapNotify; |
| 41 | |
| 42 | /** |
| 43 | * A private constructor that protects object instantiation from external. |
| 44 | * |
| 45 | * @param nonce nonce |
| 46 | * @param keyId key identifier |
| 47 | * @param authenticationData authentication data |
| 48 | * @param recordCount record count number |
| 49 | * @param mapRecords a collection of map records |
| 50 | * @param proxyMapReply proxy map reply flag |
| 51 | * @param wantMapNotify want map notify flag |
| 52 | */ |
| 53 | private DefaultLispMapRegister(long nonce, short keyId, |
| 54 | byte[] authenticationData, byte recordCount, |
| 55 | List<LispMapRecord> mapRecords, |
| 56 | boolean proxyMapReply, boolean wantMapNotify) { |
| 57 | this.nonce = nonce; |
| 58 | this.keyId = keyId; |
| 59 | this.authenticationData = authenticationData; |
| 60 | this.recordCount = recordCount; |
| 61 | this.mapRecords = mapRecords; |
| 62 | this.proxyMapReply = proxyMapReply; |
| 63 | this.wantMapNotify = wantMapNotify; |
| 64 | } |
| 65 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 66 | @Override |
| 67 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 68 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 73 | // TODO: serialize LispMapRegister message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 78 | return new DefaultRegisterBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 79 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public boolean isProxyMapReply() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 83 | return proxyMapReply; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean isWantMapNotify() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 88 | return wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 93 | return recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 98 | return nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public short getKeyId() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 103 | return keyId; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public byte[] getAuthenticationData() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 108 | return ImmutableByteSequence.copyFrom(this.authenticationData).asArray(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 112 | public List<LispMapRecord> getLispRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 113 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 116 | @Override |
| 117 | public String toString() { |
| 118 | return toStringHelper(this) |
| 119 | .add("type", getType()) |
| 120 | .add("nonce", nonce) |
| 121 | .add("recordCount", recordCount) |
| 122 | .add("keyId", keyId) |
| 123 | .add("mapRecords", mapRecords) |
| 124 | .add("proxyMapReply", proxyMapReply) |
| 125 | .add("wantMapNotify", wantMapNotify).toString(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean equals(Object o) { |
| 130 | if (this == o) { |
| 131 | return true; |
| 132 | } |
| 133 | if (o == null || getClass() != o.getClass()) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | DefaultLispMapRegister that = (DefaultLispMapRegister) o; |
| 138 | return Objects.equal(nonce, that.nonce) && |
| 139 | Objects.equal(recordCount, that.recordCount) && |
| 140 | Objects.equal(keyId, that.keyId) && |
| 141 | Objects.equal(authenticationData, that.authenticationData) && |
| 142 | Objects.equal(proxyMapReply, that.proxyMapReply) && |
| 143 | Objects.equal(wantMapNotify, that.wantMapNotify); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public int hashCode() { |
| 148 | return Objects.hashCode(nonce, recordCount, keyId, authenticationData, |
| 149 | proxyMapReply, wantMapNotify); |
| 150 | } |
| 151 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 152 | public static final class DefaultRegisterBuilder implements RegisterBuilder { |
| 153 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 154 | private long nonce; |
| 155 | private short keyId; |
| 156 | private byte[] authenticationData; |
| 157 | private byte recordCount; |
| 158 | private final List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 159 | private boolean proxyMapReply; |
| 160 | private boolean wantMapNotify; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 161 | |
| 162 | @Override |
| 163 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 164 | return LispType.LISP_MAP_REGISTER; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 168 | public RegisterBuilder withIsProxyMapReply(boolean proxyMapReply) { |
| 169 | this.proxyMapReply = proxyMapReply; |
| 170 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 174 | public RegisterBuilder withIsWantMapNotify(boolean wantMapNotify) { |
| 175 | this.wantMapNotify = wantMapNotify; |
| 176 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public RegisterBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 181 | this.recordCount = recordCount; |
| 182 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public RegisterBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 187 | this.nonce = nonce; |
| 188 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public RegisterBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 193 | this.keyId = keyId; |
| 194 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public RegisterBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 199 | this.authenticationData = authenticationData; |
| 200 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 204 | public RegisterBuilder addRecord(LispMapRecord record) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 205 | this.mapRecords.add(record); |
| 206 | return this; |
| 207 | } |
| 208 | |
| 209 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 210 | public LispMapRegister build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 211 | return new DefaultLispMapRegister(nonce, keyId, authenticationData, |
| 212 | recordCount, mapRecords, proxyMapReply, wantMapNotify); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 213 | } |
| 214 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame^] | 215 | |
| 216 | /** |
| 217 | * A private LISP message reader for MapRegister message. |
| 218 | */ |
| 219 | private static class RegisterReader implements LispMessageReader<LispMapRegister> { |
| 220 | |
| 221 | @Override |
| 222 | public LispMapRegister readFrom(ByteBuf byteBuf) throws LispParseError { |
| 223 | return null; |
| 224 | } |
| 225 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 226 | } |