Jian Li | 10a0906 | 2016-07-26 23:58:50 +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 | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame^] | 18 | import com.google.common.base.Objects; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 19 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 20 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame^] | 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 22 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 23 | /** |
| 24 | * Default implementation of LispMapRecord. |
| 25 | */ |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 26 | public final class DefaultLispMapRecord implements LispMapRecord { |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 27 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 28 | private final int recordTtl; |
| 29 | private final int locatorCount; |
| 30 | private final byte maskLength; |
| 31 | private final LispMapReplyAction action; |
| 32 | private final boolean authoritative; |
| 33 | private final short mapVersionNumber; |
| 34 | private final LispAfiAddress eidPrefixAfi; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 35 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 36 | /** |
| 37 | * A private constructor that protects object instantiation from external. |
| 38 | * |
| 39 | * @param recordTtl record time-to-live value |
| 40 | * @param locatorCount locator's count number |
| 41 | * @param maskLength mask length |
| 42 | * @param action lisp map reply action |
| 43 | * @param authoritative authoritative flag |
| 44 | * @param mapVersionNumber map version number |
| 45 | * @param eidPrefixAfi EID prefix AFI address |
| 46 | */ |
| 47 | private DefaultLispMapRecord(int recordTtl, int locatorCount, byte maskLength, |
| 48 | LispMapReplyAction action, boolean authoritative, |
| 49 | short mapVersionNumber, LispAfiAddress eidPrefixAfi) { |
| 50 | this.recordTtl = recordTtl; |
| 51 | this.locatorCount = locatorCount; |
| 52 | this.maskLength = maskLength; |
| 53 | this.action = action; |
| 54 | this.authoritative = authoritative; |
| 55 | this.mapVersionNumber = mapVersionNumber; |
| 56 | this.eidPrefixAfi = eidPrefixAfi; |
| 57 | } |
| 58 | |
| 59 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 60 | public int getRecordTtl() { |
| 61 | return recordTtl; |
| 62 | } |
| 63 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 64 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 65 | public int getLocatorCount() { |
| 66 | return locatorCount; |
| 67 | } |
| 68 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 69 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 70 | public byte getMaskLength() { |
| 71 | return maskLength; |
| 72 | } |
| 73 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 74 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 75 | public LispMapReplyAction getAction() { |
| 76 | return action; |
| 77 | } |
| 78 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 79 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 80 | public boolean isAuthoritative() { |
| 81 | return authoritative; |
| 82 | } |
| 83 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 84 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 85 | public short getMapVersionNumber() { |
| 86 | return mapVersionNumber; |
| 87 | } |
| 88 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 89 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 90 | public LispAfiAddress getEidPrefixAfi() { |
| 91 | return eidPrefixAfi; |
| 92 | } |
| 93 | |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame^] | 94 | @Override |
| 95 | public String toString() { |
| 96 | return toStringHelper(this) |
| 97 | .add("record TTL", recordTtl) |
| 98 | .add("locatorCount", locatorCount) |
| 99 | .add("maskLength", maskLength) |
| 100 | .add("action", action) |
| 101 | .add("authoritative", authoritative) |
| 102 | .add("mapVersionNumber", mapVersionNumber) |
| 103 | .add("EID prefix AFI address", eidPrefixAfi).toString(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean equals(Object o) { |
| 108 | if (this == o) { |
| 109 | return true; |
| 110 | } |
| 111 | if (o == null || getClass() != o.getClass()) { |
| 112 | return false; |
| 113 | } |
| 114 | DefaultLispMapRecord that = (DefaultLispMapRecord) o; |
| 115 | return Objects.equal(recordTtl, that.recordTtl) && |
| 116 | Objects.equal(locatorCount, that.locatorCount) && |
| 117 | Objects.equal(maskLength, that.maskLength) && |
| 118 | Objects.equal(action, that.action) && |
| 119 | Objects.equal(authoritative, that.authoritative) && |
| 120 | Objects.equal(mapVersionNumber, that.mapVersionNumber) && |
| 121 | Objects.equal(eidPrefixAfi, that.eidPrefixAfi); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int hashCode() { |
| 126 | return Objects.hashCode(recordTtl, locatorCount, maskLength, action, |
| 127 | authoritative, mapVersionNumber, eidPrefixAfi); |
| 128 | } |
| 129 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 130 | public static final class DefaultMapRecordBuilder implements MapRecordBuilder { |
| 131 | |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 132 | private int recordTtl; |
| 133 | private int locatorCount; |
| 134 | private byte maskLength; |
| 135 | private LispMapReplyAction action; |
| 136 | private boolean authoritative; |
| 137 | private short mapVersionNumber; |
| 138 | private LispAfiAddress eidPrefixAfi; |
| 139 | |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 140 | @Override |
| 141 | public MapRecordBuilder withRecordTtl(int recordTtl) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 142 | this.recordTtl = recordTtl; |
| 143 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public MapRecordBuilder withLocatorCount(int locatorCount) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 148 | this.locatorCount = locatorCount; |
| 149 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public MapRecordBuilder withMaskLength(byte maskLength) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 154 | this.maskLength = maskLength; |
| 155 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public MapRecordBuilder withAction(LispMapReplyAction action) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 160 | this.action = action; |
| 161 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public MapRecordBuilder withAuthoritative(boolean authoritative) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 166 | this.authoritative = authoritative; |
| 167 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public MapRecordBuilder withMapVersionNumber(short mapVersionNumber) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 172 | this.mapVersionNumber = mapVersionNumber; |
| 173 | return this; |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public MapRecordBuilder withEidPrefixAfi(LispAfiAddress prefix) { |
Jian Li | 631e62a | 2016-08-03 22:42:00 +0900 | [diff] [blame] | 178 | this.eidPrefixAfi = prefix; |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public LispMapRecord build() { |
| 184 | return new DefaultLispMapRecord(recordTtl, locatorCount, maskLength, |
| 185 | action, authoritative, mapVersionNumber, eidPrefixAfi); |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |