Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeStateReportObject.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
16 /**
17  * Structure that combines set of related objects.
18  *
19  * @see <a
20  *      href="http://tools.ietf.org/html/draft-crabbe-pce-stateful-pce-02#section-6.1">The
21  *      PCRpt Message</a> - &lt;state-report&gt;</br>
22  */
23 public class CompositeStateReportObject {
24
25         private final PCEPLspObject lsp;
26
27         private List<CompositeRptPathObject> paths;
28
29         /**
30          * Constructs basic composite object only with mandatory objects.
31          *
32          * @param lsp
33          *            PCEPLspObject
34          */
35         public CompositeStateReportObject(PCEPLspObject lsp) {
36                 this(lsp, null);
37         }
38
39         /**
40          * Constructs composite object with optional objects.
41          *
42          * @param lsp
43          *            PCEPLspObject
44          * @param paths
45          *            List<CompositeRptPathObject> Can't be null.
46          */
47         public CompositeStateReportObject(PCEPLspObject lsp, List<CompositeRptPathObject> paths) {
48                 if (lsp == null)
49                         throw new IllegalArgumentException("LSP Object is mandatory.");
50                 this.lsp = lsp;
51                 if (paths != null)
52                         this.paths = paths;
53                 else
54                         this.paths = Collections.emptyList();
55         }
56
57         /**
58          * Creates this object from a list of PCEPObjects.
59          * @param objects List<PCEPObject> list of PCEPObjects from whose this
60          * object should be created.
61          * @return CompositeStateReportObject
62          */
63         public static CompositeStateReportObject getCompositeFromList(List<PCEPObject> objects) {
64                 if (objects == null || objects.isEmpty()) {
65                         throw new IllegalArgumentException("List can not be null or empty.");
66                 }
67
68                 PCEPLspObject lsp = null;
69                 if (objects.get(0) instanceof PCEPLspObject) {
70                         lsp = (PCEPLspObject) objects.get(0);
71                         objects.remove(lsp);
72                 } else
73                         return null;
74
75                 final List<CompositeRptPathObject> paths = new ArrayList<CompositeRptPathObject>();
76
77                 if (!objects.isEmpty()) {
78                         CompositeRptPathObject path = CompositeRptPathObject.getCompositeFromList(objects);
79                         while (path != null) {
80                                 paths.add(path);
81                                 if (objects.isEmpty())
82                                         break;
83                                 path = CompositeRptPathObject.getCompositeFromList(objects);
84                         }
85                 }
86
87                 return new CompositeStateReportObject(lsp, paths);
88         }
89
90         /**
91          * Gets list of all objects, which are in appropriate order.
92          *
93          * @return List<PCEPObject>
94          */
95         public List<PCEPObject> getCompositeAsList() {
96                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
97                 list.add(this.lsp);
98                 if (this.paths != null && !this.paths.isEmpty())
99                         for (final CompositeRptPathObject cpo : this.paths)
100                                 list.addAll(cpo.getCompositeAsList());
101                 return list;
102         }
103
104         /**
105          * Gets {@link PCEPLspObject}.
106          *
107          * @return PCEPLspObject. Can't be null.
108          */
109         public PCEPLspObject getLsp() {
110                 return this.lsp;
111         }
112
113         /**
114          * Gets list of {@link CompositeRptPathObject}.
115          *
116          * @return List<CompositeRptPathObject>. May be null.
117          */
118         public List<CompositeRptPathObject> getPaths() {
119                 return this.paths;
120         }
121
122         @Override
123         public int hashCode() {
124                 final int prime = 31;
125                 int result = 1;
126                 result = prime * result + ((this.lsp == null) ? 0 : this.lsp.hashCode());
127                 result = prime * result + ((this.paths == null) ? 0 : this.paths.hashCode());
128                 return result;
129         }
130
131         @Override
132         public boolean equals(Object obj) {
133                 if (this == obj)
134                         return true;
135                 if (obj == null)
136                         return false;
137                 if (this.getClass() != obj.getClass())
138                         return false;
139                 final CompositeStateReportObject other = (CompositeStateReportObject) obj;
140                 if (this.lsp == null) {
141                         if (other.lsp != null)
142                                 return false;
143                 } else if (!this.lsp.equals(other.lsp))
144                         return false;
145                 if (this.paths == null) {
146                         if (other.paths != null)
147                                 return false;
148                 } else if (!this.paths.equals(other.paths))
149                         return false;
150                 return true;
151         }
152
153         @Override
154         public String toString() {
155                 final StringBuilder builder = new StringBuilder();
156                 builder.append("CompositeStateReportObject [lsp=");
157                 builder.append(this.lsp);
158                 builder.append(", paths=");
159                 builder.append(this.paths);
160                 builder.append("]");
161                 return builder.toString();
162         }
163 }