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.impl.message;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
16 import org.opendaylight.protocol.pcep.spi.MessageUtil;
17 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
32 import com.google.common.collect.Lists;
35 * Parser for {@link PcntfMessage}
37 public class PCEPNotificationMessageParser extends AbstractMessageParser {
39 public static final int TYPE = 5;
41 public PCEPNotificationMessageParser(final ObjectRegistry registry) {
46 public void serializeMessage(final Message message, final ByteBuf out) {
47 if (!(message instanceof PcntfMessage)) {
48 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
49 + ". Needed PcntfMessage.");
51 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
53 ByteBuf buffer = Unpooled.buffer();
54 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
55 if (n.getRps() != null && !n.getRps().isEmpty()) {
56 for (final Rps rps : n.getRps()) {
57 buffer.writeBytes(serializeObject(rps.getRp()));
60 if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
61 throw new IllegalArgumentException("Message must contain at least one notification object");
63 for (final Notifications not : n.getNotifications()) {
64 buffer.writeBytes(serializeObject(not.getCNotification()));
68 MessageUtil.formatMessage(TYPE, buffer, out);
72 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
73 if (objects == null) {
74 throw new IllegalArgumentException("Passed list can't be null.");
76 if (objects.isEmpty()) {
77 throw new PCEPDeserializerException("Notification message cannot be empty.");
80 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
82 while (!objects.isEmpty()) {
83 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
84 comObj = getValidNotificationComposite(objects, errors);
89 compositeNotifications.add(comObj);
91 if (compositeNotifications.isEmpty()) {
92 throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
94 if (!objects.isEmpty()) {
95 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
97 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
100 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
101 final List<Object> objects, final List<Message> errors) {
102 final List<Rps> requestParameters = Lists.newArrayList();
103 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications> notifications = Lists.newArrayList();
106 State state = State.Init;
107 while (!objects.isEmpty() && !state.equals(State.End)) {
108 obj = objects.get(0);
112 if (obj instanceof Rp) {
113 final Rp rp = (Rp) obj;
114 if (rp.isProcessingRule()) {
115 errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET));
118 requestParameters.add(new RpsBuilder().setRp(rp).build());
123 state = State.NotificationIn;
124 if (obj instanceof CNotification) {
125 final CNotification n = (CNotification) obj;
126 notifications.add(new NotificationsBuilder().setCNotification(n).build());
136 if (!state.equals(State.End)) {
141 if (notifications.isEmpty()) {
145 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
146 notifications).setRps(requestParameters).build();
150 Init, RpIn, NotificationIn, End
154 public int getMessageType() {