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