BUG-47 : unfinished PCEP migration to generated DTOs.
[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.AbstractMessageParser;
21 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
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         private final int TYPE = 5;
43
44         public PCEPNotificationMessageParser(final HandlerRegistry 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                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
54
55                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
56                         if (n.getRps() != null && !n.getRps().isEmpty()) {
57                                 for (final Rps rps : n.getRps()) {
58                                         buffer.writeBytes(serializeObject(rps));
59                                 }
60                         }
61                         if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
62                                 throw new IllegalArgumentException("Message must contain at least one notification object");
63                         } else {
64                                 for (final Notifications not : n.getNotifications()) {
65                                         buffer.writeBytes(serializeObject(not));
66                                 }
67                         }
68                 }
69         }
70
71         @Override
72         public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
73                 if (buffer == null || buffer.length == 0) {
74                         throw new PCEPDeserializerException("Notification message cannot be empty.");
75                 }
76                 final List<Object> objs = parseObjects(buffer);
77
78                 return validate(objs);
79         }
80
81         public Message validate(final List<Object> objects) throws PCEPDeserializerException {
82                 if (objects == null)
83                         throw new IllegalArgumentException("Passed list can't be null.");
84
85                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
86
87                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
88
89                 while (!objects.isEmpty()) {
90                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
91                         try {
92                                 comObj = getValidNotificationComposite(objects);
93                         } catch (final PCEPDocumentedException e) {
94                                 final PcerrMessageBuilder b = new PcerrMessageBuilder();
95                                 b.setErrors(Arrays.asList(new ErrorsBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
96                                                 maping.getFromErrorsEnum(e.getError()).value).build()));
97                                 return new PcerrBuilder().setPcerrMessage(b.build()).build();
98                         }
99
100                         if (comObj == null)
101                                 break;
102
103                         compositeNotifications.add(comObj);
104                 }
105
106                 if (compositeNotifications.isEmpty())
107                         throw new PCEPDeserializerException("Atleast one CompositeNotifiObject is mandatory.");
108
109                 if (!objects.isEmpty())
110                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
111
112                 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
113         }
114
115         private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
116                         final List<Object> objects) throws PCEPDocumentedException {
117                 final List<Rps> requestParameters = Lists.newArrayList();
118                 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();
119                 Object obj;
120
121                 int state = 1;
122                 while (!objects.isEmpty()) {
123                         obj = objects.get(0);
124
125                         if (obj instanceof UnknownObject)
126                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
127
128                         switch (state) {
129                         case 1:
130                                 state = 2;
131                                 if (obj instanceof RpObject) {
132                                         final RpObject rp = (RpObject) obj;
133                                         if (rp.isProcessingRule())
134                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
135                                         requestParameters.add((Rps) rp);
136                                         state = 1;
137                                         break;
138                                 }
139                         case 2:
140                                 if (obj instanceof NotificationObject) {
141                                         final NotificationObject n = (NotificationObject) obj;
142                                         notifications.add((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications) n);
143                                         state = 2;
144                                         break;
145                                 }
146                                 state = 3;
147                         }
148
149                         if (state == 3)
150                                 break;
151
152                         objects.remove(obj);
153                 }
154
155                 if (notifications.isEmpty())
156                         return null;
157
158                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
159                                 notifications).setRps(requestParameters).build();
160         }
161
162         @Override
163         public int getMessageType() {
164                 return this.TYPE;
165         }
166 }