BUG-47 : switched subobjects to generated source code.
[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.impl.AbstractMessageParser;
21 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NotificationObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RpObject;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
34
35 import com.google.common.collect.Lists;
36
37 /**
38  * Parser for {@link PcntfMessage}
39  */
40 public class PCEPNotificationMessageParser extends AbstractMessageParser {
41
42         public static final int TYPE = 5;
43
44         public PCEPNotificationMessageParser(final ObjectHandlerRegistry registry) {
45                 super(registry);
46         }
47
48         @Override
49         public void serializeMessage(final Message message, final ByteBuf buffer) {
50                 if (!(message instanceof PcntfMessage)) {
51                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
52                                         + ". Needed PcntfMessage.");
53                 }
54                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
55
56                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
57                         if (n.getRps() != null && !n.getRps().isEmpty()) {
58                                 for (final Rps rps : n.getRps()) {
59                                         buffer.writeBytes(serializeObject(rps));
60                                 }
61                         }
62                         if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
63                                 throw new IllegalArgumentException("Message must contain at least one notification object");
64                         } else {
65                                 for (final Notifications not : n.getNotifications()) {
66                                         buffer.writeBytes(serializeObject(not));
67                                 }
68                         }
69                 }
70         }
71
72         @Override
73         public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
74                 if (buffer == null || buffer.length == 0) {
75                         throw new PCEPDeserializerException("Notification message cannot be empty.");
76                 }
77                 final List<Object> objs = parseObjects(buffer);
78
79                 return validate(objs);
80         }
81
82         public Message validate(final List<Object> objects) throws PCEPDeserializerException {
83                 if (objects == null) {
84                         throw new IllegalArgumentException("Passed list can't be null.");
85                 }
86
87                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
88
89                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
90
91                 while (!objects.isEmpty()) {
92                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
93                         try {
94                                 comObj = getValidNotificationComposite(objects);
95                         } catch (final PCEPDocumentedException e) {
96                                 final PcerrMessageBuilder b = new PcerrMessageBuilder();
97                                 b.setErrors(Arrays.asList(new ErrorsBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
98                                                 maping.getFromErrorsEnum(e.getError()).value).build()));
99                                 return new PcerrBuilder().setPcerrMessage(b.build()).build();
100                         }
101
102                         if (comObj == null) {
103                                 break;
104                         }
105
106                         compositeNotifications.add(comObj);
107                 }
108
109                 if (compositeNotifications.isEmpty()) {
110                         throw new PCEPDeserializerException("Atleast one CompositeNotifiObject is mandatory.");
111                 }
112
113                 if (!objects.isEmpty()) {
114                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
115                 }
116
117                 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
118         }
119
120         private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
121                         final List<Object> objects) throws PCEPDocumentedException {
122                 final List<Rps> requestParameters = Lists.newArrayList();
123                 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();
124                 Object obj;
125
126                 int state = 1;
127                 while (!objects.isEmpty()) {
128                         obj = objects.get(0);
129
130                         if (obj instanceof UnknownObject) {
131                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
132                         }
133
134                         switch (state) {
135                         case 1:
136                                 state = 2;
137                                 if (obj instanceof RpObject) {
138                                         final RpObject rp = (RpObject) obj;
139                                         if (rp.isProcessingRule()) {
140                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
141                                         }
142                                         requestParameters.add((Rps) rp);
143                                         state = 1;
144                                         break;
145                                 }
146                         case 2:
147                                 if (obj instanceof NotificationObject) {
148                                         final NotificationObject n = (NotificationObject) obj;
149                                         notifications.add((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications) n);
150                                         state = 2;
151                                         break;
152                                 }
153                                 state = 3;
154                         }
155
156                         if (state == 3) {
157                                 break;
158                         }
159
160                         objects.remove(obj);
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         @Override
172         public int getMessageType() {
173                 return TYPE;
174         }
175 }