blob: 1876db661483542226d9c7e48b1a43b82c8d5096 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom4c6606f2014-09-07 11:11:21 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.provider.ProviderId;
tom4c6606f2014-09-07 11:11:21 -070019
20import java.util.Objects;
21
tomeadbb462014-09-07 16:10:19 -070022import static com.google.common.base.MoreObjects.toStringHelper;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import static org.onosproject.net.Link.State.ACTIVE;
Ray Milkey2693bda2016-01-22 16:08:14 -080024import static org.onosproject.net.DefaultAnnotations.EMPTY;
25import static com.google.common.base.Preconditions.checkNotNull;
26
tom4c6606f2014-09-07 11:11:21 -070027
28/**
29 * Default infrastructure link model implementation.
30 */
tomeadbb462014-09-07 16:10:19 -070031public class DefaultLink extends AbstractModel implements Link {
tom4c6606f2014-09-07 11:11:21 -070032
tomeadbb462014-09-07 16:10:19 -070033 private final ConnectPoint src;
34 private final ConnectPoint dst;
35 private final Type type;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080036 private final State state;
Ray Milkeyd0dd1352016-01-19 10:58:41 -080037 private final boolean isExpected;
tom4c6606f2014-09-07 11:11:21 -070038
39 /**
Thomas Vachuska57126fe2014-11-11 17:13:24 -080040 * Creates an active infrastructure link using the supplied information.
tom4c6606f2014-09-07 11:11:21 -070041 *
Thomas Vachuska57126fe2014-11-11 17:13:24 -080042 * @param providerId provider identity
43 * @param src link source
44 * @param dst link destination
45 * @param type link type
tomf5d85d42014-10-02 05:27:56 -070046 * @param annotations optional key/value annotations
tom4c6606f2014-09-07 11:11:21 -070047 */
Ray Milkey2693bda2016-01-22 16:08:14 -080048 protected DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
tomf5d85d42014-10-02 05:27:56 -070049 Type type, Annotations... annotations) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -080050 this(providerId, src, dst, type, ACTIVE, false, annotations);
51 }
52
53 /**
54 * Creates an infrastructure link using the supplied information.
55 * Links marked as durable will remain in the inventory when a vanish
56 * message is received and instead will be marked as inactive.
57 *
58 * @param providerId provider identity
59 * @param src link source
60 * @param dst link destination
61 * @param type link type
62 * @param state link state
Ray Milkeyd0dd1352016-01-19 10:58:41 -080063 * @param isExpected indicates if the link is preconfigured
Thomas Vachuska57126fe2014-11-11 17:13:24 -080064 * @param annotations optional key/value annotations
65 */
Ray Milkey2693bda2016-01-22 16:08:14 -080066 private DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
Ray Milkeyd0dd1352016-01-19 10:58:41 -080067 Type type, State state,
68 boolean isExpected, Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070069 super(providerId, annotations);
tom4c6606f2014-09-07 11:11:21 -070070 this.src = src;
71 this.dst = dst;
tomeadbb462014-09-07 16:10:19 -070072 this.type = type;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080073 this.state = state;
Ray Milkeyd0dd1352016-01-19 10:58:41 -080074 this.isExpected = isExpected;
tom4c6606f2014-09-07 11:11:21 -070075 }
76
77 @Override
78 public ConnectPoint src() {
79 return src;
80 }
81
82 @Override
83 public ConnectPoint dst() {
84 return dst;
85 }
86
tomeadbb462014-09-07 16:10:19 -070087 @Override
88 public Type type() {
89 return type;
90 }
tom4c6606f2014-09-07 11:11:21 -070091
92 @Override
Thomas Vachuska57126fe2014-11-11 17:13:24 -080093 public State state() {
94 return state;
95 }
96
97 @Override
98 public boolean isDurable() {
Ray Milkeyd0dd1352016-01-19 10:58:41 -080099 return isExpected();
100 }
101
102 @Override
103 public boolean isExpected() {
104 return isExpected;
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800105 }
106
107 // Note: Durability & state are purposefully omitted form equality & hashCode.
108
109 @Override
tom4c6606f2014-09-07 11:11:21 -0700110 public int hashCode() {
tomeadbb462014-09-07 16:10:19 -0700111 return Objects.hash(src, dst, type);
tom4c6606f2014-09-07 11:11:21 -0700112 }
113
114 @Override
115 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700116 if (this == obj) {
117 return true;
118 }
tomeadbb462014-09-07 16:10:19 -0700119 if (obj instanceof DefaultLink) {
tom4c6606f2014-09-07 11:11:21 -0700120 final DefaultLink other = (DefaultLink) obj;
121 return Objects.equals(this.src, other.src) &&
tomeadbb462014-09-07 16:10:19 -0700122 Objects.equals(this.dst, other.dst) &&
123 Objects.equals(this.type, other.type);
tom4c6606f2014-09-07 11:11:21 -0700124 }
125 return false;
126 }
127
128 @Override
129 public String toString() {
130 return toStringHelper(this)
131 .add("src", src)
132 .add("dst", dst)
tomeadbb462014-09-07 16:10:19 -0700133 .add("type", type)
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800134 .add("state", state)
Ray Milkey2693bda2016-01-22 16:08:14 -0800135 .add("expected", isExpected)
tom4c6606f2014-09-07 11:11:21 -0700136 .toString();
137 }
138
Ray Milkey2693bda2016-01-22 16:08:14 -0800139 /**
140 * Creates a new default link builder.
141 *
142 * @return default link builder
143 */
144 public static Builder builder() {
145 return new Builder();
146 }
147
148 /**
149 * Builder for DefaultLink objects.
150 */
151 public static final class Builder {
152 private ProviderId providerId;
153 private Annotations annotations = EMPTY;
154 private ConnectPoint src;
155 private ConnectPoint dst;
156 private Type type;
157 private State state = ACTIVE;
158 private boolean isExpected = false;
159
160 private Builder() {
161 // Hide constructor
162 }
163
164 /**
165 * Sets the providerId to be used by the builder.
166 *
167 * @param providerId new provider id
168 * @return self
169 */
170 public Builder providerId(ProviderId providerId) {
171 this.providerId = providerId;
172 return this;
173 }
174
175 /**
176 * Sets the annotations to be used by the builder.
177 *
178 * @param annotations new annotations
179 * @return self
180 */
181 public Builder annotations(Annotations annotations) {
182 this.annotations = annotations;
183 return this;
184 }
185
186 /**
187 * Sets the source connect point to be used by the builder.
188 *
189 * @param src source connect point
190 * @return self
191 */
192 public Builder src(ConnectPoint src) {
193 this.src = src;
194 return this;
195 }
196
197 /**
198 * Sets the destination connect point to be used by the builder.
199 *
200 * @param dst new destination connect point
201 * @return self
202 */
203 public Builder dst(ConnectPoint dst) {
204 this.dst = dst;
205 return this;
206 }
207
208 /**
209 * Sets the link type to be used by the builder.
210 *
211 * @param type new link type
212 * @return self
213 */
214 public Builder type(Type type) {
215 this.type = type;
216 return this;
217 }
218
219 /**
220 * Sets the link state to be used by the builder.
221 *
222 * @param state new link state
223 * @return self
224 */
225 public Builder state(State state) {
226 this.state = state;
227 return this;
228 }
229
230 /**
231 * Sets the expected flag to be used by the builder.
232 *
233 * @param isExpected new expected flag
234 * @return self
235 */
236 public Builder isExpected(boolean isExpected) {
237 this.isExpected = isExpected;
238 return this;
239 }
240
241 /**
242 * Builds a default link object from the accumulated parameters.
243 *
244 * @return default link object
245 */
246 public DefaultLink build() {
247 checkNotNull(src, "Source connect point cannot be null");
248 checkNotNull(dst, "Destination connect point cannot be null");
249 checkNotNull(type, "Type cannot be null");
250 checkNotNull(providerId, "Provider Id cannot be null");
251
252 return new DefaultLink(providerId, src, dst,
253 type, state,
254 isExpected, annotations);
255 }
256
257 }
258
259
260
tom4c6606f2014-09-07 11:11:21 -0700261}