Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCCreateMessageValidator.java
index 92303b1ba0ec7d9e49aec3308819eb58d941db4c..42fa0c0756c1268ce53b6e0263bff53345781680 100644 (file)
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.opendaylight.protocol.pcep.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.PCEPDocumentedException;
+import org.opendaylight.protocol.pcep.PCEPErrors;
+import org.opendaylight.protocol.pcep.PCEPObject;
+import org.opendaylight.protocol.pcep.impl.PCEPMessageValidator;
+import org.opendaylight.protocol.pcep.impl.object.UnknownObject;
+import org.opendaylight.protocol.pcep.message.PCCreateMessage;
+import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
+import org.opendaylight.protocol.pcep.object.CompositeInstantiationObject;
+import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
+import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
+import org.opendaylight.protocol.pcep.object.PCEPExplicitRouteObject;
+import org.opendaylight.protocol.pcep.object.PCEPLspaObject;
+import org.opendaylight.protocol.pcep.object.PCEPMetricObject;
+import org.opendaylight.protocol.pcep.object.PCEPRequestedPathBandwidthObject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
 
 /**
  * PCCCreateMessage validator. Validates message integrity.
  */
-// FIXME: merge with parser
-class PCCreateMessageValidator {
-
-       // @Override
-       // public List<Message> validate(final List<Object> objects) throws PCEPDeserializerException {
-       // if (objects == null)
-       // throw new IllegalArgumentException("Passed list can't be null.");
-       //
-       // final List<CompositeInstantiationObject> insts = new ArrayList<CompositeInstantiationObject>();
-       //
-       // CompositeInstantiationObject inst;
-       // while (!objects.isEmpty()) {
-       // try {
-       // if ((inst = this.getValidInstantiationObject(objects)) == null)
-       // break;
-       // } catch (final PCEPDocumentedException e) {
-       // return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
-       // }
-       //
-       // insts.add(inst);
-       // }
-       //
-       // if (insts.isEmpty())
-       // throw new PCEPDeserializerException("At least one CompositeInstantiationObject is mandatory.");
-       //
-       // if (!objects.isEmpty())
-       // throw new PCEPDeserializerException("Unprocessed objects: " + objects);
-       //
-       // return Arrays.asList((Message) new PCCreateMessage(insts));
-       // }
-       //
-       // private CompositeInstantiationObject getValidInstantiationObject(final List<Object> objects) throws
-       // PCEPDocumentedException {
-       // if (objects.get(0) instanceof UnknownObject)
-       // throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
-       // if (!(objects.get(0) instanceof EndpointsObject))
-       // return null;
-       //
-       // final EndpointsObject endPoints = ((EndpointsObject) objects.get(0));
-       // objects.remove(0);
-       //
-       // if (objects.get(0) instanceof UnknownObject)
-       // throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
-       // if (!(objects.get(0) instanceof LspaObject))
-       // throw new PCEPDocumentedException("LSPA Object must be second.", PCEPErrors.LSPA_MISSING);
-       // final LspaObject lspa = (LspaObject) objects.get(0);
-       // objects.remove(0);
-       //
-       // ExplicitRouteObject ero = null;
-       // BandwidthObject bandwidth = null;
-       // final List<MetricObject> metrics = Lists.newArrayList();
-       //
-       // Object obj;
-       // int state = 1;
-       // while (!objects.isEmpty()) {
-       // obj = objects.get(0);
-       // if (obj instanceof UnknownObject) {
-       // throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
-       // }
-       //
-       // switch (state) {
-       // case 1:
-       // state = 2;
-       // if (obj instanceof ExplicitRouteObject) {
-       // ero = (ExplicitRouteObject) obj;
-       // break;
-       // }
-       // case 2:
-       // state = 3;
-       // if (obj instanceof BandwidthObject) {
-       // bandwidth = (BandwidthObject) obj;
-       // break;
-       // }
-       // case 3:
-       // state = 4;
-       // if (obj instanceof MetricObject) {
-       // metrics.add((MetricObject) obj);
-       // state = 3;
-       // break;
-       // }
-       // }
-       //
-       // if (state == 4)
-       // break;
-       //
-       // objects.remove(0);
-       // }
-       //
-       // return new CompositeInstantiationObject(endPoints, lspa, ero, bandwidth, metrics);
-       // }
+public class PCCreateMessageValidator extends PCEPMessageValidator {
+
+       @Override
+       public List<Message> validate(final List<PCEPObject> objects) throws PCEPDeserializerException {
+               if (objects == null)
+                       throw new IllegalArgumentException("Passed list can't be null.");
+
+               final List<CompositeInstantiationObject> insts = new ArrayList<CompositeInstantiationObject>();
+
+               CompositeInstantiationObject inst;
+               while (!objects.isEmpty()) {
+                       try {
+                               if ((inst = this.getValidInstantiationObject(objects)) == null)
+                                       break;
+                       } catch (final PCEPDocumentedException e) {
+                               return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
+                       }
+
+                       insts.add(inst);
+               }
+
+               if (insts.isEmpty())
+                       throw new PCEPDeserializerException("At least one CompositeInstantiationObject is mandatory.");
+
+               if (!objects.isEmpty())
+                       throw new PCEPDeserializerException("Unprocessed objects: " + objects);
+
+               return Arrays.asList((Message) new PCCreateMessage(insts));
+       }
+
+       private CompositeInstantiationObject getValidInstantiationObject(final List<PCEPObject> objects) throws PCEPDocumentedException {
+               if (objects.get(0) instanceof UnknownObject)
+                       throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
+               if (!(objects.get(0) instanceof PCEPEndPointsObject<?>))
+                       return null;
+
+               final PCEPEndPointsObject<?> endPoints = ((PCEPEndPointsObject<?>) objects.get(0));
+               objects.remove(0);
+
+               if (objects.get(0) instanceof UnknownObject)
+                       throw new PCEPDocumentedException("Unknown object", ((UnknownObject) objects.get(0)).getError());
+               if (!(objects.get(0) instanceof PCEPLspaObject))
+                       throw new PCEPDocumentedException("LSPA Object must be second.", PCEPErrors.LSPA_MISSING);
+               final PCEPLspaObject lspa = (PCEPLspaObject) objects.get(0);
+               objects.remove(0);
+
+               PCEPExplicitRouteObject ero = null;
+               PCEPRequestedPathBandwidthObject bandwidth = null;
+               final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
+
+               PCEPObject obj;
+               int state = 1;
+               while (!objects.isEmpty()) {
+                       obj = objects.get(0);
+                       if (obj instanceof UnknownObject) {
+                               throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
+                       }
+
+                       switch (state) {
+                       case 1:
+                               state = 2;
+                               if (obj instanceof PCEPExplicitRouteObject) {
+                                       ero = (PCEPExplicitRouteObject) obj;
+                                       break;
+                               }
+                       case 2:
+                               state = 3;
+                               if (obj instanceof PCEPRequestedPathBandwidthObject) {
+                                       bandwidth = (PCEPRequestedPathBandwidthObject) obj;
+                                       break;
+                               }
+                       case 3:
+                               state = 4;
+                               if (obj instanceof PCEPMetricObject) {
+                                       metrics.add((PCEPMetricObject) obj);
+                                       state = 3;
+                                       break;
+                               }
+                       }
+
+                       if (state == 4)
+                               break;
+
+                       objects.remove(0);
+               }
+
+               return new CompositeInstantiationObject(endPoints, lspa, ero, bandwidth, metrics);
+       }
 
 }