248869dcf91814f154c28a0d45854ed21857e7b0
[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         public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
76                 if (buffer == null || buffer.length == 0) {
77                         throw new PCEPDeserializerException("Notification message cannot be empty.");
78                 }
79                 final List<Object> objs = parseObjects(buffer);
80
81                 return validate(objs);
82         }
83
84         public Message validate(final List<Object> objects) throws PCEPDeserializerException {
85                 if (objects == null) {
86                         throw new IllegalArgumentException("Passed list can't be null.");
87                 }
88
89                 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
90
91                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
92
93                 while (!objects.isEmpty()) {
94                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
95                         try {
96                                 comObj = getValidNotificationComposite(objects);
97                         } catch (final PCEPDocumentedException e) {
98                                 final PcerrMessageBuilder b = new PcerrMessageBuilder();
99                                 b.setErrors(Arrays.asList(new ErrorsBuilder().setErrorObject(
100                                                 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
101                                                                 maping.getFromErrorsEnum(e.getError()).value).build()).build()));
102                                 return new PcerrBuilder().setPcerrMessage(b.build()).build();
103                         }
104
105                         if (comObj == null) {
106                                 break;
107                         }
108
109                         compositeNotifications.add(comObj);
110                 }
111
112                 if (compositeNotifications.isEmpty()) {
113                         throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
114                 }
115
116                 if (!objects.isEmpty()) {
117                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
118                 }
119
120                 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
121         }
122
123         private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
124                         final List<Object> objects) throws PCEPDocumentedException {
125                 final List<Rps> requestParameters = Lists.newArrayList();
126                 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();
127                 Object obj;
128
129                 State state = State.Init;
130                 while (!objects.isEmpty() && !state.equals(State.End)) {
131                         obj = objects.get(0);
132
133                         if (obj instanceof UnknownObject) {
134                                 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
135                         }
136
137                         switch (state) {
138                         case Init:
139                                 state = State.RpIn;
140                                 if (obj instanceof Rp) {
141                                         final Rp rp = (Rp) obj;
142                                         if (rp.isProcessingRule()) {
143                                                 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
144                                         }
145                                         requestParameters.add(new RpsBuilder().setRp(rp).build());
146                                         state = State.Init;
147                                         break;
148                                 }
149                         case RpIn:
150                                 state = State.NotificationIn;
151                                 if (obj instanceof CNotification) {
152                                         final CNotification n = (CNotification) obj;
153                                         notifications.add(new NotificationsBuilder().setCNotification(n).build());
154                                         state = State.RpIn;
155                                         break;
156                                 }
157                         case NotificationIn:
158                                 state = State.End;
159                                 break;
160                         case End:
161                                 break;
162                         }
163                         if (!state.equals(State.End)) {
164                                 objects.remove(0);
165                         }
166                 }
167
168                 if (notifications.isEmpty()) {
169                         return null;
170                 }
171
172                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
173                                 notifications).setRps(requestParameters).build();
174         }
175
176         private enum State {
177                 Init, RpIn, NotificationIn, End
178         }
179
180         @Override
181         public int getMessageType() {
182                 return TYPE;
183         }
184 }