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 | /** |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 39 | * Creates an active 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 |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 45 | * @param annotations optional key/value annotations |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 46 | */ |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 47 | protected DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 48 | Type type, Annotations... annotations) { |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 49 | 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 Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 62 | * @param isExpected indicates if the link is preconfigured |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 63 | * @param annotations optional key/value annotations |
| 64 | */ |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 65 | private DefaultLink(ProviderId providerId, ConnectPoint src, ConnectPoint dst, |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 66 | Type type, State state, |
| 67 | boolean isExpected, Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 68 | super(providerId, annotations); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 69 | this.src = src; |
| 70 | this.dst = dst; |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 71 | this.type = type; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 72 | this.state = state; |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 73 | this.isExpected = isExpected; |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public ConnectPoint src() { |
| 78 | return src; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public ConnectPoint dst() { |
| 83 | return dst; |
| 84 | } |
| 85 | |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public Type type() { |
| 88 | return type; |
| 89 | } |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 90 | |
| 91 | @Override |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 92 | public State state() { |
| 93 | return state; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean isDurable() { |
Ray Milkey | d0dd135 | 2016-01-19 10:58:41 -0800 | [diff] [blame] | 98 | return isExpected(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean isExpected() { |
| 103 | return isExpected; |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Note: Durability & state are purposefully omitted form equality & hashCode. |
| 107 | |
| 108 | @Override |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 109 | public int hashCode() { |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 110 | return Objects.hash(src, dst, type); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 115 | if (this == obj) { |
| 116 | return true; |
| 117 | } |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 118 | if (obj instanceof DefaultLink) { |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 119 | final DefaultLink other = (DefaultLink) obj; |
| 120 | return Objects.equals(this.src, other.src) && |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 121 | Objects.equals(this.dst, other.dst) && |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 122 | Objects.equals(this.type, other.type) && |
| 123 | Objects.equals(this.isExpected, other.isExpected); |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String toString() { |
| 130 | return toStringHelper(this) |
| 131 | .add("src", src) |
| 132 | .add("dst", dst) |
tom | eadbb46 | 2014-09-07 16:10:19 -0700 | [diff] [blame] | 133 | .add("type", type) |
Thomas Vachuska | 57126fe | 2014-11-11 17:13:24 -0800 | [diff] [blame] | 134 | .add("state", state) |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 135 | .add("expected", isExpected) |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 136 | .toString(); |
| 137 | } |
| 138 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 139 | /** |
| 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 | */ |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 151 | public static class Builder { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 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 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 160 | protected Builder() { |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 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 | |
tom | 4c6606f | 2014-09-07 11:11:21 -0700 | [diff] [blame] | 261 | } |