Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPNotificationMessageParser.java
index 6936eba87f62c4ac7b6f28d34ad95ca4f6541260..068e25a91d296de35ca2d3d064dccd4e84b90843 100644 (file)
@@ -17,7 +17,6 @@ import org.opendaylight.protocol.pcep.PCEPDocumentedException;
 import org.opendaylight.protocol.pcep.PCEPErrorMapping;
 import org.opendaylight.protocol.pcep.PCEPErrors;
 import org.opendaylight.protocol.pcep.UnknownObject;
-import org.opendaylight.protocol.pcep.impl.AbstractMessageParser;
 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
@@ -73,20 +72,15 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
        }
 
        @Override
-       public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
-               if (buffer == null || buffer.length == 0) {
-                       throw new PCEPDeserializerException("Notification message cannot be empty.");
-               }
-               final List<Object> objs = parseObjects(buffer);
-
-               return validate(objs);
-       }
-
-       public Message validate(final List<Object> objects) throws PCEPDeserializerException {
+       protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
                if (objects == null) {
                        throw new IllegalArgumentException("Passed list can't be null.");
                }
 
+               if (objects.isEmpty()) {
+                       throw new PCEPDeserializerException("Notification message cannot be empty.");
+               }
+
                final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
 
                final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
@@ -127,8 +121,8 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
                final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> notifications = Lists.newArrayList();
                Object obj;
 
-               int state = 1;
-               while (!objects.isEmpty()) {
+               State state = State.Init;
+               while (!objects.isEmpty() && !state.equals(State.End)) {
                        obj = objects.get(0);
 
                        if (obj instanceof UnknownObject) {
@@ -136,32 +130,34 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
                        }
 
                        switch (state) {
-                       case 1:
-                               state = 2;
+                       case Init:
+                               state = State.RpIn;
                                if (obj instanceof Rp) {
                                        final Rp rp = (Rp) obj;
                                        if (rp.isProcessingRule()) {
                                                throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
                                        }
                                        requestParameters.add(new RpsBuilder().setRp(rp).build());
-                                       state = 1;
+                                       state = State.Init;
                                        break;
                                }
-                       case 2:
+                       case RpIn:
+                               state = State.NotificationIn;
                                if (obj instanceof CNotification) {
                                        final CNotification n = (CNotification) obj;
                                        notifications.add(new NotificationsBuilder().setCNotification(n).build());
-                                       state = 2;
+                                       state = State.RpIn;
                                        break;
                                }
-                               state = 3;
-                       }
-
-                       if (state == 3) {
+                       case NotificationIn:
+                               state = State.End;
+                               break;
+                       case End:
                                break;
                        }
-
-                       objects.remove(obj);
+                       if (!state.equals(State.End)) {
+                               objects.remove(0);
+                       }
                }
 
                if (notifications.isEmpty()) {
@@ -172,6 +168,10 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
                                notifications).setRps(requestParameters).build();
        }
 
+       private enum State {
+               Init, RpIn, NotificationIn, End
+       }
+
        @Override
        public int getMessageType() {
                return TYPE;