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.List;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.PCEPErrors;
17 import org.opendaylight.protocol.pcep.PCEPObject;
18 import org.opendaylight.protocol.pcep.impl.PCEPMessageValidator;
19 import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
20 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
21 import org.opendaylight.protocol.pcep.message.PCEPReportMessage;
22 import org.opendaylight.protocol.pcep.object.CompositeRptPathObject;
23 import org.opendaylight.protocol.pcep.object.CompositeStateReportObject;
24 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
25 import org.opendaylight.protocol.pcep.object.PCEPExistingPathBandwidthObject;
26 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
27 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
28 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
29 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
30 import org.opendaylight.protocol.pcep.object.PCEPReportedRouteObject;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
34 * PCEPReportMessage validator. Validates message integrity.
36 public class PCEPReportMessageValidator extends PCEPMessageValidator {
39 public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
41 throw new IllegalArgumentException("Passed list can't be null.");
43 final List<CompositeStateReportObject> report = new ArrayList<CompositeStateReportObject>();
45 while (!objects.isEmpty()) {
46 if (objects.get(0) instanceof UnknownObject)
47 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) objects.get(0)).getError())));
49 if (!(objects.get(0) instanceof PCEPLspObject))
50 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
52 final PCEPLspObject lsp = (PCEPLspObject) objects.get(0);
55 final List<CompositeRptPathObject> paths = new ArrayList<CompositeRptPathObject>();
57 if (!objects.isEmpty()) {
59 CompositeRptPathObject path;
60 path = this.getValidCompositePath(objects);
61 while (path != null) {
63 if (objects.isEmpty())
65 path = this.getValidCompositePath(objects);
67 } catch (final PCEPDocumentedException e) {
68 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
72 report.add(new CompositeStateReportObject(lsp, paths));
76 throw new PCEPDeserializerException("Atleast one CompositeStateReportObject is mandatory.");
78 if (!objects.isEmpty()) {
79 if (objects.get(0) instanceof UnknownObject)
80 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) objects.get(0)).getError())));
81 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
84 return Arrays.asList((Message) new PCEPReportMessage(report));
87 private CompositeRptPathObject getValidCompositePath(final List<PCEPObject> objects) throws PCEPDocumentedException {
88 if (objects.get(0) instanceof UnknownObject)
89 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
90 if (!(objects.get(0) instanceof PCEPExplicitRouteObject))
92 final PCEPExplicitRouteObject explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
95 PCEPLspaObject pathLspa = null;
96 PCEPExistingPathBandwidthObject pathBandwidth = null;
97 PCEPReportedRouteObject pathRro = null;
98 final List<PCEPMetricObject> pathMetrics = new ArrayList<PCEPMetricObject>();
102 while (!objects.isEmpty()) {
103 obj = objects.get(0);
104 if (obj instanceof UnknownObject) {
105 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
111 if (obj instanceof PCEPLspaObject) {
112 pathLspa = (PCEPLspaObject) obj;
117 if (obj instanceof PCEPExistingPathBandwidthObject) {
118 pathBandwidth = (PCEPExistingPathBandwidthObject) obj;
124 if (obj instanceof PCEPReportedRouteObject) {
125 pathRro = (PCEPReportedRouteObject) obj;
130 if (obj instanceof PCEPMetricObject) {
131 pathMetrics.add((PCEPMetricObject) obj);
143 return new CompositeRptPathObject(explicitRoute, pathLspa, pathBandwidth, pathRro, pathMetrics);