Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPUpdateRequestMessageParser.java
index ace6fe69f561cfeb160b54270723c3e7cb597069..ca0ba404e6281be14ade122f959edc50d5858224 100644 (file)
@@ -12,13 +12,33 @@ import io.netty.buffer.ByteBuf;
 import java.util.List;
 
 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
-import org.opendaylight.protocol.pcep.impl.AbstractMessageParser;
+import org.opendaylight.protocol.pcep.PCEPDocumentedException;
+import org.opendaylight.protocol.pcep.PCEPErrors;
+import org.opendaylight.protocol.pcep.UnknownObject;
 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcupd;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcupdBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcupdMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.bandwidth.object.Bandwidth;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.include.route.object.Iro;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.Metrics;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.MetricsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.Lsp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.PcupdMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.Updates;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.UpdatesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.Path;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcupd.message.pcupd.message.updates.PathBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.srp.object.Srp;
+
+import com.google.common.collect.Lists;
 
 /**
- * Parser for {@link PcupdMessage}
+ * Parser for {@link Pcupd}
  */
 public class PCEPUpdateRequestMessageParser extends AbstractMessageParser {
 
@@ -30,126 +50,144 @@ public class PCEPUpdateRequestMessageParser extends AbstractMessageParser {
 
        @Override
        public void serializeMessage(final Message message, final ByteBuf buffer) {
-               if (!(message instanceof PcupdMessage)) {
+               if (!(message instanceof Pcupd)) {
                        throw new IllegalArgumentException("Wrong instance of PCEPMessage. Passed instance of " + message.getClass()
                                        + ". Nedded PcupdMessage.");
                }
-
+               final Pcupd msg = (Pcupd) message;
+               final List<Updates> updates = msg.getPcupdMessage().getUpdates();
+               for (final Updates update : updates) {
+                       buffer.writeBytes(serializeObject(update.getSrp()));
+                       buffer.writeBytes(serializeObject(update.getLsp()));
+                       final Path p = update.getPath();
+                       if (p != null) {
+                               buffer.writeBytes(serializeObject(p.getEro()));
+                               if (p.getLspa() != null) {
+                                       buffer.writeBytes(serializeObject(p.getLspa()));
+                               }
+                               if (p.getBandwidth() != null) {
+                                       buffer.writeBytes(serializeObject(p.getBandwidth()));
+                               }
+                               if (p.getMetrics() != null && !p.getMetrics().isEmpty()) {
+                                       for (final Metrics m : p.getMetrics()) {
+                                               buffer.writeBytes(serializeObject(m.getMetric()));
+                                       }
+                               }
+                               if (p.getIro() != null) {
+                                       buffer.writeBytes(serializeObject(p.getIro()));
+                               }
+                       }
+               }
        }
 
        @Override
-       public PcupdMessage parseMessage(final byte[] buffer) throws PCEPDeserializerException {
-               // TODO Auto-generated method stub
-               return null;
-       }
-
-       public List<Message> validate(final List<Object> objects) throws PCEPDeserializerException {
+       protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException, PCEPDocumentedException {
                if (objects == null) {
                        throw new IllegalArgumentException("Passed list can't be null.");
                }
+               if (objects.isEmpty()) {
+                       throw new PCEPDeserializerException("Pcup message cannot be empty.");
+               }
+
+               final List<Updates> updateRequests = Lists.newArrayList();
 
-               // final List<CompositeUpdateRequestObject> updateRequests = new ArrayList<CompositeUpdateRequestObject>();
-               //
-               // while (!objects.isEmpty()) {
-               // if (objects.get(0) instanceof UnknownObject) {
-               // logger.warn("Unknown object found (should be Lsp) - {}.", objects.get(0));
-               // return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(((UnknownObject)
-               // objects.get(0)).getError())));
-               // }
-               // if (!(objects.get(0) instanceof PCEPLspObject)) {
-               // return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
-               // }
-               //
-               // final PCEPLspObject lsp = (PCEPLspObject) objects.get(0);
-               // objects.remove(0);
-               //
-               // final List<CompositeUpdPathObject> paths = new ArrayList<CompositeUpdPathObject>();
-               //
-               // if (!objects.isEmpty()) {
-               // try {
-               // CompositeUpdPathObject path;
-               // path = this.getValidCompositePath(objects);
-               // while (path != null) {
-               // paths.add(path);
-               // if (objects.isEmpty()) {
-               // break;
-               // }
-               // path = this.getValidCompositePath(objects);
-               // }
-               // } catch (final PCEPDocumentedException e) {
-               // logger.warn("Serializing failed: {}.", e);
-               // return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(e.getError())));
-               // }
-               // }
-               //
-               // updateRequests.add(new CompositeUpdateRequestObject(lsp, paths));
-               // }
-               //
-               // if (updateRequests.isEmpty()) {
-               // return Arrays.asList((Message) new PCEPErrorMessage(new PCEPErrorObject(PCEPErrors.LSP_MISSING)));
-               // }
-               //
-               // if (!objects.isEmpty()) {
-               // throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
-               // }
-               //
-               // return Arrays.asList((Message) new PCEPUpdateRequestMessage(updateRequests));
-               return null;
+               while (!objects.isEmpty()) {
+                       final Updates update = getValidUpdates(objects);
+                       if (update != null) {
+                               updateRequests.add(update);
+                       }
+               }
+               if (!objects.isEmpty()) {
+                       if (objects.get(0) instanceof UnknownObject) {
+                               throw new PCEPDocumentedException("Unknown object encountered", ((UnknownObject) objects.get(0)).getError());
+                       }
+                       throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
+               }
+               return new PcupdBuilder().setPcupdMessage(new PcupdMessageBuilder().setUpdates(updateRequests).build()).build();
        }
 
-       // private CompositeUpdPathObject getValidCompositePath(final List<Object> objects) throws PCEPDocumentedException {
-       // if (!(objects.get(0) instanceof PCEPExplicitRouteObject)) {
-       // return null;
-       // }
-       //
-       // if (objects.get(0) instanceof UnknownObject) {
-       // }
-       // final PCEPExplicitRouteObject explicitRoute = (PCEPExplicitRouteObject) objects.get(0);
-       // objects.remove(0);
-       //
-       // PCEPLspaObject pathLspa = null;
-       // PCEPRequestedPathBandwidthObject pathBandwidth = null;
-       // final List<PCEPMetricObject> pathMetrics = new ArrayList<PCEPMetricObject>();
-       //
-       // 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 PCEPLspaObject) {
-       // pathLspa = (PCEPLspaObject) obj;
-       // break;
-       // }
-       // case 2:
-       // state = 3;
-       // if (obj instanceof PCEPRequestedPathBandwidthObject) {
-       // pathBandwidth = (PCEPRequestedPathBandwidthObject) obj;
-       // break;
-       // }
-       // case 3:
-       // state = 4;
-       // if (obj instanceof PCEPMetricObject) {
-       // pathMetrics.add((PCEPMetricObject) obj);
-       // state = 3;
-       // break;
-       // }
-       // }
-       //
-       // if (state == 4) {
-       // break;
-       // }
-       //
-       // objects.remove(0);
-       // }
-       //
-       // return new CompositeUpdPathObject(explicitRoute, pathLspa, pathBandwidth, pathMetrics);
-       // }
+       private Updates getValidUpdates(final List<Object> objects) throws PCEPDocumentedException {
+               final UpdatesBuilder builder = new UpdatesBuilder();
+               if (objects.get(0) instanceof Srp) {
+                       builder.setSrp((Srp) objects.get(0));
+                       objects.remove(0);
+               } else {
+                       throw new PCEPDocumentedException("Srp object missing.", PCEPErrors.SRP_MISSING);
+               }
+               if (objects.get(0) instanceof Lsp) {
+                       builder.setLsp((Lsp) objects.get(0));
+                       objects.remove(0);
+               } else {
+                       throw new PCEPDocumentedException("Lsp object missing.", PCEPErrors.LSP_MISSING);
+               }
+               if (!objects.isEmpty()) {
+                       final PathBuilder pBuilder = new PathBuilder();
+                       if (objects.get(0) instanceof Ero) {
+                               pBuilder.setEro((Ero) objects.get(0));
+                               objects.remove(0);
+                       } else {
+                               throw new PCEPDocumentedException("Ero object missing.", PCEPErrors.ERO_MISSING);
+                       }
+                       parsePath(objects, pBuilder);
+                       builder.setPath(pBuilder.build());
+               }
+               return builder.build();
+       }
+
+       private void parsePath(final List<Object> objects, final PathBuilder pBuilder) throws PCEPDocumentedException {
+               final List<Metrics> pathMetrics = Lists.newArrayList();
+               Object obj;
+               State state = State.Init;
+               while (!objects.isEmpty() && !state.equals(State.End)) {
+                       obj = objects.get(0);
+                       switch (state) {
+                       case Init:
+                               state = State.LspaIn;
+                               if (obj instanceof Lspa) {
+                                       pBuilder.setLspa((Lspa) obj);
+                                       break;
+                               }
+                       case LspaIn:
+                               state = State.BandwidthIn;
+                               if (obj instanceof Bandwidth) {
+                                       pBuilder.setBandwidth((Bandwidth) obj);
+                                       break;
+                               }
+                       case BandwidthIn:
+                               state = State.MetricIn;
+                               if (obj instanceof Metric) {
+                                       pathMetrics.add(new MetricsBuilder().setMetric((Metric) obj).build());
+                                       state = State.BandwidthIn;
+                                       break;
+                               }
+                       case MetricIn:
+                               state = State.IroIn;
+                               if (obj instanceof Iro) {
+                                       pBuilder.setIro((Iro) obj);
+                                       break;
+                               }
+                       case IroIn:
+                               state = State.End;
+                               break;
+                       case End:
+                               break;
+                       default:
+                               if (obj instanceof UnknownObject) {
+                                       throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
+                               }
+                       }
+                       if (!state.equals(State.End)) {
+                               objects.remove(0);
+                       }
+               }
+               if (!pathMetrics.isEmpty()) {
+                       pBuilder.setMetrics(pathMetrics);
+               }
+       }
+
+       private enum State {
+               Init, LspaIn, BandwidthIn, MetricIn, IroIn, End
+       }
 
        @Override
        public int getMessageType() {