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 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;
34 * Parser for {@link PcntfMessage}.
36 public class PCEPNotificationMessageParser extends AbstractMessageParser {
38 public static final int TYPE = 5;
40 public PCEPNotificationMessageParser(final ObjectRegistry registry) {
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);
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);
62 MessageUtil.formatMessage(TYPE, buffer, out);
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.");
73 final List<Notifications> compositeNotifications = new ArrayList<>();
75 while (!objects.isEmpty()) {
76 final Notifications comObj = getValidNotificationComposite(objects, errors);
80 compositeNotifications.add(comObj);
82 if (compositeNotifications.isEmpty()) {
83 throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
85 if (!objects.isEmpty()) {
86 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
88 return new PcntfBuilder()
89 .setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
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<>();
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) {
104 if (!state.equals(State.END)) {
109 if (notifications.isEmpty()) {
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();
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) {
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()));
130 requestParameters.add(new RpsBuilder().setRp(rp).build());
135 if (obj instanceof CNotification) {
136 final CNotification n = (CNotification) obj;
137 notifications.add(new NotificationsBuilder().setCNotification(n).build());
141 case NOTIFICATION_IN:
150 INIT, RP_IN, NOTIFICATION_IN, END