Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 18 | import com.google.common.base.Objects; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import io.netty.buffer.ByteBuf; |
| 22 | import org.onlab.packet.DeserializationException; |
| 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 26 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Default LISP referral record class. |
| 35 | */ |
| 36 | public final class DefaultLispReferralRecord extends AbstractLispRecord |
| 37 | implements LispReferralRecord { |
| 38 | |
| 39 | private final boolean incomplete; |
| 40 | private final List<LispReferral> referrals; |
| 41 | private final List<LispSignature> signatures; |
| 42 | |
| 43 | static final ReferralRecordWriter WRITER; |
| 44 | static { |
| 45 | WRITER = new ReferralRecordWriter(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * A private constructor that protects object instantiation from external. |
| 50 | * |
| 51 | * @param recordTtl record time-to-live value |
| 52 | * @param maskLength mask length |
| 53 | * @param action lisp map reply action |
| 54 | * @param authoritative authoritative flag |
| 55 | * @param mapVersionNumber map version number |
| 56 | * @param eidPrefixAfi EID prefix AFI address |
| 57 | * @param incomplete incomplete flag value |
| 58 | * @param referrals a collection referrals |
| 59 | * @param signatures a collection signatures |
| 60 | */ |
| 61 | private DefaultLispReferralRecord(int recordTtl, byte maskLength, |
| 62 | LispMapReplyAction action, boolean authoritative, |
| 63 | short mapVersionNumber, LispAfiAddress eidPrefixAfi, |
| 64 | boolean incomplete, List<LispReferral> referrals, |
| 65 | List<LispSignature> signatures) { |
| 66 | super(recordTtl, maskLength, action, authoritative, mapVersionNumber, eidPrefixAfi); |
| 67 | this.incomplete = incomplete; |
| 68 | this.referrals = referrals; |
| 69 | this.signatures = signatures; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int getReferralCount() { |
| 74 | return referrals.size(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int getSignatureCount() { |
| 79 | return signatures.size(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean isIncomplete() { |
| 84 | return incomplete; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public List<LispReferral> getReferrals() { |
| 89 | return ImmutableList.copyOf(referrals); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public List<LispSignature> getSignatures() { |
| 94 | return ImmutableList.copyOf(signatures); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 99 | WRITER.writeTo(byteBuf, this); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return toStringHelper(this) |
| 105 | .add("record TTL", recordTtl) |
| 106 | .add("maskLength", maskLength) |
| 107 | .add("action", action) |
| 108 | .add("authoritative", authoritative) |
| 109 | .add("mapVersionNumber", mapVersionNumber) |
| 110 | .add("EID prefix AFI address", eidPrefixAfi) |
| 111 | .add("incomplete", incomplete) |
| 112 | .add("referrals", referrals) |
| 113 | .add("signatures", signatures) |
| 114 | .toString(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean equals(Object o) { |
| 119 | if (this == o) { |
| 120 | return true; |
| 121 | } |
| 122 | if (o == null || getClass() != o.getClass()) { |
| 123 | return false; |
| 124 | } |
| 125 | DefaultLispReferralRecord that = (DefaultLispReferralRecord) o; |
| 126 | return Objects.equal(recordTtl, that.recordTtl) && |
| 127 | Objects.equal(maskLength, that.maskLength) && |
| 128 | Objects.equal(action, that.action) && |
| 129 | Objects.equal(authoritative, that.authoritative) && |
| 130 | Objects.equal(mapVersionNumber, that.mapVersionNumber) && |
| 131 | Objects.equal(eidPrefixAfi, that.eidPrefixAfi) && |
| 132 | Objects.equal(referrals, that.referrals) && |
| 133 | Objects.equal(signatures, that.signatures); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int hashCode() { |
| 138 | return Objects.hashCode(recordTtl, maskLength, action, authoritative, |
| 139 | mapVersionNumber, eidPrefixAfi, incomplete, referrals, signatures); |
| 140 | } |
| 141 | |
| 142 | public static final class DefaultReferralRecordBuilder |
| 143 | extends AbstractRecordBuilder<ReferralRecordBuilder> |
| 144 | implements ReferralRecordBuilder { |
| 145 | |
| 146 | private boolean incomplete; |
| 147 | private List<LispReferral> referrals = Lists.newArrayList(); |
| 148 | private List<LispSignature> signatures = Lists.newArrayList(); |
| 149 | |
| 150 | @Override |
| 151 | public ReferralRecordBuilder withReferrals(List<LispReferral> referrals) { |
| 152 | if (referrals != null) { |
| 153 | this.referrals = ImmutableList.copyOf(referrals); |
| 154 | } |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public ReferralRecordBuilder withSignatures(List<LispSignature> signatures) { |
| 160 | if (signatures != null) { |
| 161 | this.signatures = ImmutableList.copyOf(signatures); |
| 162 | } |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public ReferralRecordBuilder withIsIncomplete(boolean incomplete) { |
| 168 | this.incomplete = incomplete; |
| 169 | return this; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public LispReferralRecord build() { |
| 174 | |
| 175 | checkNotNull(eidPrefixAfi, "Must specify an EID prefix"); |
| 176 | |
| 177 | return new DefaultLispReferralRecord(recordTtl, maskLength, action, |
| 178 | authoritative, mapVersionNumber, eidPrefixAfi, |
| 179 | incomplete, referrals, signatures); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public static final class ReferralRecordReader |
| 184 | implements LispMessageReader<LispReferralRecord> { |
| 185 | |
| 186 | @Override |
| 187 | public LispReferralRecord readFrom(ByteBuf byteBuf) |
| 188 | throws LispParseError, LispReaderException, |
| 189 | DeserializationException { |
| 190 | // TODO: need to implement serialization logic |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * A LISP message writer for ReferralRecord portion. |
| 197 | */ |
| 198 | public static final class ReferralRecordWriter |
| 199 | implements LispMessageWriter<LispReferralRecord> { |
| 200 | |
| 201 | @Override |
| 202 | public void writeTo(ByteBuf byteBuf, LispReferralRecord message) |
| 203 | throws LispWriterException { |
| 204 | // TODO: need to implement serialization logic |
| 205 | } |
| 206 | } |
| 207 | } |