2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.message;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
36 * Parser for {@link PcntfMessage}.
38 public class PCEPNotificationMessageParser extends AbstractMessageParser {
40 public static final int TYPE = 5;
42 public PCEPNotificationMessageParser(final ObjectRegistry registry) {
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);
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));
60 MessageUtil.formatMessage(TYPE, buffer, out);
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.");
71 final var compositeNotifications = new ArrayList<Notifications>();
72 while (!objects.isEmpty()) {
73 final Notifications comObj = getValidNotificationComposite(objects, errors);
77 compositeNotifications.add(comObj);
79 if (compositeNotifications.isEmpty()) {
80 throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
82 if (!objects.isEmpty()) {
83 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
85 return new PcntfBuilder()
86 .setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build())
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<>();
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) {
101 if (state == State.END) {
108 if (notifications.isEmpty()) {
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();
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) {
123 if (obj instanceof Rp rp) {
124 if (rp.getProcessingRule()) {
125 errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.empty()));
128 requestParameters.add(new RpsBuilder().setRp(rp).build());
133 if (obj instanceof CNotification n) {
134 notifications.add(new NotificationsBuilder().setCNotification(n).build());
138 case NOTIFICATION_IN:
147 INIT, RP_IN, NOTIFICATION_IN, END