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