ea7ebcae04e6701f39b12b91e41d25014a342230
[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.rev181109.OpenBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.OpenMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.message.OpenMessageBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.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,
39                 "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
40         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.message
41             .OpenMessage open = ((OpenMessage) message).getOpenMessage();
42         Preconditions.checkArgument(open.getOpen() != null, "Open Object must be present in Open Message.");
43         final ByteBuf buffer = Unpooled.buffer();
44         serializeObject(open.getOpen(), buffer);
45         MessageUtil.formatMessage(TYPE, buffer, out);
46     }
47
48     @Override
49     protected org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Open validate(
50             final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
51         Preconditions.checkArgument(objects != null, "Passed list can't be null.");
52
53         if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
54             throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
55         }
56
57         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.open.message
58             .OpenMessage msg = new OpenMessageBuilder().setOpen((Open) objects.get(0)).build();
59
60         objects.remove(0);
61
62         if (!objects.isEmpty()) {
63             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
64         }
65
66         return new OpenBuilder().setOpenMessage(msg).build();
67     }
68 }