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.impl.AbstractMessageParser;
21 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
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 public static final int TYPE = 5;
44 public PCEPNotificationMessageParser(final ObjectHandlerRegistry 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.");
54 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
56 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
57 if (n.getRps() != null && !n.getRps().isEmpty()) {
58 for (final Rps rps : n.getRps()) {
59 buffer.writeBytes(serializeObject(rps));
62 if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
63 throw new IllegalArgumentException("Message must contain at least one notification object");
65 for (final Notifications not : n.getNotifications()) {
66 buffer.writeBytes(serializeObject(not));
73 public Message parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
74 if (buffer == null || buffer.length == 0) {
75 throw new PCEPDeserializerException("Notification message cannot be empty.");
77 final List<Object> objs = parseObjects(buffer);
79 return validate(objs);
82 public Message validate(final List<Object> objects) throws PCEPDeserializerException {
83 if (objects == null) {
84 throw new IllegalArgumentException("Passed list can't be null.");
87 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
89 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
91 while (!objects.isEmpty()) {
92 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
94 comObj = getValidNotificationComposite(objects);
95 } catch (final PCEPDocumentedException e) {
96 final PcerrMessageBuilder b = new PcerrMessageBuilder();
97 b.setErrors(Arrays.asList(new ErrorsBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
98 maping.getFromErrorsEnum(e.getError()).value).build()));
99 return new PcerrBuilder().setPcerrMessage(b.build()).build();
102 if (comObj == null) {
106 compositeNotifications.add(comObj);
109 if (compositeNotifications.isEmpty()) {
110 throw new PCEPDeserializerException("Atleast one CompositeNotifiObject is mandatory.");
113 if (!objects.isEmpty()) {
114 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
117 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
120 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
121 final List<Object> objects) throws PCEPDocumentedException {
122 final List<Rps> requestParameters = Lists.newArrayList();
123 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();
127 while (!objects.isEmpty()) {
128 obj = objects.get(0);
130 if (obj instanceof UnknownObject) {
131 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
137 if (obj instanceof RpObject) {
138 final RpObject rp = (RpObject) obj;
139 if (rp.isProcessingRule()) {
140 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
142 requestParameters.add((Rps) rp);
147 if (obj instanceof NotificationObject) {
148 final NotificationObject n = (NotificationObject) obj;
149 notifications.add((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications) n);
163 if (notifications.isEmpty()) {
167 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.NotificationsBuilder().setNotifications(
168 notifications).setRps(requestParameters).build();
172 public int getMessageType() {