Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeReplySvecObject.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.pcep.object;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPObject;
15 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
16
17 /**
18  * Composite SvecObject used in {@link PCEPReplyMessage}
19  */
20 public class CompositeReplySvecObject {
21
22         private final PCEPSvecObject svec;
23         private final PCEPObjectiveFunctionObject objectiveFunction;
24         private final List<PCEPMetricObject> metrics;
25
26         /**
27          * Constructs basic composite object only with mandatory objects.
28          * 
29          * @param svec
30          *            PCEPSvecObject
31          */
32         public CompositeReplySvecObject(PCEPSvecObject svec) {
33                 this(svec, null, null);
34         }
35
36         /**
37          * Constructs composite object also with optional objects.
38          * 
39          * @param svec
40          *            PCEPSvecObject
41          * @param objectiveFunction
42          *            PCEPObjectiveFunctionObject
43          * @param metrics
44          *            list of PCEPMetricObject
45          */
46         public CompositeReplySvecObject(PCEPSvecObject svec, PCEPObjectiveFunctionObject objectiveFunction, List<PCEPMetricObject> metrics) {
47                 if (svec == null)
48                         throw new IllegalArgumentException("Svec object is mandatory.");
49                 this.svec = svec;
50                 this.objectiveFunction = objectiveFunction;
51                 if (metrics != null)
52                         this.metrics = metrics;
53                 else
54                         this.metrics = Collections.emptyList();
55         }
56
57         /**
58          * Gets list of all objects, which are in appropriate order.
59          * 
60          * @return List<PCEPObject>. Can't be null or empty.
61          */
62         public List<PCEPObject> getCompositeAsList() {
63                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
64                 list.add(this.svec);
65                 if (this.objectiveFunction != null)
66                         list.add(this.objectiveFunction);
67                 if (this.metrics != null && !this.metrics.isEmpty())
68                         list.addAll(this.metrics);
69                 return list;
70         }
71
72         /**
73          * Creates this object from a list of PCEPObjects.
74          * 
75          * @param objects
76          *            List<PCEPObject> list of PCEPObjects from whose this object
77          *            should be created.
78          * @return CompositePathObject
79          */
80         public static CompositeReplySvecObject getCompositeFromList(List<PCEPObject> objects) {
81                 if (objects == null || objects.isEmpty()) {
82                         throw new IllegalArgumentException("List cannot be null or empty.");
83                 }
84
85                 PCEPSvecObject svec = null;
86                 if (objects.get(0) instanceof PCEPSvecObject) {
87                         svec = (PCEPSvecObject) objects.get(0);
88                         objects.remove(svec);
89                 } else
90                         return null;
91
92                 PCEPObjectiveFunctionObject of = null;
93                 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
94
95                 int state = 1;
96                 while (!objects.isEmpty()) {
97                         final PCEPObject obj = objects.get(0);
98
99                         switch (state) {
100                                 case 1:
101                                         state = 2;
102                                         if (obj instanceof PCEPObjectiveFunctionObject) {
103                                                 of = (PCEPObjectiveFunctionObject) obj;
104                                                 break;
105                                         }
106                                 case 2:
107                                         state = 3;
108                                         if (obj instanceof PCEPMetricObject) {
109                                                 metrics.add((PCEPMetricObject) obj);
110                                                 state = 2;
111
112                                                 break;
113                                         }
114                         }
115
116                         if (state == 3)
117                                 break;
118
119                         objects.remove(obj);
120                 }
121
122                 return new CompositeReplySvecObject(svec, of, metrics);
123         }
124
125         /**
126          * Gets {@link PCEPSvecObject}
127          * 
128          * @return PCEPSvecObject. Can't be null.
129          */
130         public PCEPSvecObject getSvec() {
131                 return this.svec;
132         }
133
134         /**
135          * Gets {@link PCEPObjectiveFunctionObject}
136          * 
137          * @return PCEPObjectiveFunctionObject. May be null.
138          */
139         public PCEPObjectiveFunctionObject getObjectiveFunction() {
140                 return this.objectiveFunction;
141         }
142
143         /**
144          * @return the metrics
145          */
146         public List<PCEPMetricObject> getMetrics() {
147                 return this.metrics;
148         }
149
150         @Override
151         public String toString() {
152                 final StringBuilder builder = new StringBuilder();
153                 builder.append("CompositeReplySvecObject [svec=");
154                 builder.append(this.svec);
155                 builder.append(", objectiveFunction=");
156                 builder.append(this.objectiveFunction);
157                 builder.append(", metrics=");
158                 builder.append(this.metrics);
159                 builder.append("]");
160                 return builder.toString();
161         }
162
163         @Override
164         public int hashCode() {
165                 final int prime = 31;
166                 int result = 1;
167                 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
168                 result = prime * result + ((this.objectiveFunction == null) ? 0 : this.objectiveFunction.hashCode());
169                 result = prime * result + ((this.svec == null) ? 0 : this.svec.hashCode());
170                 return result;
171         }
172
173         @Override
174         public boolean equals(Object obj) {
175                 if (this == obj)
176                         return true;
177                 if (obj == null)
178                         return false;
179                 if (this.getClass() != obj.getClass())
180                         return false;
181                 final CompositeReplySvecObject other = (CompositeReplySvecObject) obj;
182                 if (this.metrics == null) {
183                         if (other.metrics != null)
184                                 return false;
185                 } else if (!this.metrics.equals(other.metrics))
186                         return false;
187                 if (this.objectiveFunction == null) {
188                         if (other.objectiveFunction != null)
189                                 return false;
190                 } else if (!this.objectiveFunction.equals(other.objectiveFunction))
191                         return false;
192                 if (this.svec == null) {
193                         if (other.svec != null)
194                                 return false;
195                 } else if (!this.svec.equals(other.svec))
196                         return false;
197                 return true;
198         }
199
200 }