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