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