Merge "Randomize port to allow concurrent execution"
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / object / CompositeUpdateRequestObject.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.2">The
21  *      PCRpt Message</a> - &lt;update-request&gt;</br>
22  */
23 public class CompositeUpdateRequestObject {
24
25         private final PCEPLspObject lsp;
26
27         private List<CompositeUpdPathObject> paths;
28
29         /**
30          * Constructs basic composite object only with mandatory objects.
31          *
32          * @param lsp
33          *            PCEPLspObject. Can't be null.
34          */
35         public CompositeUpdateRequestObject(PCEPLspObject lsp) {
36                 this(lsp, null);
37         }
38
39         /**
40          * Constructs composite object with optional objects.
41          *
42          * @param lsp
43          *            PCEPLspObject. Can't be null.
44          * @param paths
45          *            List<CompositeUpdPathObject>
46          */
47         public CompositeUpdateRequestObject(PCEPLspObject lsp, List<CompositeUpdPathObject> 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          * Gets list of all objects, which are in appropriate order.
59          *
60          * @return List<PCEPObject>. Can't be null.
61          */
62         public List<PCEPObject> getCompositeAsList() {
63                 final List<PCEPObject> list = new ArrayList<PCEPObject>();
64                 list.add(this.lsp);
65                 if (this.paths != null && !this.paths.isEmpty())
66                         for (final CompositeUpdPathObject cpo : this.paths)
67                                 list.addAll(cpo.getCompositeAsList());
68                 return list;
69         }
70
71         /**
72          * Creates this object from a list of PCEPObjects.
73          * @param objects List<PCEPObject> list of PCEPObjects from whose this
74          * object should be created.
75          * @return CompositeUpdateRequestObject
76          */
77         public static CompositeUpdateRequestObject getCompositeFromList(List<PCEPObject> objects) {
78                 if (objects == null || objects.isEmpty()) {
79                         throw new IllegalArgumentException("List cannot be null or empty.");
80                 }
81
82                 PCEPLspObject lsp = null;
83                 if (objects.get(0) instanceof PCEPLspObject) {
84                         lsp = (PCEPLspObject) objects.get(0);
85                         objects.remove(lsp);
86                 } else
87                         return null;
88
89                 final List<CompositeUpdPathObject> paths = new ArrayList<CompositeUpdPathObject>();
90
91                 if (!objects.isEmpty()) {
92                         CompositeUpdPathObject path = CompositeUpdPathObject.getCompositeFromList(objects);
93                         while (path != null) {
94                                 paths.add(path);
95                                 if (objects.isEmpty())
96                                         break;
97                                 path = CompositeUpdPathObject.getCompositeFromList(objects);
98                         }
99                 }
100
101                 return new CompositeUpdateRequestObject(lsp, paths);
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 CompositeUpdPathObject}.
115          *
116          * @return List<CompositeUpdPathObject>. Can't be null, but may be empty.
117          */
118         public List<CompositeUpdPathObject> 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 CompositeUpdateRequestObject other = (CompositeUpdateRequestObject) 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("CompositeUpdateRequestObject [lsp=");
157                 builder.append(this.lsp);
158                 builder.append(", paths=");
159                 builder.append(this.paths);
160                 builder.append("]");
161                 return builder.toString();
162         }
163 }