BUG-64 : initial rewrite, MessageRegistry.
[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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12
13 import java.util.List;
14
15 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
16 import org.opendaylight.protocol.pcep.spi.MessageUtil;
17 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
25
26 /**
27  * Parser for {@link OpenMessage}
28  */
29 public class PCEPOpenMessageParser extends AbstractMessageParser {
30
31         public static final int TYPE = 1;
32
33         public PCEPOpenMessageParser(final ObjectRegistry registry) {
34                 super(registry);
35         }
36
37         @Override
38         public void serializeMessage(final Message message, final ByteBuf out) {
39                 if (!(message instanceof OpenMessage)) {
40                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance " + message.getClass() + ". Needed OpenMessage.");
41                 }
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                 buffer.writeBytes(serializeObject(open.getOpen()));
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(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
54                 if (objects == null) {
55                         throw new IllegalArgumentException("Passed list can't be null.");
56                 }
57
58                 if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
59                         throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
60                 }
61
62                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage msg = new OpenMessageBuilder().setOpen(
63                                 (Open) objects.get(0)).build();
64
65                 objects.remove(0);
66
67                 if (!objects.isEmpty()) {
68                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
69                 }
70
71                 return new OpenBuilder().setOpenMessage(msg).build();
72         }
73
74         @Override
75         public int getMessageType() {
76                 return TYPE;
77         }
78 }