Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPRequestMessageValidator.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.impl.message;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
15 import org.opendaylight.protocol.pcep.PCEPErrors;
16 import org.opendaylight.protocol.pcep.PCEPObject;
17 import org.opendaylight.protocol.pcep.impl.PCEPMessageValidator;
18 import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
19 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
20 import org.opendaylight.protocol.pcep.message.PCEPRequestMessage;
21 import org.opendaylight.protocol.pcep.object.CompositeErrorObject;
22 import org.opendaylight.protocol.pcep.object.CompositeRequestObject;
23 import org.opendaylight.protocol.pcep.object.CompositeRequestSvecObject;
24 import org.opendaylight.protocol.pcep.object.PCEPClassTypeObject;
25 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
26 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
27 import org.opendaylight.protocol.pcep.object.PCEPExcludeRouteObject;
28 import org.opendaylight.protocol.pcep.object.PCEPExistingPathBandwidthObject;
29 import org.opendaylight.protocol.pcep.object.PCEPGlobalConstraintsObject;
30 import org.opendaylight.protocol.pcep.object.PCEPIncludeRouteObject;
31 import org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject;
32 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
33 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
34 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
35 import org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject;
36 import org.opendaylight.protocol.pcep.object.PCEPReportedRouteObject;
37 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
38 import org.opendaylight.protocol.pcep.object.PCEPRequestedPathBandwidthObject;
39 import org.opendaylight.protocol.pcep.object.PCEPSvecObject;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nps.concepts.rev130930.Bandwidth;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
42
43 import com.google.common.collect.Lists;
44
45 /**
46  * PCEPRequestMessage validator. Validates message integrity.
47  */
48 public class PCEPRequestMessageValidator extends PCEPMessageValidator {
49
50         @Override
51         public List<Message> validate(final List<PCEPObject> objects) {
52                 if (objects == null)
53                         throw new IllegalArgumentException("Passed list can't be null.");
54
55                 final List<Message> msgs = Lists.newArrayList();
56                 final List<CompositeRequestSvecObject> svecList = new ArrayList<CompositeRequestSvecObject>();
57
58                 CompositeRequestSvecObject svecComp;
59                 while (!objects.isEmpty()) {
60                         try {
61                                 if ((svecComp = getValidSvecComposite(objects)) == null)
62                                         break;
63                         } catch (final PCEPDocumentedException e) {
64                                 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
65                                 return msgs;
66                         }
67
68                         svecList.add(svecComp);
69                 }
70
71                 while (!objects.isEmpty()) {
72                         final List<CompositeRequestObject> requests = new ArrayList<CompositeRequestObject>();
73                         PCEPRequestParameterObject rpObj = null;
74                         boolean requestRejected = false;
75
76                         if (objects.get(0) instanceof PCEPRequestParameterObject) {
77                                 rpObj = (PCEPRequestParameterObject) objects.get(0);
78                                 objects.remove(rpObj);
79                                 if (!rpObj.isProcessed()) {
80                                         msgs.add(new PCEPErrorMessage(new CompositeErrorObject(rpObj, new PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
81                                         requestRejected = true;
82                                 }
83
84                         } else {
85                                 // if RP obj is missing return error only;
86                                 msgs.clear();
87                                 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.RP_MISSING)));
88                                 return msgs;
89                         }
90
91                         PCEPEndPointsObject<?> endPoints = null;
92                         if (objects.get(0) instanceof PCEPEndPointsObject<?>) {
93                                 endPoints = (PCEPEndPointsObject<?>) objects.get(0);
94                                 objects.remove(0);
95                                 if (!endPoints.isProcessed()) {
96                                         msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
97                                         requestRejected = true;
98                                 }
99                         } else {
100                                 msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(PCEPErrors.END_POINTS_MISSING))));
101                                 requestRejected = true;
102                         }
103
104                         // ignore all continual end-points objects
105                         while (!objects.isEmpty() && objects.get(0) instanceof PCEPEndPointsObject<?>) {
106                                 objects.remove(0);
107                         }
108
109                         PCEPClassTypeObject classType = null;
110                         PCEPLspObject lsp = null;
111                         PCEPLspaObject lspa = null;
112                         PCEPRequestedPathBandwidthObject bandwidth = null;
113                         final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
114                         PCEPReportedRouteObject rro = null;
115                         PCEPExistingPathBandwidthObject rroBandwidth = null;
116                         PCEPIncludeRouteObject iro = null;
117                         PCEPLoadBalancingObject loadBalancing = null;
118
119                         int state = 1;
120                         while (!objects.isEmpty()) {
121                                 final PCEPObject obj = objects.get(0);
122                                 if (obj instanceof UnknownObject) {
123                                         if (((UnknownObject) obj).isProcessed()) {
124                                                 msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(((UnknownObject) obj).getError()))));
125                                                 requestRejected = true;
126                                         }
127
128                                         objects.remove(0);
129                                         continue;
130                                 }
131                                 switch (state) {
132                                 case 1:
133                                         state = 2;
134                                         if (obj instanceof PCEPClassTypeObject) {
135                                                 classType = (PCEPClassTypeObject) obj;
136                                                 if (!classType.isProcessed()) {
137                                                         msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(PCEPErrors.P_FLAG_NOT_SET))));
138                                                         requestRejected = true;
139                                                 }
140                                                 break;
141                                         }
142                                 case 2:
143                                         state = 3;
144                                         if (obj instanceof PCEPLspObject) {
145                                                 lsp = (PCEPLspObject) obj;
146                                                 break;
147                                         }
148                                 case 3:
149                                         state = 4;
150                                         if (obj instanceof PCEPLspaObject) {
151                                                 lspa = (PCEPLspaObject) obj;
152                                                 break;
153                                         }
154                                 case 4:
155                                         state = 5;
156                                         if (obj instanceof PCEPRequestedPathBandwidthObject) {
157                                                 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
158                                                 break;
159                                         }
160                                 case 5:
161                                         state = 6;
162                                         if (obj instanceof PCEPMetricObject) {
163                                                 metrics.add((PCEPMetricObject) obj);
164                                                 state = 5;
165
166                                                 break;
167                                         }
168                                 case 6:
169                                         state = 8;
170                                         if (obj instanceof PCEPReportedRouteObject) {
171                                                 rro = (PCEPReportedRouteObject) obj;
172                                                 state = 7;
173                                                 break;
174                                         }
175                                 case 7:
176                                         state = 8;
177                                         if (obj instanceof PCEPExistingPathBandwidthObject) {
178                                                 rroBandwidth = (PCEPExistingPathBandwidthObject) obj;
179                                                 break;
180                                         }
181                                 case 8:
182                                         state = 9;
183                                         if (obj instanceof PCEPIncludeRouteObject) {
184                                                 iro = (PCEPIncludeRouteObject) obj;
185                                                 break;
186                                         }
187                                 case 9:
188                                         if (obj instanceof PCEPLoadBalancingObject) {
189                                                 loadBalancing = (PCEPLoadBalancingObject) obj;
190                                                 break;
191                                         }
192                                         state = 10;
193                                 }
194
195                                 if (state == 10) {
196                                         break;
197                                 }
198
199                                 objects.remove(obj);
200                         }
201
202                         if (rpObj.isReoptimized() && bandwidth != null && bandwidth.getBandwidth() != new Bandwidth(new byte[] { 0 }) && rro == null) {
203                                 msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(PCEPErrors.RRO_MISSING))));
204                                 requestRejected = true;
205                         }
206
207                         if (!requestRejected) {
208                                 requests.add(new CompositeRequestObject(rpObj, endPoints, classType, lsp, lspa, bandwidth, metrics, rro, rroBandwidth, iro, loadBalancing));
209                                 msgs.add(new PCEPRequestMessage(Collections.unmodifiableList(svecList), Collections.unmodifiableList(requests)));
210                         }
211                 }
212
213                 return msgs;
214         }
215
216         private static CompositeRequestSvecObject getValidSvecComposite(final List<PCEPObject> objects) throws PCEPDocumentedException {
217                 if (objects == null || objects.isEmpty()) {
218                         throw new IllegalArgumentException("List cannot be null or empty.");
219                 }
220
221                 PCEPSvecObject svec = null;
222                 if (objects.get(0) instanceof PCEPSvecObject) {
223                         svec = (PCEPSvecObject) objects.get(0);
224                         objects.remove(svec);
225                 } else
226                         return null;
227
228                 PCEPObjectiveFunctionObject of = null;
229                 PCEPGlobalConstraintsObject gc = null;
230                 PCEPExcludeRouteObject xro = null;
231                 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
232
233                 int state = 1;
234                 while (!objects.isEmpty()) {
235                         final PCEPObject obj = objects.get(0);
236
237                         if (obj instanceof UnknownObject && ((UnknownObject) obj).isProcessed()) {
238                                 throw new PCEPDocumentedException("Unknown object in SVEC list.", ((UnknownObject) obj).getError());
239                         }
240
241                         switch (state) {
242                         case 1:
243                                 state = 2;
244                                 if (obj instanceof PCEPObjectiveFunctionObject) {
245                                         of = (PCEPObjectiveFunctionObject) obj;
246                                         break;
247                                 }
248                         case 2:
249                                 state = 3;
250                                 if (obj instanceof PCEPGlobalConstraintsObject) {
251                                         gc = (PCEPGlobalConstraintsObject) obj;
252                                         break;
253                                 }
254                         case 3:
255                                 state = 4;
256                                 if (obj instanceof PCEPExcludeRouteObject) {
257                                         xro = (PCEPExcludeRouteObject) obj;
258                                         break;
259                                 }
260                         case 4:
261                                 state = 5;
262                                 if (obj instanceof PCEPMetricObject) {
263                                         metrics.add((PCEPMetricObject) obj);
264                                         state = 4;
265
266                                         break;
267                                 }
268                         }
269
270                         if (state == 5)
271                                 break;
272
273                         objects.remove(obj);
274                 }
275
276                 return new CompositeRequestSvecObject(svec, of, gc, xro, metrics);
277         }
278
279         private static PCEPRequestParameterObject copyRP(final PCEPRequestParameterObject origRp, final boolean processed) {
280                 return new PCEPRequestParameterObject(origRp.isLoose(), origRp.isBidirectional(), origRp.isReoptimized(), origRp.isMakeBeforeBreak(), origRp.isReportRequestOrder(), origRp.isSuplyOFOnResponse(), origRp.isFragmentation(), origRp.isP2mp(), origRp.isEroCompression(), origRp.getPriority(), origRp.getRequestID(), origRp.getTlvs(), processed, origRp.isIgnored());
281         }
282 }