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 | a7b394d | 2016-08-21 23:11:46 +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 | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 22 | import org.onlab.util.ByteOperator; |
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; |
| 25 | |
| 26 | import java.util.List; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 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 reply message class. |
| 32 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 33 | public final class DefaultLispMapReply implements LispMapReply { |
| 34 | |
| 35 | private final long nonce; |
| 36 | private final byte recordCount; |
| 37 | private final boolean probe; |
| 38 | private final boolean etr; |
| 39 | private final boolean security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 40 | private final List<LispMapRecord> mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * A private constructor that protects object instantiation from external. |
| 44 | * |
| 45 | * @param nonce nonce |
| 46 | * @param recordCount record count number |
| 47 | * @param probe probe flag |
| 48 | * @param etr etr flag |
| 49 | * @param security security flag |
| 50 | */ |
| 51 | private DefaultLispMapReply(long nonce, byte recordCount, boolean probe, |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 52 | boolean etr, boolean security, List<LispMapRecord> mapRecords) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 53 | this.nonce = nonce; |
| 54 | this.recordCount = recordCount; |
| 55 | this.probe = probe; |
| 56 | this.etr = etr; |
| 57 | this.security = security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 58 | this.mapRecords = mapRecords; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 59 | } |
| 60 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 61 | @Override |
| 62 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 63 | return LispType.LISP_MAP_REPLY; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 68 | // TODO: serialize LispMapReply message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 73 | return new DefaultReplyBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 74 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public boolean isProbe() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 78 | return this.probe; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean isEtr() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 83 | return this.etr; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean isSecurity() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 88 | return this.security; |
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 this.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 this.nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 101 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 102 | public List<LispMapRecord> getMapRecords() { |
| 103 | return ImmutableList.copyOf(mapRecords); |
| 104 | } |
| 105 | |
| 106 | @Override |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 107 | public String toString() { |
| 108 | return toStringHelper(this) |
| 109 | .add("type", getType()) |
| 110 | .add("nonce", nonce) |
| 111 | .add("recordCount", recordCount) |
| 112 | .add("probe", probe) |
| 113 | .add("etr", etr) |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 114 | .add("security", security) |
| 115 | .add("map records", mapRecords).toString(); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object o) { |
| 120 | if (this == o) { |
| 121 | return true; |
| 122 | } |
| 123 | if (o == null || getClass() != o.getClass()) { |
| 124 | return false; |
| 125 | } |
| 126 | DefaultLispMapReply that = (DefaultLispMapReply) o; |
| 127 | return Objects.equal(nonce, that.nonce) && |
| 128 | Objects.equal(recordCount, that.recordCount) && |
| 129 | Objects.equal(probe, that.probe) && |
| 130 | Objects.equal(etr, that.etr) && |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 131 | Objects.equal(security, that.security) && |
| 132 | Objects.equal(mapRecords, that.mapRecords); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public int hashCode() { |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 137 | return Objects.hashCode(nonce, recordCount, probe, etr, security, mapRecords); |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 140 | public static final class DefaultReplyBuilder implements ReplyBuilder { |
| 141 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 142 | private long nonce; |
| 143 | private byte recordCount; |
| 144 | private boolean probe; |
| 145 | private boolean etr; |
| 146 | private boolean security; |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 147 | private List<LispMapRecord> mapRecords; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 151 | return LispType.LISP_MAP_REPLY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 155 | public ReplyBuilder withIsProbe(boolean probe) { |
| 156 | this.probe = probe; |
| 157 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 161 | public ReplyBuilder withIsEtr(boolean etr) { |
| 162 | this.etr = etr; |
| 163 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 167 | public ReplyBuilder withIsSecurity(boolean security) { |
| 168 | this.security = security; |
| 169 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public ReplyBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 174 | this.recordCount = recordCount; |
| 175 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public ReplyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 180 | this.nonce = nonce; |
| 181 | return this; |
| 182 | } |
| 183 | |
| 184 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 185 | public ReplyBuilder withMapRecords(List<LispMapRecord> mapRecords) { |
| 186 | this.mapRecords = ImmutableList.copyOf(mapRecords); |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 191 | public LispMapReply build() { |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 192 | return new DefaultLispMapReply(nonce, recordCount, probe, etr, security, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 193 | } |
| 194 | } |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * A private LISP message reader for MapReply message. |
| 198 | */ |
| 199 | private static class ReplyReader implements LispMessageReader<LispMapReply> { |
| 200 | |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 201 | private static final int PROBE_INDEX = 3; |
| 202 | private static final int ETR_INDEX = 2; |
| 203 | private static final int SECURITY_INDEX = 1; |
| 204 | private static final int RESERVED_SKIP_LENGTH = 2; |
| 205 | |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 206 | @Override |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 207 | public LispMapReply readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException { |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 208 | |
| 209 | if (byteBuf.readerIndex() != 0) { |
| 210 | return null; |
| 211 | } |
| 212 | |
| 213 | byte typeWithFlags = byteBuf.readByte(); |
| 214 | |
| 215 | // probe -> 1 bit |
| 216 | boolean probe = ByteOperator.getBit(typeWithFlags, PROBE_INDEX); |
| 217 | |
| 218 | // etr -> 1bit |
| 219 | boolean etr = ByteOperator.getBit(typeWithFlags, ETR_INDEX); |
| 220 | |
| 221 | // security -> 1 bit |
| 222 | boolean security = ByteOperator.getBit(typeWithFlags, SECURITY_INDEX); |
| 223 | |
| 224 | // skip two bytes as they represent reserved fields |
| 225 | byteBuf.skipBytes(RESERVED_SKIP_LENGTH); |
| 226 | |
| 227 | // record count -> 8 bits |
| 228 | byte recordCount = (byte) byteBuf.readUnsignedByte(); |
| 229 | |
| 230 | // nonce -> 64 bits |
| 231 | long nonce = byteBuf.readLong(); |
| 232 | |
Jian Li | a7b394d | 2016-08-21 23:11:46 +0900 | [diff] [blame^] | 233 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 234 | for (int i = 0; i < recordCount; i++) { |
| 235 | mapRecords.add(new DefaultLispMapRecord.MapRecordReader().readFrom(byteBuf)); |
| 236 | } |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 237 | |
| 238 | return new DefaultReplyBuilder() |
| 239 | .withIsProbe(probe) |
| 240 | .withIsEtr(etr) |
| 241 | .withIsSecurity(security) |
| 242 | .withRecordCount(recordCount) |
| 243 | .withNonce(nonce) |
| 244 | .build(); |
Jian Li | 26069e2 | 2016-08-10 22:00:52 +0900 | [diff] [blame] | 245 | } |
| 246 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 247 | } |