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