blob: f065b8562a0a2e696bfbb50823da9c758b64c091 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 /**
Brian Stanke612cebf2016-05-02 10:21:33 -040039 * Creates an 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
Brian Stanke612cebf2016-05-02 10:21:33 -040045 * @param state link state
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,
Brian Stanke612cebf2016-05-02 10:21:33 -040049 Type type, State state, Annotations... annotations) {
50 this(providerId, src, dst, type, state, false, annotations);
Thomas Vachuska57126fe2014-11-11 17:13:24 -080051 }
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
Thomas Vachuska3afbc7f2016-02-01 15:55:38 -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
Ray Milkeyd0dd1352016-01-19 10:58:41 -080098 public boolean isExpected() {
99 return isExpected;
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800100 }
101
102 // Note: Durability & state are purposefully omitted form equality & hashCode.
103
104 @Override
tom4c6606f2014-09-07 11:11:21 -0700105 public int hashCode() {
tomeadbb462014-09-07 16:10:19 -0700106 return Objects.hash(src, dst, type);
tom4c6606f2014-09-07 11:11:21 -0700107 }
108
109 @Override
110 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700111 if (this == obj) {
112 return true;
113 }
tomeadbb462014-09-07 16:10:19 -0700114 if (obj instanceof DefaultLink) {
tom4c6606f2014-09-07 11:11:21 -0700115 final DefaultLink other = (DefaultLink) obj;
116 return Objects.equals(this.src, other.src) &&
tomeadbb462014-09-07 16:10:19 -0700117 Objects.equals(this.dst, other.dst) &&
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800118 Objects.equals(this.type, other.type) &&
119 Objects.equals(this.isExpected, other.isExpected);
tom4c6606f2014-09-07 11:11:21 -0700120 }
121 return false;
122 }
123
124 @Override
125 public String toString() {
126 return toStringHelper(this)
127 .add("src", src)
128 .add("dst", dst)
tomeadbb462014-09-07 16:10:19 -0700129 .add("type", type)
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800130 .add("state", state)
Ray Milkey2693bda2016-01-22 16:08:14 -0800131 .add("expected", isExpected)
tom4c6606f2014-09-07 11:11:21 -0700132 .toString();
133 }
134
Ray Milkey2693bda2016-01-22 16:08:14 -0800135 /**
136 * Creates a new default link builder.
137 *
138 * @return default link builder
139 */
140 public static Builder builder() {
141 return new Builder();
142 }
143
144 /**
145 * Builder for DefaultLink objects.
146 */
Brian Stanke9a108972016-04-11 15:25:17 -0400147 public static class Builder {
Ray Milkey2693bda2016-01-22 16:08:14 -0800148 private ProviderId providerId;
149 private Annotations annotations = EMPTY;
150 private ConnectPoint src;
151 private ConnectPoint dst;
152 private Type type;
153 private State state = ACTIVE;
154 private boolean isExpected = false;
155
Brian Stanke9a108972016-04-11 15:25:17 -0400156 protected Builder() {
Ray Milkey2693bda2016-01-22 16:08:14 -0800157 // Hide constructor
158 }
159
160 /**
161 * Sets the providerId to be used by the builder.
162 *
163 * @param providerId new provider id
164 * @return self
165 */
166 public Builder providerId(ProviderId providerId) {
167 this.providerId = providerId;
168 return this;
169 }
170
171 /**
172 * Sets the annotations to be used by the builder.
173 *
174 * @param annotations new annotations
175 * @return self
176 */
177 public Builder annotations(Annotations annotations) {
178 this.annotations = annotations;
179 return this;
180 }
181
182 /**
183 * Sets the source connect point to be used by the builder.
184 *
185 * @param src source connect point
186 * @return self
187 */
188 public Builder src(ConnectPoint src) {
189 this.src = src;
190 return this;
191 }
192
193 /**
194 * Sets the destination connect point to be used by the builder.
195 *
196 * @param dst new destination connect point
197 * @return self
198 */
199 public Builder dst(ConnectPoint dst) {
200 this.dst = dst;
201 return this;
202 }
203
204 /**
205 * Sets the link type to be used by the builder.
206 *
207 * @param type new link type
208 * @return self
209 */
210 public Builder type(Type type) {
211 this.type = type;
212 return this;
213 }
214
215 /**
216 * Sets the link state to be used by the builder.
217 *
218 * @param state new link state
219 * @return self
220 */
221 public Builder state(State state) {
222 this.state = state;
223 return this;
224 }
225
226 /**
227 * Sets the expected flag to be used by the builder.
228 *
229 * @param isExpected new expected flag
230 * @return self
231 */
232 public Builder isExpected(boolean isExpected) {
233 this.isExpected = isExpected;
234 return this;
235 }
236
237 /**
238 * Builds a default link object from the accumulated parameters.
239 *
240 * @return default link object
241 */
242 public DefaultLink build() {
243 checkNotNull(src, "Source connect point cannot be null");
244 checkNotNull(dst, "Destination connect point cannot be null");
245 checkNotNull(type, "Type cannot be null");
246 checkNotNull(providerId, "Provider Id cannot be null");
247
248 return new DefaultLink(providerId, src, dst,
249 type, state,
250 isExpected, annotations);
251 }
252
253 }
254
255
256
tom4c6606f2014-09-07 11:11:21 -0700257}