blob: 6f003764d74ecef0932b8ccdc32fe6361d245930 [file] [log] [blame]
Jian Li10a09062016-07-26 23:58:50 +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 Li18f3bce2016-08-04 17:36:41 +090018import com.google.common.base.Objects;
Jian Li10a09062016-07-26 23:58:50 +090019import org.onosproject.lisp.msg.types.LispAfiAddress;
20
Jian Li18f3bce2016-08-04 17:36:41 +090021import static com.google.common.base.MoreObjects.toStringHelper;
22
Jian Li10a09062016-07-26 23:58:50 +090023/**
24 * Default implementation of LispMapRecord.
25 */
Jian Li631e62a2016-08-03 22:42:00 +090026public final class DefaultLispMapRecord implements LispMapRecord {
Jian Li10a09062016-07-26 23:58:50 +090027
Jian Li631e62a2016-08-03 22:42:00 +090028 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 Li10a09062016-07-26 23:58:50 +090035
Jian Li631e62a2016-08-03 22:42:00 +090036 /**
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 Li10a09062016-07-26 23:58:50 +090060 public int getRecordTtl() {
61 return recordTtl;
62 }
63
Jian Li631e62a2016-08-03 22:42:00 +090064 @Override
Jian Li10a09062016-07-26 23:58:50 +090065 public int getLocatorCount() {
66 return locatorCount;
67 }
68
Jian Li631e62a2016-08-03 22:42:00 +090069 @Override
Jian Li10a09062016-07-26 23:58:50 +090070 public byte getMaskLength() {
71 return maskLength;
72 }
73
Jian Li631e62a2016-08-03 22:42:00 +090074 @Override
Jian Li10a09062016-07-26 23:58:50 +090075 public LispMapReplyAction getAction() {
76 return action;
77 }
78
Jian Li631e62a2016-08-03 22:42:00 +090079 @Override
Jian Li10a09062016-07-26 23:58:50 +090080 public boolean isAuthoritative() {
81 return authoritative;
82 }
83
Jian Li631e62a2016-08-03 22:42:00 +090084 @Override
Jian Li10a09062016-07-26 23:58:50 +090085 public short getMapVersionNumber() {
86 return mapVersionNumber;
87 }
88
Jian Li631e62a2016-08-03 22:42:00 +090089 @Override
Jian Li10a09062016-07-26 23:58:50 +090090 public LispAfiAddress getEidPrefixAfi() {
91 return eidPrefixAfi;
92 }
93
Jian Li18f3bce2016-08-04 17:36:41 +090094 @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 Li10a09062016-07-26 23:58:50 +0900130 public static final class DefaultMapRecordBuilder implements MapRecordBuilder {
131
Jian Li631e62a2016-08-03 22:42:00 +0900132 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 Li10a09062016-07-26 23:58:50 +0900140 @Override
141 public MapRecordBuilder withRecordTtl(int recordTtl) {
Jian Li631e62a2016-08-03 22:42:00 +0900142 this.recordTtl = recordTtl;
143 return this;
Jian Li10a09062016-07-26 23:58:50 +0900144 }
145
146 @Override
147 public MapRecordBuilder withLocatorCount(int locatorCount) {
Jian Li631e62a2016-08-03 22:42:00 +0900148 this.locatorCount = locatorCount;
149 return this;
Jian Li10a09062016-07-26 23:58:50 +0900150 }
151
152 @Override
153 public MapRecordBuilder withMaskLength(byte maskLength) {
Jian Li631e62a2016-08-03 22:42:00 +0900154 this.maskLength = maskLength;
155 return this;
Jian Li10a09062016-07-26 23:58:50 +0900156 }
157
158 @Override
159 public MapRecordBuilder withAction(LispMapReplyAction action) {
Jian Li631e62a2016-08-03 22:42:00 +0900160 this.action = action;
161 return this;
Jian Li10a09062016-07-26 23:58:50 +0900162 }
163
164 @Override
165 public MapRecordBuilder withAuthoritative(boolean authoritative) {
Jian Li631e62a2016-08-03 22:42:00 +0900166 this.authoritative = authoritative;
167 return this;
Jian Li10a09062016-07-26 23:58:50 +0900168 }
169
170 @Override
171 public MapRecordBuilder withMapVersionNumber(short mapVersionNumber) {
Jian Li631e62a2016-08-03 22:42:00 +0900172 this.mapVersionNumber = mapVersionNumber;
173 return this;
Jian Li10a09062016-07-26 23:58:50 +0900174 }
175
176 @Override
177 public MapRecordBuilder withEidPrefixAfi(LispAfiAddress prefix) {
Jian Li631e62a2016-08-03 22:42:00 +0900178 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 Li10a09062016-07-26 23:58:50 +0900186 }
187 }
188}