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