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