BUG-64 : initial rewrite, MessageRegistry.
[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 import io.netty.buffer.Unpooled;
12
13 import java.util.List;
14
15 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
16 import org.opendaylight.protocol.pcep.spi.MessageUtil;
17 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
31
32 import com.google.common.collect.Lists;
33
34 /**
35  * Parser for {@link PcntfMessage}
36  */
37 public class PCEPNotificationMessageParser extends AbstractMessageParser {
38
39         public static final int TYPE = 5;
40
41         public PCEPNotificationMessageParser(final ObjectRegistry registry) {
42                 super(registry);
43         }
44
45         @Override
46         public void serializeMessage(final Message message, final ByteBuf out) {
47                 if (!(message instanceof PcntfMessage)) {
48                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
49                                         + ". Needed PcntfMessage.");
50                 }
51                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
52
53                 ByteBuf buffer = Unpooled.buffer();
54                 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
55                         if (n.getRps() != null && !n.getRps().isEmpty()) {
56                                 for (final Rps rps : n.getRps()) {
57                                         buffer.writeBytes(serializeObject(rps.getRp()));
58                                 }
59                         }
60                         if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
61                                 throw new IllegalArgumentException("Message must contain at least one notification object");
62                         } else {
63                                 for (final Notifications not : n.getNotifications()) {
64                                         buffer.writeBytes(serializeObject(not.getCNotification()));
65                                 }
66                         }
67                 }
68                 MessageUtil.formatMessage(TYPE, buffer, out);
69         }
70
71         @Override
72         protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
73                 if (objects == null) {
74                         throw new IllegalArgumentException("Passed list can't be null.");
75                 }
76                 if (objects.isEmpty()) {
77                         throw new PCEPDeserializerException("Notification message cannot be empty.");
78                 }
79
80                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
81
82                 while (!objects.isEmpty()) {
83                         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
84                         comObj = getValidNotificationComposite(objects, errors);
85
86                         if (comObj == null) {
87                                 break;
88                         }
89                         compositeNotifications.add(comObj);
90                 }
91                 if (compositeNotifications.isEmpty()) {
92                         throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
93                 }
94                 if (!objects.isEmpty()) {
95                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
96                 }
97                 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
98         }
99
100         private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
101                         final List<Object> objects, final List<Message> errors) {
102                 final List<Rps> requestParameters = Lists.newArrayList();
103                 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();
104                 Object obj;
105
106                 State state = State.Init;
107                 while (!objects.isEmpty() && !state.equals(State.End)) {
108                         obj = objects.get(0);
109                         switch (state) {
110                         case Init:
111                                 state = State.RpIn;
112                                 if (obj instanceof Rp) {
113                                         final Rp rp = (Rp) obj;
114                                         if (rp.isProcessingRule()) {
115                                                 errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET));
116                                                 return null;
117                                         }
118                                         requestParameters.add(new RpsBuilder().setRp(rp).build());
119                                         state = State.Init;
120                                         break;
121                                 }
122                         case RpIn:
123                                 state = State.NotificationIn;
124                                 if (obj instanceof CNotification) {
125                                         final CNotification n = (CNotification) obj;
126                                         notifications.add(new NotificationsBuilder().setCNotification(n).build());
127                                         state = State.RpIn;
128                                         break;
129                                 }
130                         case NotificationIn:
131                                 state = State.End;
132                                 break;
133                         case End:
134                                 break;
135                         }
136                         if (!state.equals(State.End)) {
137                                 objects.remove(0);
138                         }
139                 }
140
141                 if (notifications.isEmpty()) {
142                         return null;
143                 }
144
145                 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
146                                 notifications).setRps(requestParameters).build();
147         }
148
149         private enum State {
150                 Init, RpIn, NotificationIn, End
151         }
152
153         @Override
154         public int getMessageType() {
155                 return TYPE;
156         }
157 }