Bug-731: PCEPErrors enum with error-type and error-value
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPNotificationMessageParser.java
index c1dc700caca9ad39550107a454837a6f9d4f4bf1..401f10cb4875e9c2d0e3af6cc99ea50d1d4ae2d2 100644 (file)
@@ -7,12 +7,17 @@
  */
 package org.opendaylight.protocol.pcep.impl.message;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
 import io.netty.buffer.ByteBuf;
-
+import io.netty.buffer.Unpooled;
 import java.util.List;
-
-import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
+import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
+import org.opendaylight.protocol.pcep.spi.MessageUtil;
+import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
+import org.opendaylight.protocol.pcep.spi.PCEPErrors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
 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.Object;
@@ -25,127 +30,119 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.typ
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
 
-import com.google.common.collect.Lists;
-
 /**
  * Parser for {@link PcntfMessage}
  */
 public class PCEPNotificationMessageParser extends AbstractMessageParser {
 
-       public static final int TYPE = 5;
-
-       public PCEPNotificationMessageParser(final ObjectHandlerRegistry registry) {
-               super(registry);
-       }
-
-       @Override
-       public void serializeMessage(final Message message, final ByteBuf buffer) {
-               if (!(message instanceof PcntfMessage)) {
-                       throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
-                                       + ". Needed PcntfMessage.");
-               }
-               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
-
-               for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
-                       if (n.getRps() != null && !n.getRps().isEmpty()) {
-                               for (final Rps rps : n.getRps()) {
-                                       buffer.writeBytes(serializeObject(rps.getRp()));
-                               }
-                       }
-                       if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
-                               throw new IllegalArgumentException("Message must contain at least one notification object");
-                       } else {
-                               for (final Notifications not : n.getNotifications()) {
-                                       buffer.writeBytes(serializeObject(not.getCNotification()));
-                               }
-                       }
-               }
-       }
-
-       @Override
-       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 List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
-
-               while (!objects.isEmpty()) {
-                       org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
-                       comObj = getValidNotificationComposite(objects);
-
-                       if (comObj == null) {
-                               break;
-                       }
-                       compositeNotifications.add(comObj);
-               }
-
-               if (compositeNotifications.isEmpty()) {
-                       throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
-               }
-
-               if (!objects.isEmpty()) {
-                       throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
-               }
-
-               return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
-       }
-
-       private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
-                       final List<Object> objects) {
-               final List<Rps> requestParameters = Lists.newArrayList();
-               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;
-
-               State state = State.Init;
-               while (!objects.isEmpty() && !state.equals(State.End)) {
-                       obj = objects.get(0);
-                       switch (state) {
-                       case Init:
-                               state = State.RpIn;
-                               if (obj instanceof Rp) {
-                                       final Rp rp = (Rp) obj;
-                                       requestParameters.add(new RpsBuilder().setRp(rp).build());
-                                       state = State.Init;
-                                       break;
-                               }
-                       case RpIn:
-                               state = State.NotificationIn;
-                               if (obj instanceof CNotification) {
-                                       final CNotification n = (CNotification) obj;
-                                       notifications.add(new NotificationsBuilder().setCNotification(n).build());
-                                       state = State.RpIn;
-                                       break;
-                               }
-                       case NotificationIn:
-                               state = State.End;
-                               break;
-                       case End:
-                               break;
-                       }
-                       if (!state.equals(State.End)) {
-                               objects.remove(0);
-                       }
-               }
-
-               if (notifications.isEmpty()) {
-                       return null;
-               }
-
-               return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
-                               notifications).setRps(requestParameters).build();
-       }
-
-       private enum State {
-               Init, RpIn, NotificationIn, End
-       }
-
-       @Override
-       public int getMessageType() {
-               return TYPE;
-       }
+    public static final int TYPE = 5;
+
+    public PCEPNotificationMessageParser(final ObjectRegistry registry) {
+        super(registry);
+    }
+
+    @Override
+    public void serializeMessage(final Message message, final ByteBuf out) {
+        Preconditions.checkArgument(message instanceof PcntfMessage, "Wrong instance of Message. Passed instance of %s. Need PcntfMessage.", message.getClass());
+        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
+
+        ByteBuf buffer = Unpooled.buffer();
+        for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
+            if (n.getRps() != null && !n.getRps().isEmpty()) {
+                for (final Rps rps : n.getRps()) {
+                    serializeObject(rps.getRp(), buffer);
+                }
+            }
+            if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
+                throw new IllegalArgumentException("Message must contain at least one notification object");
+            } else {
+                for (final Notifications not : n.getNotifications()) {
+                    serializeObject(not.getCNotification(), buffer);
+                }
+            }
+        }
+        MessageUtil.formatMessage(TYPE, buffer, out);
+    }
+
+    @Override
+    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 List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
+
+        while (!objects.isEmpty()) {
+            org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
+            comObj = getValidNotificationComposite(objects, errors);
+
+            if (comObj == null) {
+                break;
+            }
+            compositeNotifications.add(comObj);
+        }
+        if (compositeNotifications.isEmpty()) {
+            throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
+        }
+        if (!objects.isEmpty()) {
+            throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
+        }
+        return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
+    }
+
+    private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
+            final List<Object> objects, final List<Message> errors) {
+        final List<Rps> requestParameters = Lists.newArrayList();
+        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;
+
+        State state = State.Init;
+        while (!objects.isEmpty() && !state.equals(State.End)) {
+            obj = objects.get(0);
+            switch (state) {
+            case Init:
+                state = State.RpIn;
+                if (obj instanceof Rp) {
+                    final Rp rp = (Rp) obj;
+                    if (rp.isProcessingRule()) {
+                        errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.<Rp>absent()));
+                        return null;
+                    }
+                    requestParameters.add(new RpsBuilder().setRp(rp).build());
+                    state = State.Init;
+                    break;
+                }
+            case RpIn:
+                state = State.NotificationIn;
+                if (obj instanceof CNotification) {
+                    final CNotification n = (CNotification) obj;
+                    notifications.add(new NotificationsBuilder().setCNotification(n).build());
+                    state = State.RpIn;
+                    break;
+                }
+            case NotificationIn:
+                state = State.End;
+                break;
+            case End:
+                break;
+            }
+            if (!state.equals(State.End)) {
+                objects.remove(0);
+            }
+        }
+
+        if (notifications.isEmpty()) {
+            return null;
+        }
+
+        return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
+                notifications).setRps(requestParameters).build();
+    }
+
+    private enum State {
+        Init, RpIn, NotificationIn, End
+    }
 }