Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.net.provider.ProviderId; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Objects; |
| 21 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 22 | import static com.google.common.base.MoreObjects.toStringHelper; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import static org.onosproject.net.Link.State.ACTIVE; |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.DefaultAnnotations.EMPTY; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Default infrastructure link model implementation. |
| 29 | */ |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 30 | public class DefaultLink extends AbstractProjectableModel implements Link { |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 31 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 32 | private final ConnectPoint src; |
| 33 | private final ConnectPoint dst; |
| 34 | private final Type type; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 35 | private final State state; |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 36 | private final boolean isExpected; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 39 | * Creates an infrastructure link using the supplied information. |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 40 | * |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 41 | * @param providerId provider identity |
| 42 | * @param src link source |
| 43 | * @param dst link destination |
| 44 | * @param type link type |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 45 | * @param state link state |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 46 | * @param annotations optional key/value annotations |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 47 | */ |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 48 | protected DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst, |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 49 | Type type, State state, Annotations... annotations) { |
| 50 | this(providerId, src, dst, type, state, false, annotations); |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 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 |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 63 | * @param isExpected indicates if the link is preconfigured |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 64 | * @param annotations optional key/value annotations |
| 65 | */ |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 66 | private DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst, |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 67 | Type type, State state, |
| 68 | boolean isExpected, Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 69 | super(providerId, annotations); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 70 | this.src = src; |
| 71 | this.dst = dst; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 72 | this.type = type; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 73 | this.state = state; |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 74 | this.isExpected = isExpected; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ConnectPoint src() { |
| 79 | return src; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public ConnectPoint dst() { |
| 84 | return dst; |
| 85 | } |
| 86 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public Type type() { |
| 89 | return type; |
| 90 | } |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 91 | |
| 92 | @Override |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 93 | public State state() { |
| 94 | return state; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean isDurable() { |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 99 | return isExpected(); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean isExpected() { |
| 104 | return isExpected; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Note: Durability & state are purposefully omitted form equality & hashCode. |
| 108 | |
| 109 | @Override |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 110 | public int hashCode() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 111 | return Objects.hash(src, dst, type); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 116 | if (this == obj) { |
| 117 | return true; |
| 118 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 119 | if (obj instanceof DefaultLink) { |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 120 | final DefaultLink other = (DefaultLink) obj; |
| 121 | return Objects.equals(this.src, other.src) && |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 122 | Objects.equals(this.dst, other.dst) && |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 123 | Objects.equals(this.type, other.type) && |
| 124 | Objects.equals(this.isExpected, other.isExpected); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String toString() { |
| 131 | return toStringHelper(this) |
| 132 | .add("src", src) |
| 133 | .add("dst", dst) |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 134 | .add("type", type) |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 135 | .add("state", state) |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 136 | .add("expected", isExpected) |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 137 | .toString(); |
| 138 | } |
| 139 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 140 | /** |
| 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 152 | public static class Builder { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 153 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 161 | protected Builder() { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 162 | // 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 | |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 262 | } |