Rework parser infrastructure to support partial message processing
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPNotificationMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.message;
9
10 import io.netty.buffer.ByteBuf;
11
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPErrorMapping;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.pcep.UnknownObject;
20 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
36
37 import com.google.common.collect.Lists;
38
39 /**
40  * Parser for {@link PcntfMessage}
41  */
42 public class PCEPNotificationMessageParser extends AbstractMessageParser {
43
44         public static final int TYPE = 5;
45
46         public PCEPNotificationMessageParser(final ObjectHandlerRegistry registry) {
47                 super(registry);
48         }
49
50         @Override
51         public void serializeMessage(final Message message, final ByteBuf buffer) {
52                 if (!(message instanceof PcntfMessage)) {
53                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
54                                         + ". Needed PcntfMessage.");
55                 }
56                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
57
58                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
59                         if (n.getRps() != null && !n.getRps().isEmpty()) {
60                                 for (final Rps rps : n.getRps()) {
61                                         buffer.writeBytes(serializeObject(rps.getRp()));
62                                 }
63                         }
64                         if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
65                                 throw new IllegalArgumentException("Message must contain at least one notification object");
66                         } else {
67                                 for (final Notifications not : n.getNotifications()) {
68                                         buffer.writeBytes(serializeObject(not.getCNotification()));
69                                 }
70                         }
71                 }
72         }
73
74         @Override
75         protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
76                 if (objects == null) {
77                         throw new IllegalArgumentException("Passed list can't be null.");
78                 }
79
80                 if (objects.isEmpty()) {
81                         throw new PCEPDeserializerException("Notification message cannot be empty.");
82                 }
83
84                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
85
86                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
87
88                 while (!objects.isEmpty()) {
89                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
90                         try {
91                                 comObj = getValidNotificationComposite(objects);
92                         } catch (final PCEPDocumentedException e) {
93                                 final PcerrMessageBuilder b = new PcerrMessageBuilder();
94                                 b.setErrors(Arrays.asList(new ErrorsBuilder().setErrorObject(
95                                                 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
96                                                                 maping.getFromErrorsEnum(e.getError()).value).build()).build()));
97                                 return new PcerrBuilder().setPcerrMessage(b.build()).build();
98                         }
99
100                         if (comObj == null) {
101                                 break;
102                         }
103
104                         compositeNotifications.add(comObj);
105                 }
106
107                 if (compositeNotifications.isEmpty()) {
108                         throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
109                 }
110
111                 if (!objects.isEmpty()) {
112                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
113                 }
114
115                 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
116         }
117
118         private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
119                         final List<Object> objects) throws PCEPDocumentedException {
120                 final List<Rps> requestParameters = Lists.newArrayList();
121                 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();
122                 Object obj;
123
124                 State state = State.Init;
125                 while (!objects.isEmpty() && !state.equals(State.End)) {
126                         obj = objects.get(0);
127
128                         if (obj instanceof UnknownObject) {
129                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
130                         }
131
132                         switch (state) {
133                         case Init:
134                                 state = State.RpIn;
135                                 if (obj instanceof Rp) {
136                                         final Rp rp = (Rp) obj;
137                                         if (rp.isProcessingRule()) {
138                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
139                                         }
140                                         requestParameters.add(new RpsBuilder().setRp(rp).build());
141                                         state = State.Init;
142                                         break;
143                                 }
144                         case RpIn:
145                                 state = State.NotificationIn;
146                                 if (obj instanceof CNotification) {
147                                         final CNotification n = (CNotification) obj;
148                                         notifications.add(new NotificationsBuilder().setCNotification(n).build());
149                                         state = State.RpIn;
150                                         break;
151                                 }
152                         case NotificationIn:
153                                 state = State.End;
154                                 break;
155                         case End:
156                                 break;
157                         }
158                         if (!state.equals(State.End)) {
159                                 objects.remove(0);
160                         }
161                 }
162
163                 if (notifications.isEmpty()) {
164                         return null;
165                 }
166
167                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
168                                 notifications).setRps(requestParameters).build();
169         }
170
171         private enum State {
172                 Init, RpIn, NotificationIn, End
173         }
174
175         @Override
176         public int getMessageType() {
177                 return TYPE;
178         }
179 }