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.object.CompositeErrorObject;
22 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
23 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
24 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
28 * PCEPErrorMessage validator. Validates message integrity.
30 public class PCEPErrorMessageValidator extends PCEPMessageValidator {
33 public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
35 throw new IllegalArgumentException("Passed list can't be null.");
37 PCEPOpenObject openObj = null;
38 final List<CompositeErrorObject> errors = new ArrayList<CompositeErrorObject>();
39 final List<PCEPErrorObject> errorObjects = new ArrayList<PCEPErrorObject>();
43 while (!objects.isEmpty()) {
46 if (obj instanceof UnknownObject)
47 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject) obj).getError())));
51 if (obj instanceof PCEPErrorObject) {
52 errorObjects.add((PCEPErrorObject) obj);
58 if (obj instanceof PCEPOpenObject) {
59 openObj = (PCEPOpenObject) obj;
63 while (!objects.isEmpty()) {
64 CompositeErrorObject comObj;
67 comObj = getValidErrorComposite(objects);
68 } catch (final PCEPDocumentedException e) {
69 return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
89 if (errors.isEmpty() && errorObjects.isEmpty())
90 throw new PCEPDeserializerException("At least one PCEPErrorObject is mandatory.");
92 if (!objects.isEmpty())
93 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
95 return Arrays.asList((Message) new PCEPErrorMessage(openObj, errorObjects, errors));
98 private static CompositeErrorObject getValidErrorComposite(final List<PCEPObject> objects) throws PCEPDocumentedException,
99 PCEPDeserializerException {
100 final List<PCEPRequestParameterObject> requestParameters = new ArrayList<PCEPRequestParameterObject>();
101 final List<PCEPErrorObject> errors = new ArrayList<PCEPErrorObject>();
105 while (!objects.isEmpty()) {
106 obj = objects.get(0);
108 if (obj instanceof UnknownObject)
109 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
114 if (obj instanceof PCEPRequestParameterObject) {
115 if (((PCEPRequestParameterObject) obj).isProcessed())
116 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
117 requestParameters.add((PCEPRequestParameterObject) obj);
122 if (obj instanceof PCEPErrorObject) {
123 errors.add((PCEPErrorObject) obj);
136 if (errors.isEmpty())
139 return new CompositeErrorObject(requestParameters, errors);