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.ObjectHandlerRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcerrBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcntfBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcntfMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.notification.object.CNotification;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessageBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.NotificationsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Rps;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.RpsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
37 import com.google.common.collect.Lists;
40 * Parser for {@link PcntfMessage}
42 public class PCEPNotificationMessageParser extends AbstractMessageParser {
44 public static final int TYPE = 5;
46 public PCEPNotificationMessageParser(final ObjectHandlerRegistry registry) {
51 public void serializeMessage(final Message message, final ByteBuf buffer) {
52 if (!(message instanceof PcntfMessage)) {
53 throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
54 + ". Needed PcntfMessage.");
56 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.PcntfMessage msg = ((PcntfMessage) message).getPcntfMessage();
58 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications n : msg.getNotifications()) {
59 if (n.getRps() != null && !n.getRps().isEmpty()) {
60 for (final Rps rps : n.getRps()) {
61 buffer.writeBytes(serializeObject(rps.getRp()));
64 if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
65 throw new IllegalArgumentException("Message must contain at least one notification object");
67 for (final Notifications not : n.getNotifications()) {
68 buffer.writeBytes(serializeObject(not.getCNotification()));
75 protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
76 if (objects == null) {
77 throw new IllegalArgumentException("Passed list can't be null.");
80 if (objects.isEmpty()) {
81 throw new PCEPDeserializerException("Notification message cannot be empty.");
84 final PCEPErrorMapping maping = PCEPErrorMapping.getInstance();
86 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications> compositeNotifications = Lists.newArrayList();
88 while (!objects.isEmpty()) {
89 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications comObj;
91 comObj = getValidNotificationComposite(objects);
92 } catch (final PCEPDocumentedException e) {
93 final PcerrMessageBuilder b = new PcerrMessageBuilder();
94 b.setErrors(Arrays.asList(new ErrorsBuilder().setErrorObject(
95 new ErrorObjectBuilder().setType(maping.getFromErrorsEnum(e.getError()).type).setValue(
96 maping.getFromErrorsEnum(e.getError()).value).build()).build()));
97 return new PcerrBuilder().setPcerrMessage(b.build()).build();
100 if (comObj == null) {
104 compositeNotifications.add(comObj);
107 if (compositeNotifications.isEmpty()) {
108 throw new PCEPDeserializerException("Atleast one Notifications is mandatory.");
111 if (!objects.isEmpty()) {
112 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
115 return new PcntfBuilder().setPcntfMessage(new PcntfMessageBuilder().setNotifications(compositeNotifications).build()).build();
118 private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.Notifications getValidNotificationComposite(
119 final List<Object> objects) throws PCEPDocumentedException {
120 final List<Rps> requestParameters = Lists.newArrayList();
121 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();
124 State state = State.Init;
125 while (!objects.isEmpty() && !state.equals(State.End)) {
126 obj = objects.get(0);
128 if (obj instanceof UnknownObject) {
129 throw new PCEPDocumentedException("Unknown object", ((UnknownObject) obj).getError());
135 if (obj instanceof Rp) {
136 final Rp rp = (Rp) obj;
137 if (rp.isProcessingRule()) {
138 throw new PCEPDocumentedException("Invalid setting of P flag.", PCEPErrors.P_FLAG_NOT_SET);
140 requestParameters.add(new RpsBuilder().setRp(rp).build());
145 state = State.NotificationIn;
146 if (obj instanceof CNotification) {
147 final CNotification n = (CNotification) obj;
148 notifications.add(new NotificationsBuilder().setCNotification(n).build());
158 if (!state.equals(State.End)) {
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 Init, RpIn, NotificationIn, End
176 public int getMessageType() {