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