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.PCEPUpdateRequestMessage;
22 import org.opendaylight.protocol.pcep.object.CompositeUpdPathObject;
23 import org.opendaylight.protocol.pcep.object.CompositeUpdateRequestObject;
24 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
25 import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
26 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
27 import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
28 import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
29 import org.opendaylight.protocol.pcep.object.PCEPRequestedPathBandwidthObject;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * PCEPUpdateRequestMessage validator. Validates message integrity.
37 public class PCEPUpdateRequestMessageValidator extends PCEPMessageValidator {
39 private static final Logger logger = LoggerFactory.getLogger(PCEPUpdateRequestMessageValidator.class);
42 public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
44 throw new IllegalArgumentException("Passed list can't be null.");
46 final List<CompositeUpdateRequestObject> updateRequests = new ArrayList<CompositeUpdateRequestObject>();
48 while (!objects.isEmpty()) {
49 if (objects.get(0) instanceof UnknownObject) {
50 logger.warn("Unknown object found (should be Lsp) - {}.", objects.get(0));
51 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) objects.get(0)).getError())));
53 if (!(objects.get(0) instanceof PCEPLspObject))
54 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
56 final PCEPLspObject lsp = (PCEPLspObject) objects.get(0);
59 final List<CompositeUpdPathObject> paths = new ArrayList<CompositeUpdPathObject>();
61 if (!objects.isEmpty()) {
63 CompositeUpdPathObject path;
64 path = this.getValidCompositePath(objects);
65 while (path != null) {
67 if (objects.isEmpty())
69 path = this.getValidCompositePath(objects);
71 } catch (final PCEPDocumentedException e) {
72 logger.warn("Serializing failed: {}.", e);
73 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
77 updateRequests.add(new CompositeUpdateRequestObject(lsp, paths));
80 if (updateRequests.isEmpty())
81 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
83 if (!objects.isEmpty())
84 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
86 return Arrays.asList((Message) new PCEPUpdateRequestMessage(updateRequests));
89 private CompositeUpdPathObject getValidCompositePath(final List<PCEPObject> objects) throws PCEPDocumentedException {
90 if (!(objects.get(0) instanceof PCEPExplicitRouteObject))
93 if (objects.get(0) instanceof UnknownObject)
94 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
95 final PCEPExplicitRouteObject explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
98 PCEPLspaObject pathLspa = null;
99 PCEPRequestedPathBandwidthObject pathBandwidth = null;
100 final List<PCEPMetricObject> pathMetrics = new ArrayList<PCEPMetricObject>();
104 while (!objects.isEmpty()) {
105 obj = objects.get(0);
106 if (obj instanceof UnknownObject) {
107 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
113 if (obj instanceof PCEPLspaObject) {
114 pathLspa = (PCEPLspaObject) obj;
119 if (obj instanceof PCEPRequestedPathBandwidthObject) {
120 pathBandwidth = (PCEPRequestedPathBandwidthObject) obj;
125 if (obj instanceof PCEPMetricObject) {
126 pathMetrics.add((PCEPMetricObject) obj);
138 return new CompositeUpdPathObject(explicitRoute, pathLspa, pathBandwidth, pathMetrics);