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.Collections;
12 import java.util.List;
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;
43 import com.google.common.collect.Lists;
46 * PCEPRequestMessage validator. Validates message integrity.
48 public class PCEPRequestMessageValidator extends PCEPMessageValidator {
51 public List<Message> validate(final List<PCEPObject> objects) {
53 throw new IllegalArgumentException("Passed list can't be null.");
55 final List<Message> msgs = Lists.newArrayList();
56 final List<CompositeRequestSvecObject> svecList = new ArrayList<CompositeRequestSvecObject>();
58 CompositeRequestSvecObject svecComp;
59 while (!objects.isEmpty()) {
61 if ((svecComp = getValidSvecComposite(objects)) == null)
63 } catch (final PCEPDocumentedException e) {
64 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
68 svecList.add(svecComp);
71 while (!objects.isEmpty()) {
72 final List<CompositeRequestObject> requests = new ArrayList<CompositeRequestObject>();
73 PCEPRequestParameterObject rpObj = null;
74 boolean requestRejected = false;
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;
85 // if RP obj is missing return error only;
87 msgs.add(new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.RP_MISSING)));
91 PCEPEndPointsObject<?> endPoints = null;
92 if (objects.get(0) instanceof PCEPEndPointsObject<?>) {
93 endPoints = (PCEPEndPointsObject<?>) objects.get(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;
100 msgs.add(new PCEPErrorMessage(new CompositeErrorObject(copyRP(rpObj, false), new PCEPErrorObject(PCEPErrors.END_POINTS_MISSING))));
101 requestRejected = true;
104 // ignore all continual end-points objects
105 while (!objects.isEmpty() && objects.get(0) instanceof PCEPEndPointsObject<?>) {
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;
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;
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;
144 if (obj instanceof PCEPLspObject) {
145 lsp = (PCEPLspObject) obj;
150 if (obj instanceof PCEPLspaObject) {
151 lspa = (PCEPLspaObject) obj;
156 if (obj instanceof PCEPRequestedPathBandwidthObject) {
157 bandwidth = (PCEPRequestedPathBandwidthObject) obj;
162 if (obj instanceof PCEPMetricObject) {
163 metrics.add((PCEPMetricObject) obj);
170 if (obj instanceof PCEPReportedRouteObject) {
171 rro = (PCEPReportedRouteObject) obj;
177 if (obj instanceof PCEPExistingPathBandwidthObject) {
178 rroBandwidth = (PCEPExistingPathBandwidthObject) obj;
183 if (obj instanceof PCEPIncludeRouteObject) {
184 iro = (PCEPIncludeRouteObject) obj;
188 if (obj instanceof PCEPLoadBalancingObject) {
189 loadBalancing = (PCEPLoadBalancingObject) obj;
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;
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)));
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.");
221 PCEPSvecObject svec = null;
222 if (objects.get(0) instanceof PCEPSvecObject) {
223 svec = (PCEPSvecObject) objects.get(0);
224 objects.remove(svec);
228 PCEPObjectiveFunctionObject of = null;
229 PCEPGlobalConstraintsObject gc = null;
230 PCEPExcludeRouteObject xro = null;
231 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
234 while (!objects.isEmpty()) {
235 final PCEPObject obj = objects.get(0);
237 if (obj instanceof UnknownObject && ((UnknownObject) obj).isProcessed()) {
238 throw new PCEPDocumentedException("Unknown object in SVEC list.", ((UnknownObject) obj).getError());
244 if (obj instanceof PCEPObjectiveFunctionObject) {
245 of = (PCEPObjectiveFunctionObject) obj;
250 if (obj instanceof PCEPGlobalConstraintsObject) {
251 gc = (PCEPGlobalConstraintsObject) obj;
256 if (obj instanceof PCEPExcludeRouteObject) {
257 xro = (PCEPExcludeRouteObject) obj;
262 if (obj instanceof PCEPMetricObject) {
263 metrics.add((PCEPMetricObject) obj);
276 return new CompositeRequestSvecObject(svec, of, gc, xro, metrics);
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());