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;
12 import java.util.Arrays;
13 import java.util.List;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
17 import org.opendaylight.protocol.pcep.PCEPErrorMapping;
18 import org.opendaylight.protocol.pcep.PCEPErrors;
19 import org.opendaylight.protocol.pcep.UnknownObject;
20 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
21 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NotificationObject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RpObject;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
35 import com.google.common.collect.Lists;
38 * Parser for {@link PcntfMessage}
40 public class PCEPNotificationMessageParser extends AbstractMessageParser {
42 private final int TYPE = 5;
44 public PCEPNotificationMessageParser(final HandlerRegistry registry) {
49 public void serializeMessage(final Message message, final ByteBuf buffer) {
50 if (!(message instanceof PcntfMessage))
51 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
52 + ". Needed PcntfMessage.");
53 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
55 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
56 if (n.getRps() != null && !n.getRps().isEmpty()) {
57 for (final Rps rps : n.getRps()) {
58 buffer.writeBytes(serializeObject(rps));
61 if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
62 throw new IllegalArgumentException("Message must contain at least one notification object");
64 for (final Notifications not : n.getNotifications()) {
65 buffer.writeBytes(serializeObject(not));
72 public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
73 if (buffer == null || buffer.length == 0) {
74 throw new PCEPDeserializerException("Notification message cannot be empty.");
76 final List<Object> objs = parseObjects(buffer);
78 return validate(objs);
81 public Message validate(final List<Object> objects) throws PCEPDeserializerException {
83 throw new IllegalArgumentException("Passed list can't be null.");
85 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
87 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
89 while (!objects.isEmpty()) {
90 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
92 comObj = getValidNotificationComposite(objects);
93 } catch (final PCEPDocumentedException e) {
94 final PcerrMessageBuilder b = new PcerrMessageBuilder();
95 b.setErrors(Arrays.asList(new ErrorsBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
96 maping.getFromErrorsEnum(e.getError()).value).build()));
97 return new PcerrBuilder().setPcerrMessage(b.build()).build();
103 compositeNotifications.add(comObj);
106 if (compositeNotifications.isEmpty())
107 throw new PCEPDeserializerException("Atleast one CompositeNotifiObject is mandatory.");
109 if (!objects.isEmpty())
110 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
112 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
115 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
116 final List<Object> objects) throws PCEPDocumentedException {
117 final List<Rps> requestParameters = Lists.newArrayList();
118 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();
122 while (!objects.isEmpty()) {
123 obj = objects.get(0);
125 if (obj instanceof UnknownObject)
126 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
131 if (obj instanceof RpObject) {
132 final RpObject rp = (RpObject) obj;
133 if (rp.isProcessingRule())
134 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
135 requestParameters.add((Rps) rp);
140 if (obj instanceof NotificationObject) {
141 final NotificationObject n = (NotificationObject) obj;
142 notifications.add((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications) n);
155 if (notifications.isEmpty())
158 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
159 notifications).setRps(requestParameters).build();
163 public int getMessageType() {