blob: 739b47e10ba48d51bbe389d61b8361fbce79c043 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 * Default infrastructure link model implementation.
29 */
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080030public class DefaultLink extends AbstractProjectableModel implements Link {
tom4c6606f2014-09-07 11:11:21 -070031
tomeadbb462014-09-07 16:10:19 -070032 private final ConnectPoint src;
33 private final ConnectPoint dst;
34 private final Type type;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080035 private final State state;
Ray Milkeyd0dd1352016-01-19 10:58:41 -080036 private final boolean isExpected;
tom4c6606f2014-09-07 11:11:21 -070037
38 /**
Thomas Vachuska57126fe2014-11-11 17:13:24 -080039 * Creates an active infrastructure link using the supplied information.
tom4c6606f2014-09-07 11:11:21 -070040 *
Thomas Vachuska57126fe2014-11-11 17:13:24 -080041 * @param providerId provider identity
42 * @param src link source
43 * @param dst link destination
44 * @param type link type
tomf5d85d42014-10-02 05:27:56 -070045 * @param annotations optional key/value annotations
tom4c6606f2014-09-07 11:11:21 -070046 */
Ray Milkey2693bda2016-01-22 16:08:14 -080047 protected DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
tomf5d85d42014-10-02 05:27:56 -070048 Type type, Annotations... annotations) {
Thomas Vachuska57126fe2014-11-11 17:13:24 -080049 this(providerId, src, dst, type, ACTIVE, false, annotations);
50 }
51
52 /**
53 * Creates an infrastructure link using the supplied information.
54 * Links marked as durable will remain in the inventory when a vanish
55 * message is received and instead will be marked as inactive.
56 *
57 * @param providerId provider identity
58 * @param src link source
59 * @param dst link destination
60 * @param type link type
61 * @param state link state
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -080062 * @param isExpected indicates if the link is preconfigured
Thomas Vachuska57126fe2014-11-11 17:13:24 -080063 * @param annotations optional key/value annotations
64 */
Ray Milkey2693bda2016-01-22 16:08:14 -080065 private DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst,
Ray Milkeyd0dd1352016-01-19 10:58:41 -080066 Type type, State state,
67 boolean isExpected, Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -070068 super(providerId, annotations);
tom4c6606f2014-09-07 11:11:21 -070069 this.src = src;
70 this.dst = dst;
tomeadbb462014-09-07 16:10:19 -070071 this.type = type;
Thomas Vachuska57126fe2014-11-11 17:13:24 -080072 this.state = state;
Ray Milkeyd0dd1352016-01-19 10:58:41 -080073 this.isExpected = isExpected;
tom4c6606f2014-09-07 11:11:21 -070074 }
75
76 @Override
77 public ConnectPoint src() {
78 return src;
79 }
80
81 @Override
82 public ConnectPoint dst() {
83 return dst;
84 }
85
tomeadbb462014-09-07 16:10:19 -070086 @Override
87 public Type type() {
88 return type;
89 }
tom4c6606f2014-09-07 11:11:21 -070090
91 @Override
Thomas Vachuska57126fe2014-11-11 17:13:24 -080092 public State state() {
93 return state;
94 }
95
96 @Override
97 public boolean isDurable() {
Ray Milkeyd0dd1352016-01-19 10:58:41 -080098 return isExpected();
99 }
100
101 @Override
102 public boolean isExpected() {
103 return isExpected;
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800104 }
105
106 // Note: Durability & state are purposefully omitted form equality & hashCode.
107
108 @Override
tom4c6606f2014-09-07 11:11:21 -0700109 public int hashCode() {
tomeadbb462014-09-07 16:10:19 -0700110 return Objects.hash(src, dst, type);
tom4c6606f2014-09-07 11:11:21 -0700111 }
112
113 @Override
114 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700115 if (this == obj) {
116 return true;
117 }
tomeadbb462014-09-07 16:10:19 -0700118 if (obj instanceof DefaultLink) {
tom4c6606f2014-09-07 11:11:21 -0700119 final DefaultLink other = (DefaultLink) obj;
120 return Objects.equals(this.src, other.src) &&
tomeadbb462014-09-07 16:10:19 -0700121 Objects.equals(this.dst, other.dst) &&
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800122 Objects.equals(this.type, other.type) &&
123 Objects.equals(this.isExpected, other.isExpected);
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}