blob: 628d785ffea96446868b50cec56557511b979b04 [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 /**
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
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) &&
Ray Milkeyb7f0f642016-01-22 16:08:14 -0800123 Objects.equals(this.type, other.type) &&
124 Objects.equals(this.isExpected, other.isExpected);
tom4c6606f2014-09-07 11:11:21 -0700125 }
126 return false;
127 }
128
129 @Override
130 public String toString() {
131 return toStringHelper(this)
132 .add("src", src)
133 .add("dst", dst)
tomeadbb462014-09-07 16:10:19 -0700134 .add("type", type)
Thomas Vachuska57126fe2014-11-11 17:13:24 -0800135 .add("state", state)
Ray Milkey2693bda2016-01-22 16:08:14 -0800136 .add("expected", isExpected)
tom4c6606f2014-09-07 11:11:21 -0700137 .toString();
138 }
139
Ray Milkey2693bda2016-01-22 16:08:14 -0800140 /**
141 * Creates a new default link builder.
142 *
143 * @return default link builder
144 */
145 public static Builder builder() {
146 return new Builder();
147 }
148
149 /**
150 * Builder for DefaultLink objects.
151 */
Brian Stanke9a108972016-04-11 15:25:17 -0400152 public static class Builder {
Ray Milkey2693bda2016-01-22 16:08:14 -0800153 private ProviderId providerId;
154 private Annotations annotations = EMPTY;
155 private ConnectPoint src;
156 private ConnectPoint dst;
157 private Type type;
158 private State state = ACTIVE;
159 private boolean isExpected = false;
160
Brian Stanke9a108972016-04-11 15:25:17 -0400161 protected Builder() {
Ray Milkey2693bda2016-01-22 16:08:14 -0800162 // Hide constructor
163 }
164
165 /**
166 * Sets the providerId to be used by the builder.
167 *
168 * @param providerId new provider id
169 * @return self
170 */
171 public Builder providerId(ProviderId providerId) {
172 this.providerId = providerId;
173 return this;
174 }
175
176 /**
177 * Sets the annotations to be used by the builder.
178 *
179 * @param annotations new annotations
180 * @return self
181 */
182 public Builder annotations(Annotations annotations) {
183 this.annotations = annotations;
184 return this;
185 }
186
187 /**
188 * Sets the source connect point to be used by the builder.
189 *
190 * @param src source connect point
191 * @return self
192 */
193 public Builder src(ConnectPoint src) {
194 this.src = src;
195 return this;
196 }
197
198 /**
199 * Sets the destination connect point to be used by the builder.
200 *
201 * @param dst new destination connect point
202 * @return self
203 */
204 public Builder dst(ConnectPoint dst) {
205 this.dst = dst;
206 return this;
207 }
208
209 /**
210 * Sets the link type to be used by the builder.
211 *
212 * @param type new link type
213 * @return self
214 */
215 public Builder type(Type type) {
216 this.type = type;
217 return this;
218 }
219
220 /**
221 * Sets the link state to be used by the builder.
222 *
223 * @param state new link state
224 * @return self
225 */
226 public Builder state(State state) {
227 this.state = state;
228 return this;
229 }
230
231 /**
232 * Sets the expected flag to be used by the builder.
233 *
234 * @param isExpected new expected flag
235 * @return self
236 */
237 public Builder isExpected(boolean isExpected) {
238 this.isExpected = isExpected;
239 return this;
240 }
241
242 /**
243 * Builds a default link object from the accumulated parameters.
244 *
245 * @return default link object
246 */
247 public DefaultLink build() {
248 checkNotNull(src, "Source connect point cannot be null");
249 checkNotNull(dst, "Destination connect point cannot be null");
250 checkNotNull(type, "Type cannot be null");
251 checkNotNull(providerId, "Provider Id cannot be null");
252
253 return new DefaultLink(providerId, src, dst,
254 type, state,
255 isExpected, annotations);
256 }
257
258 }
259
260
261
tom4c6606f2014-09-07 11:11:21 -0700262}