2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.message;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPErrors;
18 import org.opendaylight.protocol.pcep.PCEPObject;
19 import org.opendaylight.protocol.pcep.impl.PCEPMessageValidator;
20 import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
21 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
22 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
23 import org.opendaylight.protocol.pcep.object.CompositeErrorObject;
24 import org.opendaylight.protocol.pcep.object.CompositePathObject;
25 import org.opendaylight.protocol.pcep.object.CompositeReplySvecObject;
26 import org.opendaylight.protocol.pcep.object.CompositeResponseObject;
27 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
28 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
29 import org.opendaylight.protocol.pcep.object.PCEPIncludeRouteObject;
30 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
31 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
32 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
33 import org.opendaylight.protocol.pcep.object.PCEPNoPathObject;
34 import org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject;
35 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
36 import org.opendaylight.protocol.pcep.object.PCEPRequestedPathBandwidthObject;
37 import org.opendaylight.protocol.pcep.object.PCEPSvecObject;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
40 import com.google.common.collect.Lists;
43 * PCEPReplyMessage validator. Validates message integrity.
45 public class PCEPReplyMessageValidator extends PCEPMessageValidator {
47 private static class SubReplyValidator {
49 private boolean requestRejected = true;
51 private List<Message> msgs = Lists.newArrayList();
53 private PCEPRequestParameterObject rpObj;
55 private PCEPNoPathObject noPath;
56 private PCEPLspObject lsp;
57 private PCEPLspaObject lspa;
58 private PCEPRequestedPathBandwidthObject bandwidth;
59 private List<PCEPMetricObject> metrics;
60 private PCEPIncludeRouteObject iro;
61 private List<CompositePathObject> paths;
64 this.requestRejected = false;
65 this.msgs = Lists.newArrayList();
72 this.metrics = new ArrayList<PCEPMetricObject>();
73 this.paths = new ArrayList<CompositePathObject>();
76 public List<Message> validate(final List<PCEPObject> objects, final List<CompositeReplySvecObject> svecList) {
79 if (!(objects.get(0) instanceof PCEPRequestParameterObject))
82 final PCEPRequestParameterObject rpObj = (PCEPRequestParameterObject) objects.get(0);
87 while (!objects.isEmpty()) {
89 if (obj instanceof UnknownObject) {
90 if (((UnknownObject) obj).isProcessed()) {
91 this.msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(((UnknownObject) obj).getError()))));
92 this.requestRejected = true;
102 if (obj instanceof PCEPNoPathObject) {
103 this.noPath = (PCEPNoPathObject) obj;
108 if (obj instanceof PCEPLspObject) {
109 this.lsp = (PCEPLspObject) obj;
114 if (obj instanceof PCEPLspaObject) {
115 this.lspa = (PCEPLspaObject) obj;
120 if (obj instanceof PCEPRequestedPathBandwidthObject) {
121 this.bandwidth = (PCEPRequestedPathBandwidthObject) obj;
126 if (obj instanceof PCEPMetricObject) {
127 this.metrics.add((PCEPMetricObject) obj);
133 if (obj instanceof PCEPIncludeRouteObject) {
134 this.iro = (PCEPIncludeRouteObject) obj;
149 if (!objects.isEmpty()) {
150 CompositePathObject path = this.getValidCompositePath(objects);
151 while (path != null) {
152 this.paths.add(path);
153 if (objects.isEmpty())
155 path = this.getValidCompositePath(objects);
159 if (!this.requestRejected) {
160 this.msgs.add(new PCEPReplyMessage(Collections.unmodifiableList(Arrays.asList(new CompositeResponseObject(rpObj, this.noPath, this.lsp, this.lspa, this.bandwidth, this.metrics, this.iro, this.paths))), Collections.unmodifiableList(svecList)));
166 private CompositePathObject getValidCompositePath(final List<PCEPObject> objects) {
167 if (!(objects.get(0) instanceof PCEPExplicitRouteObject))
170 final PCEPExplicitRouteObject explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
173 PCEPLspaObject pathLspa = null;
174 PCEPRequestedPathBandwidthObject pathBandwidth = null;
175 final List<PCEPMetricObject> pathMetrics = new ArrayList<PCEPMetricObject>();
176 PCEPIncludeRouteObject pathIro = null;
180 while (!objects.isEmpty()) {
181 obj = objects.get(0);
182 if (obj instanceof UnknownObject) {
183 if (((UnknownObject) obj).isProcessed()) {
184 this.msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(this.rpObj, false), new PCEPErrorObject(((UnknownObject) obj).getError()))));
185 this.requestRejected = true;
194 if (obj instanceof PCEPLspaObject) {
195 pathLspa = (PCEPLspaObject) obj;
200 if (obj instanceof PCEPRequestedPathBandwidthObject) {
201 pathBandwidth = (PCEPRequestedPathBandwidthObject) obj;
206 if (obj instanceof PCEPMetricObject) {
207 pathMetrics.add((PCEPMetricObject) obj);
213 if (obj instanceof PCEPIncludeRouteObject) {
214 pathIro = (PCEPIncludeRouteObject) obj;
230 return new CompositePathObject(explicitRoute, pathLspa, pathBandwidth, pathMetrics, pathIro);
235 public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
237 throw new IllegalArgumentException("Passed list can't be null.");
239 final List<Message> msgs = Lists.newArrayList();
240 final List<CompositeReplySvecObject> svecList = new ArrayList<CompositeReplySvecObject>();
242 CompositeReplySvecObject svecComp;
243 while (!objects.isEmpty()) {
245 if ((svecComp = this.getValidSvecComposite(objects)) == null)
247 } catch (final PCEPDocumentedException e) {
248 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
252 svecList.add(svecComp);
255 List<Message> subMessages;
256 final SubReplyValidator subValidator = new SubReplyValidator();
257 while (!objects.isEmpty()) {
258 subMessages = subValidator.validate(objects, svecList);
259 if (subMessages == null)
261 msgs.addAll(subMessages);
264 if (msgs.isEmpty()) {
265 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.RP_MISSING)));
269 if (!objects.isEmpty())
270 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
275 private CompositeReplySvecObject getValidSvecComposite(final List<PCEPObject> objects) throws PCEPDocumentedException {
277 throw new IllegalArgumentException("List cannot be null.");
279 if (!(objects.get(0) instanceof PCEPSvecObject))
282 final PCEPSvecObject svec = (PCEPSvecObject) objects.get(0);
285 PCEPObjectiveFunctionObject of = null;
286 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
290 while (!objects.isEmpty()) {
291 obj = objects.get(0);
293 if (obj instanceof UnknownObject)
294 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
299 if (obj instanceof PCEPObjectiveFunctionObject) {
300 of = (PCEPObjectiveFunctionObject) obj;
305 if (obj instanceof PCEPMetricObject) {
306 metrics.add((PCEPMetricObject) obj);
318 return new CompositeReplySvecObject(svec, of, metrics);
321 private static PCEPRequestParameterObject copyRP(final PCEPRequestParameterObject origRp, final boolean processed) {
322 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());