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