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.List;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.open.message.Open;
25 * Parser for {@link OpenMessage}
27 public class PCEPOpenMessageParser extends AbstractMessageParser {
29 private final int TYPE = 1;
31 public PCEPOpenMessageParser(final HandlerRegistry registry) {
36 public void serializeMessage(final Message message, final ByteBuf buffer) {
37 if (!(message instanceof OpenMessage))
38 throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Nedded OpenMessage.");
39 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
41 if (open.getOpen() == null) {
42 throw new IllegalArgumentException("Open Object must be present in Open Message.");
45 buffer.writeBytes(serializeObject(open.getOpen()));
49 public OpenMessage parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
50 if (buffer == null || buffer.length == 0) {
51 throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
53 final List<Object> objs = parseObjects(buffer);
55 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder().setOpenMessage(
56 validate(objs)).build();
59 private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage validate(
60 final List<Object> objects) throws PCEPDeserializerException {
62 throw new IllegalArgumentException("Passed list can't be null.");
64 if (objects.isEmpty() || !(objects.get(0) instanceof Open))
65 throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
67 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
68 (Open) objects.get(0)).build();
72 if (!objects.isEmpty())
73 throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
79 public int getMessageType() {