blob: 6e5e218e91bd9adf39ce33c3cb6090ffa8321ac5 [file] [log] [blame]
Jian Li451175e2016-07-19 23:22:20 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
Jian Li20850d32016-08-04 02:15:57 +090018import com.google.common.base.Objects;
Jian Li451175e2016-07-19 23:22:20 +090019import io.netty.buffer.ByteBuf;
Jian Li47671902016-08-11 01:18:18 +090020import org.onlab.util.ByteOperator;
Jian Li26069e22016-08-10 22:00:52 +090021import org.onosproject.lisp.msg.exceptions.LispParseError;
Jian Li451175e2016-07-19 23:22:20 +090022
Jian Li20850d32016-08-04 02:15:57 +090023import static com.google.common.base.MoreObjects.toStringHelper;
24
Jian Li451175e2016-07-19 23:22:20 +090025/**
26 * Default LISP map reply message class.
27 */
Jian Lif59c0ad2016-08-02 18:11:30 +090028public 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 Li451175e2016-07-19 23:22:20 +090054 @Override
55 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +090056 return LispType.LISP_MAP_REPLY;
Jian Li451175e2016-07-19 23:22:20 +090057 }
58
59 @Override
60 public void writeTo(ByteBuf byteBuf) {
Jian Lif59c0ad2016-08-02 18:11:30 +090061 // TODO: serialize LispMapReply message
Jian Li451175e2016-07-19 23:22:20 +090062 }
63
64 @Override
65 public Builder createBuilder() {
Jian Li525fded2016-08-04 01:15:33 +090066 return new DefaultReplyBuilder();
Jian Li451175e2016-07-19 23:22:20 +090067 }
Jian Li719b3bf2016-07-22 00:38:29 +090068
69 @Override
70 public boolean isProbe() {
Jian Lif59c0ad2016-08-02 18:11:30 +090071 return this.probe;
Jian Li719b3bf2016-07-22 00:38:29 +090072 }
73
74 @Override
75 public boolean isEtr() {
Jian Lif59c0ad2016-08-02 18:11:30 +090076 return this.etr;
Jian Li719b3bf2016-07-22 00:38:29 +090077 }
78
79 @Override
80 public boolean isSecurity() {
Jian Lif59c0ad2016-08-02 18:11:30 +090081 return this.security;
Jian Li719b3bf2016-07-22 00:38:29 +090082 }
83
84 @Override
85 public byte getRecordCount() {
Jian Lif59c0ad2016-08-02 18:11:30 +090086 return this.recordCount;
Jian Li719b3bf2016-07-22 00:38:29 +090087 }
88
89 @Override
90 public long getNonce() {
Jian Lif59c0ad2016-08-02 18:11:30 +090091 return this.nonce;
Jian Li719b3bf2016-07-22 00:38:29 +090092 }
93
Jian Li20850d32016-08-04 02:15:57 +090094 @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 Li719b3bf2016-07-22 00:38:29 +0900126 public static final class DefaultReplyBuilder implements ReplyBuilder {
127
Jian Lif59c0ad2016-08-02 18:11:30 +0900128 private long nonce;
129 private byte recordCount;
130 private boolean probe;
131 private boolean etr;
132 private boolean security;
Jian Li719b3bf2016-07-22 00:38:29 +0900133
134 @Override
135 public LispType getType() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900136 return LispType.LISP_MAP_REPLY;
Jian Li719b3bf2016-07-22 00:38:29 +0900137 }
138
139 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900140 public ReplyBuilder withIsProbe(boolean probe) {
141 this.probe = probe;
142 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900143 }
144
145 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900146 public ReplyBuilder withIsEtr(boolean etr) {
147 this.etr = etr;
148 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900149 }
150
151 @Override
Jian Lif59c0ad2016-08-02 18:11:30 +0900152 public ReplyBuilder withIsSecurity(boolean security) {
153 this.security = security;
154 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900155 }
156
157 @Override
158 public ReplyBuilder withRecordCount(byte recordCount) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900159 this.recordCount = recordCount;
160 return this;
Jian Li719b3bf2016-07-22 00:38:29 +0900161 }
162
163 @Override
164 public ReplyBuilder withNonce(long nonce) {
Jian Lif59c0ad2016-08-02 18:11:30 +0900165 this.nonce = nonce;
166 return this;
167 }
168
169 @Override
Jian Li525fded2016-08-04 01:15:33 +0900170 public LispMapReply build() {
Jian Lif59c0ad2016-08-02 18:11:30 +0900171 return new DefaultLispMapReply(nonce, recordCount, probe, etr, security);
Jian Li719b3bf2016-07-22 00:38:29 +0900172 }
173 }
Jian Li26069e22016-08-10 22:00:52 +0900174
175 /**
176 * A private LISP message reader for MapReply message.
177 */
178 private static class ReplyReader implements LispMessageReader<LispMapReply> {
179
Jian Li47671902016-08-11 01:18:18 +0900180 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 Li26069e22016-08-10 22:00:52 +0900185 @Override
186 public LispMapReply readFrom(ByteBuf byteBuf) throws LispParseError {
Jian Li47671902016-08-11 01:18:18 +0900187
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 Li26069e22016-08-10 22:00:52 +0900221 }
222 }
Jian Li451175e2016-07-19 23:22:20 +0900223}