Initial framework migration to netty.
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / NetworkLinkState.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.linkstate;
9
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.Set;
13
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;
23
24 /**
25  * 
26  * A single link in network topology. Network link is a connecting line between two network nodes with bunch of
27  * attributes.
28  */
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;
41
42         private NetworkLinkState() {
43                 this(NetworkObjectState.EMPTY, new DefaultingTypesafeContainer<Metric<?>>(), null, LinkProtectionType.UNPROTECTED, null, null, null);
44         }
45
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) {
50                 super(orig);
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;
60         }
61
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);
67         }
68
69         protected NetworkLinkState(final NetworkLinkState orig) {
70                 super(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;
80         }
81
82         @Override
83         protected NetworkLinkState newInstance() {
84                 return new NetworkLinkState(this);
85         }
86
87         /**
88          * Get AdministrativeGroup attribute of network link {@link AdministrativeGroup}.
89          * 
90          * @return AdministrativeGroup of network link.
91          */
92         public final AdministrativeGroup getAdministrativeGroup() {
93                 return this.administrativeGroup;
94         }
95
96         public final NetworkLinkState withAdministrativeGroup(final AdministrativeGroup administrativeGroup) {
97                 final NetworkLinkState ret = newInstance();
98                 ret.administrativeGroup = administrativeGroup;
99                 return ret;
100         }
101
102         /**
103          * Get maximum bandwidth attribute of network link {@link Bandwidth}.
104          * 
105          * @return Bandwidth maximum bandwidth of network link.
106          */
107         public final Bandwidth getMaximumBandwidth() {
108                 return this.maximumBandwidth;
109         }
110
111         public final NetworkLinkState withMaximumBandwidth(final Bandwidth maximumBandwidth) {
112                 final NetworkLinkState ret = newInstance();
113                 ret.maximumBandwidth = maximumBandwidth;
114                 return ret;
115         }
116
117         /**
118          * Get maximum reservable bandwidth attribute of network link {@link Bandwidth}.
119          * 
120          * @return Bandwidth maximum reservable bandwidth of network link.
121          */
122         public final Bandwidth getMaximumReservableBandwidth() {
123                 return this.reservableBandwidth;
124         }
125
126         public final NetworkLinkState withReservableBandwidth(final Bandwidth reservableBandwidth) {
127                 final NetworkLinkState ret = newInstance();
128                 ret.reservableBandwidth = reservableBandwidth;
129                 return ret;
130         }
131
132         /**
133          * Get unreserved bandwidth attribute of network link {@link Bandwidth}.
134          * 
135          * @return Bandwidth[] unreserved bandwidth of network link.
136          */
137         public final Bandwidth[] getUnreservedBandwidth() {
138                 return this.unreservedBandwidth;
139         }
140
141         public final NetworkLinkState withUnreservedBandwidth(final Bandwidth[] unreservedBandwidth) {
142                 Preconditions.checkNotNull(unreservedBandwidth);
143                 Preconditions.checkArgument(unreservedBandwidth.length == 8);
144
145                 final NetworkLinkState ret = newInstance();
146                 ret.unreservedBandwidth = unreservedBandwidth;
147                 return ret;
148         }
149
150         /**
151          * Get link protection type attribute of network link {@link LinkProtectionType}.
152          * 
153          * @return LinkProtectionType of network link.
154          */
155         public final LinkProtectionType getProtectionType() {
156                 return this.protectionType;
157         }
158
159         public final NetworkLinkState withProtectionType(final LinkProtectionType protectionType) {
160                 final NetworkLinkState ret = newInstance();
161                 ret.protectionType = protectionType;
162                 return ret;
163         }
164
165         /**
166          * Get set of enabled MPLSProtocols of network link {@link MPLSProtocol}.
167          * 
168          * @return Set<MPLSProtocol> enabled MPLSProtocols of network link.
169          */
170         public final Set<MPLSProtocol> getEnabledMPLSProtocols() {
171                 return this.enabledMPLSProtocols;
172         }
173
174         public final NetworkLinkState withEnabledMPLSProtocols(final Set<MPLSProtocol> enabledMPLSProtocols) {
175                 final NetworkLinkState ret = newInstance();
176                 ret.enabledMPLSProtocols = enabledMPLSProtocols;
177                 return ret;
178         }
179
180         /**
181          * Get set of SharedRiskLinkGroups of network link {@link SharedRiskLinkGroup}.
182          * 
183          * @return Set<SharedRiskLinkGroup> shared risk link groups of network link.
184          */
185         public final Set<SharedRiskLinkGroup> getSharedRiskLinkGroups() {
186                 return this.sharedRiskLinkGroups;
187         }
188
189         public final NetworkLinkState withSharedRiskLinkGroups(final Set<SharedRiskLinkGroup> sharedRiskLinkGroups) {
190                 Preconditions.checkNotNull(sharedRiskLinkGroups);
191                 final NetworkLinkState ret = newInstance();
192                 ret.sharedRiskLinkGroups = sharedRiskLinkGroups;
193                 return ret;
194         }
195
196         /**
197          * Get default Metric attribute of network link {@link Metric}.
198          * 
199          * @return default Metric of network link.
200          */
201         public final Metric<?> getDefaultMetric() {
202                 return this.metrics.getDefaultEntry();
203         }
204
205         public final <T extends Metric<?>> NetworkLinkState withDefaultMetric(final T metric) {
206                 // FIXME: this violates API contract of State!
207                 this.metrics.setDefaultEntry(metric);
208                 return this;
209         }
210
211         /**
212          * Get metric attribute of submitted metric type of network link {@link Metric}.
213          * 
214          * @param <T> sub-type of metric.
215          * @param metricType which value should be returned.
216          * @return metric of submitted type of network link.
217          */
218         public final <T extends Metric<?>> T getMetric(final Class<? extends T> metricType) {
219                 return this.metrics.getEntry(metricType);
220         }
221
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);
225                 return this;
226         }
227
228         public final String getSymbolicName() {
229                 return this.symbolicName;
230         }
231
232         public final NetworkLinkState withSymbolicName(final String symbolicName) {
233                 final NetworkLinkState ret = newInstance();
234                 ret.symbolicName = symbolicName;
235                 return ret;
236         }
237
238         @Override
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);
250         }
251
252         /* (non-Javadoc)
253          * @see java.lang.Object#hashCode()
254          */
255         @Override
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);
268                 return result;
269         }
270
271         /* (non-Javadoc)
272          * @see java.lang.Object#equals(java.lang.Object)
273          */
274         @Override
275         public boolean equals(final Object obj) {
276                 if (this == obj)
277                         return true;
278                 if (!super.equals(obj))
279                         return false;
280                 if (getClass() != obj.getClass())
281                         return false;
282                 final NetworkLinkState other = (NetworkLinkState) obj;
283                 if (this.administrativeGroup == null) {
284                         if (other.administrativeGroup != null)
285                                 return false;
286                 } else if (!this.administrativeGroup.equals(other.administrativeGroup))
287                         return false;
288                 if (this.enabledMPLSProtocols == null) {
289                         if (other.enabledMPLSProtocols != null)
290                                 return false;
291                 } else if (!this.enabledMPLSProtocols.equals(other.enabledMPLSProtocols))
292                         return false;
293                 if (this.maximumBandwidth == null) {
294                         if (other.maximumBandwidth != null)
295                                 return false;
296                 } else if (!this.maximumBandwidth.equals(other.maximumBandwidth))
297                         return false;
298                 if (this.metrics == null) {
299                         if (other.metrics != null)
300                                 return false;
301                 } else if (!this.metrics.equals(other.metrics))
302                         return false;
303                 if (this.symbolicName == null) {
304                         if (other.symbolicName != null)
305                                 return false;
306                 } else if (!this.symbolicName.equals(other.symbolicName))
307                         return false;
308                 if (this.protectionType != other.protectionType)
309                         return false;
310                 if (this.reservableBandwidth == null) {
311                         if (other.reservableBandwidth != null)
312                                 return false;
313                 } else if (!this.reservableBandwidth.equals(other.reservableBandwidth))
314                         return false;
315                 if (this.sharedRiskLinkGroups == null) {
316                         if (other.sharedRiskLinkGroups != null)
317                                 return false;
318                 } else if (!this.sharedRiskLinkGroups.equals(other.sharedRiskLinkGroups))
319                         return false;
320                 if (!Arrays.equals(this.unreservedBandwidth, other.unreservedBandwidth))
321                         return false;
322                 return true;
323         }
324 }