Merge "Fixed vendor specific tlv's payload parser/serializer"
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPOpenMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.message;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import java.util.List;
16
17 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
18 import org.opendaylight.protocol.pcep.spi.MessageUtil;
19 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
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.OpenMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
27
28 /**
29  * Parser for {@link OpenMessage}
30  */
31 public class PCEPOpenMessageParser extends AbstractMessageParser {
32
33     public static final int TYPE = 1;
34
35     public PCEPOpenMessageParser(final ObjectRegistry registry) {
36         super(registry);
37     }
38
39     @Override
40     public void serializeMessage(final Message message, final ByteBuf out) {
41         Preconditions.checkArgument(message instanceof OpenMessage, "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
42         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
43
44         if (open.getOpen() == null) {
45             throw new IllegalArgumentException("Open Object must be present in Open Message.");
46         }
47         ByteBuf buffer = Unpooled.buffer();
48         serializeObject(open.getOpen(), buffer);
49         MessageUtil.formatMessage(TYPE, buffer, out);
50     }
51
52     @Override
53     protected org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open validate(
54             final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
55         if (objects == null) {
56             throw new IllegalArgumentException("Passed list can't be null.");
57         }
58
59         if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
60             throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
61         }
62
63         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
64                 (Open) objects.get(0)).build();
65
66         objects.remove(0);
67
68         if (!objects.isEmpty()) {
69             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
70         }
71
72         return new OpenBuilder().setOpenMessage(msg).build();
73     }
74 }