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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
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.rev131007.PcntfBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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, "Wrong instance of Message. Passed instance of %s. Need PcntfMessage.", message.getClass());
47 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
49 ByteBuf buffer = Unpooled.buffer();
50 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
51 if (n.getRps() != null && !n.getRps().isEmpty()) {
52 for (final Rps rps : n.getRps()) {
53 serializeObject(rps.getRp(), buffer);
56 if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
57 throw new IllegalArgumentException("Message must contain at least one notification object");
59 for (final Notifications not : n.getNotifications()) {
60 serializeObject(not.getCNotification(), buffer);
64 MessageUtil.formatMessage(TYPE, buffer, out);
68 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
69 if (objects == null) {
70 throw new IllegalArgumentException("Passed list can't be null.");
72 if (objects.isEmpty()) {
73 throw new PCEPDeserializerException("Notification message cannot be empty.");
76 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
78 while (!objects.isEmpty()) {
79 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
80 comObj = getValidNotificationComposite(objects, errors);
85 compositeNotifications.add(comObj);
87 if (compositeNotifications.isEmpty()) {
88 throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
90 if (!objects.isEmpty()) {
91 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
93 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
96 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
97 final List<Object> objects, final List<Message> errors) {
98 final List<Rps> requestParameters = Lists.newArrayList();
99 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();
102 State state = State.Init;
103 while (!objects.isEmpty() && !state.equals(State.End)) {
104 obj = objects.get(0);
108 if (obj instanceof Rp) {
109 final Rp rp = (Rp) obj;
110 if (rp.isProcessingRule()) {
111 errors.add(createErrorMsg(PCEPErrors.P_FLAG_NOT_SET, Optional.<Rp>absent()));
114 requestParameters.add(new RpsBuilder().setRp(rp).build());
119 state = State.NotificationIn;
120 if (obj instanceof CNotification) {
121 final CNotification n = (CNotification) obj;
122 notifications.add(new NotificationsBuilder().setCNotification(n).build());
132 if (!state.equals(State.End)) {
137 if (notifications.isEmpty()) {
141 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
142 notifications).setRps(requestParameters).build();
146 Init, RpIn, NotificationIn, End