fc084e53d9db0f31e8ffc11199bf86326af86476
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.message;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
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;
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 ByteBuf buffer = Unpooled.buffer();
48         for (final Notifications n : ((PcntfMessage) message).getPcntfMessage().getNotifications()) {
49             if (n.getRps() != null) {
50                 for (final Rps rps : n.getRps()) {
51                     serializeObject(rps.getRp(), buffer);
52                 }
53             }
54             Preconditions.checkArgument(n.getNotifications() != null && !n.getNotifications().isEmpty(), "Message must contain at least one notification object");
55             for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications not : n.getNotifications()) {
56                 serializeObject(not.getCNotification(), buffer);
57             }
58         }
59         MessageUtil.formatMessage(TYPE, buffer, out);
60     }
61
62     @Override
63     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
64         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
65         if (objects.isEmpty()) {
66             throw new PCEPDeserializerException("Notification message cannot be empty.");
67         }
68
69         final List<Notifications> compositeNotifications = new ArrayList<>();
70
71         while (!objects.isEmpty()) {
72             final Notifications comObj = getValidNotificationComposite(objects, errors);
73             if (comObj == null) {
74                 break;
75             }
76             compositeNotifications.add(comObj);
77         }
78         if (compositeNotifications.isEmpty()) {
79             throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
80         }
81         if (!objects.isEmpty()) {
82             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
83         }
84         return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
85     }
86
87     private static Notifications getValidNotificationComposite(final List<Object> objects, final List<Message> errors) {
88         final List<Rps> requestParameters = new ArrayList<>();
89         final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> notifications = new ArrayList<>();
90         Object obj;
91
92         State state = State.INIT;
93         while (!objects.isEmpty() && !state.equals(State.END)) {
94             obj = objects.get(0);
95             if ((state = insertObject(state, obj, errors, requestParameters, notifications)) == null) {
96                 return null;
97             }
98             if (!state.equals(State.END)) {
99                 objects.remove(0);
100             }
101         }
102
103         if (notifications.isEmpty()) {
104             return null;
105         }
106
107         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
108                 notifications).setRps(requestParameters).build();
109     }
110
111     private static State insertObject(final State state, final Object obj, final List<Message> errors, final List<Rps> requestParameters, final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> notifications) {
112         switch (state) {
113         case INIT:
114             if (obj instanceof Rp) {
115                 final Rp rp = (Rp) obj;
116                 if (rp.isProcessingRule()) {
117                     errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.<Rp>absent()));
118                     return null;
119                 }
120                 requestParameters.add(new RpsBuilder().setRp(rp).build());
121                 return State.INIT;
122             }
123         case RP_IN:
124             if (obj instanceof CNotification) {
125                 final CNotification n = (CNotification) obj;
126                 notifications.add(new NotificationsBuilder().setCNotification(n).build());
127                 return State.RP_IN;
128             }
129         case NOTIFICATION_IN:
130         case END:
131             return State.END;
132         default:
133             return state;
134         }
135     }
136
137     private enum State {
138         INIT, RP_IN, NOTIFICATION_IN, END
139     }
140 }