2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.linkstate;
10 import java.util.Arrays;
11 import java.util.Collections;
14 import org.opendaylight.protocol.concepts.Bandwidth;
15 import org.opendaylight.protocol.concepts.Metric;
16 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
17 import org.opendaylight.protocol.bgp.linkstate.AdministrativeGroup;
18 import org.opendaylight.protocol.bgp.linkstate.LinkProtectionType;
19 import org.opendaylight.protocol.bgp.linkstate.MPLSProtocol;
20 import org.opendaylight.protocol.util.DefaultingTypesafeContainer;
21 import com.google.common.base.Objects.ToStringHelper;
22 import com.google.common.base.Preconditions;
26 * A single link in network topology. Network link is a connecting line between two network nodes with bunch of
29 public final class NetworkLinkState extends NetworkObjectState {
30 public static final NetworkLinkState EMPTY = new NetworkLinkState();
31 private static final long serialVersionUID = 1L;
32 private final DefaultingTypesafeContainer<Metric<?>> metrics;
33 private Set<SharedRiskLinkGroup> sharedRiskLinkGroups;
34 private AdministrativeGroup administrativeGroup;
35 private Set<MPLSProtocol> enabledMPLSProtocols;
36 private LinkProtectionType protectionType;
37 private Bandwidth[] unreservedBandwidth;
38 private Bandwidth reservableBandwidth;
39 private Bandwidth maximumBandwidth;
40 private String symbolicName;
42 private NetworkLinkState() {
43 this(NetworkObjectState.EMPTY, new DefaultingTypesafeContainer<Metric<?>>(), null, LinkProtectionType.UNPROTECTED, null, null, null);
46 public NetworkLinkState(final NetworkObjectState orig, final DefaultingTypesafeContainer<Metric<?>> metrics,
47 final Set<SharedRiskLinkGroup> sharedRiskLinkGroups, final AdministrativeGroup administrativeGroup,
48 final Set<MPLSProtocol> enabledMPLSProtocols, final LinkProtectionType protectionType, final String symbolicName,
49 final Bandwidth[] unreservedBandwidth, final Bandwidth reservableBandwidth, final Bandwidth maximumBandwidth) {
51 this.metrics = Preconditions.checkNotNull(metrics, "Metric is mandatory.");
52 this.sharedRiskLinkGroups = sharedRiskLinkGroups;
53 this.administrativeGroup = administrativeGroup;
54 this.enabledMPLSProtocols = enabledMPLSProtocols;
55 this.protectionType = Preconditions.checkNotNull(protectionType);
56 this.symbolicName = symbolicName;
57 this.unreservedBandwidth = unreservedBandwidth;
58 this.reservableBandwidth = reservableBandwidth;
59 this.maximumBandwidth = maximumBandwidth;
62 public NetworkLinkState(final NetworkObjectState orig, final DefaultingTypesafeContainer<Metric<?>> metrics,
63 final AdministrativeGroup administrativeGroup, final LinkProtectionType protectionType, final Bandwidth[] unreservedBandwidth,
64 final Bandwidth reservableBandwidth, final Bandwidth maximumBandwidth) {
65 this(orig, metrics, Collections.<SharedRiskLinkGroup> emptySet(), administrativeGroup, Collections.<MPLSProtocol> emptySet(), protectionType,
66 null, unreservedBandwidth, reservableBandwidth, maximumBandwidth);
69 protected NetworkLinkState(final NetworkLinkState orig) {
71 this.metrics = orig.metrics;
72 this.sharedRiskLinkGroups = orig.sharedRiskLinkGroups;
73 this.administrativeGroup = orig.administrativeGroup;
74 this.enabledMPLSProtocols = orig.enabledMPLSProtocols;
75 this.protectionType = orig.protectionType;
76 this.symbolicName = orig.symbolicName;
77 this.unreservedBandwidth = orig.unreservedBandwidth;
78 this.reservableBandwidth = orig.reservableBandwidth;
79 this.maximumBandwidth = orig.maximumBandwidth;
83 protected NetworkLinkState newInstance() {
84 return new NetworkLinkState(this);
88 * Get AdministrativeGroup attribute of network link {@link AdministrativeGroup}.
90 * @return AdministrativeGroup of network link.
92 public final AdministrativeGroup getAdministrativeGroup() {
93 return this.administrativeGroup;
96 public final NetworkLinkState withAdministrativeGroup(final AdministrativeGroup administrativeGroup) {
97 final NetworkLinkState ret = newInstance();
98 ret.administrativeGroup = administrativeGroup;
103 * Get maximum bandwidth attribute of network link {@link Bandwidth}.
105 * @return Bandwidth maximum bandwidth of network link.
107 public final Bandwidth getMaximumBandwidth() {
108 return this.maximumBandwidth;
111 public final NetworkLinkState withMaximumBandwidth(final Bandwidth maximumBandwidth) {
112 final NetworkLinkState ret = newInstance();
113 ret.maximumBandwidth = maximumBandwidth;
118 * Get maximum reservable bandwidth attribute of network link {@link Bandwidth}.
120 * @return Bandwidth maximum reservable bandwidth of network link.
122 public final Bandwidth getMaximumReservableBandwidth() {
123 return this.reservableBandwidth;
126 public final NetworkLinkState withReservableBandwidth(final Bandwidth reservableBandwidth) {
127 final NetworkLinkState ret = newInstance();
128 ret.reservableBandwidth = reservableBandwidth;
133 * Get unreserved bandwidth attribute of network link {@link Bandwidth}.
135 * @return Bandwidth[] unreserved bandwidth of network link.
137 public final Bandwidth[] getUnreservedBandwidth() {
138 return this.unreservedBandwidth;
141 public final NetworkLinkState withUnreservedBandwidth(final Bandwidth[] unreservedBandwidth) {
142 Preconditions.checkNotNull(unreservedBandwidth);
143 Preconditions.checkArgument(unreservedBandwidth.length == 8);
145 final NetworkLinkState ret = newInstance();
146 ret.unreservedBandwidth = unreservedBandwidth;
151 * Get link protection type attribute of network link {@link LinkProtectionType}.
153 * @return LinkProtectionType of network link.
155 public final LinkProtectionType getProtectionType() {
156 return this.protectionType;
159 public final NetworkLinkState withProtectionType(final LinkProtectionType protectionType) {
160 final NetworkLinkState ret = newInstance();
161 ret.protectionType = protectionType;
166 * Get set of enabled MPLSProtocols of network link {@link MPLSProtocol}.
168 * @return Set<MPLSProtocol> enabled MPLSProtocols of network link.
170 public final Set<MPLSProtocol> getEnabledMPLSProtocols() {
171 return this.enabledMPLSProtocols;
174 public final NetworkLinkState withEnabledMPLSProtocols(final Set<MPLSProtocol> enabledMPLSProtocols) {
175 final NetworkLinkState ret = newInstance();
176 ret.enabledMPLSProtocols = enabledMPLSProtocols;
181 * Get set of SharedRiskLinkGroups of network link {@link SharedRiskLinkGroup}.
183 * @return Set<SharedRiskLinkGroup> shared risk link groups of network link.
185 public final Set<SharedRiskLinkGroup> getSharedRiskLinkGroups() {
186 return this.sharedRiskLinkGroups;
189 public final NetworkLinkState withSharedRiskLinkGroups(final Set<SharedRiskLinkGroup> sharedRiskLinkGroups) {
190 Preconditions.checkNotNull(sharedRiskLinkGroups);
191 final NetworkLinkState ret = newInstance();
192 ret.sharedRiskLinkGroups = sharedRiskLinkGroups;
197 * Get default Metric attribute of network link {@link Metric}.
199 * @return default Metric of network link.
201 public final Metric<?> getDefaultMetric() {
202 return this.metrics.getDefaultEntry();
205 public final <T extends Metric<?>> NetworkLinkState withDefaultMetric(final T metric) {
206 // FIXME: this violates API contract of State!
207 this.metrics.setDefaultEntry(metric);
212 * Get metric attribute of submitted metric type of network link {@link Metric}.
214 * @param <T> sub-type of metric.
215 * @param metricType which value should be returned.
216 * @return metric of submitted type of network link.
218 public final <T extends Metric<?>> T getMetric(final Class<? extends T> metricType) {
219 return this.metrics.getEntry(metricType);
222 public final <T extends Metric<T>> NetworkLinkState withMetric(final Class<T> metricType, final T metric) {
223 // FIXME: this violates API contract of State!
224 this.metrics.setEntry(metricType, metric);
228 public final String getSymbolicName() {
229 return this.symbolicName;
232 public final NetworkLinkState withSymbolicName(final String symbolicName) {
233 final NetworkLinkState ret = newInstance();
234 ret.symbolicName = symbolicName;
239 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
240 toStringHelper.add("metrics", this.metrics);
241 toStringHelper.add("SRLGs", this.sharedRiskLinkGroups);
242 toStringHelper.add("administrativeGroup", this.administrativeGroup);
243 toStringHelper.add("enabledMPLSProtocols", this.enabledMPLSProtocols);
244 toStringHelper.add("protectionType", this.protectionType);
245 toStringHelper.add("unreservedBandwidth", this.unreservedBandwidth);
246 toStringHelper.add("reservableBandwidth", this.reservableBandwidth);
247 toStringHelper.add("maximumBandwidth", this.maximumBandwidth);
248 toStringHelper.add("symbolicName", this.symbolicName);
249 return super.addToStringAttributes(toStringHelper);
253 * @see java.lang.Object#hashCode()
256 public int hashCode() {
257 final int prime = 31;
258 int result = super.hashCode();
259 result = prime * result + ((this.administrativeGroup == null) ? 0 : this.administrativeGroup.hashCode());
260 result = prime * result + ((this.enabledMPLSProtocols == null) ? 0 : this.enabledMPLSProtocols.hashCode());
261 result = prime * result + ((this.maximumBandwidth == null) ? 0 : this.maximumBandwidth.hashCode());
262 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
263 result = prime * result + ((this.protectionType == null) ? 0 : this.protectionType.hashCode());
264 result = prime * result + ((this.symbolicName == null) ? 0 : this.symbolicName.hashCode());
265 result = prime * result + ((this.reservableBandwidth == null) ? 0 : this.reservableBandwidth.hashCode());
266 result = prime * result + ((this.sharedRiskLinkGroups == null) ? 0 : this.sharedRiskLinkGroups.hashCode());
267 result = prime * result + Arrays.hashCode(this.unreservedBandwidth);
272 * @see java.lang.Object#equals(java.lang.Object)
275 public boolean equals(final Object obj) {
278 if (!super.equals(obj))
280 if (getClass() != obj.getClass())
282 final NetworkLinkState other = (NetworkLinkState) obj;
283 if (this.administrativeGroup == null) {
284 if (other.administrativeGroup != null)
286 } else if (!this.administrativeGroup.equals(other.administrativeGroup))
288 if (this.enabledMPLSProtocols == null) {
289 if (other.enabledMPLSProtocols != null)
291 } else if (!this.enabledMPLSProtocols.equals(other.enabledMPLSProtocols))
293 if (this.maximumBandwidth == null) {
294 if (other.maximumBandwidth != null)
296 } else if (!this.maximumBandwidth.equals(other.maximumBandwidth))
298 if (this.metrics == null) {
299 if (other.metrics != null)
301 } else if (!this.metrics.equals(other.metrics))
303 if (this.symbolicName == null) {
304 if (other.symbolicName != null)
306 } else if (!this.symbolicName.equals(other.symbolicName))
308 if (this.protectionType != other.protectionType)
310 if (this.reservableBandwidth == null) {
311 if (other.reservableBandwidth != null)
313 } else if (!this.reservableBandwidth.equals(other.reservableBandwidth))
315 if (this.sharedRiskLinkGroups == null) {
316 if (other.sharedRiskLinkGroups != null)
318 } else if (!this.sharedRiskLinkGroups.equals(other.sharedRiskLinkGroups))
320 if (!Arrays.equals(this.unreservedBandwidth, other.unreservedBandwidth))