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