BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / message / PCEPCloseMessageParser.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
12 import java.util.List;
13
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
16 import org.opendaylight.protocol.pcep.impl.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectHandlerRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Close;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.c.close.message.CClose;
26
27 /**
28  * Parser for {@link org.opendaylight.protocol.pcep.message.PCEPCloseMessage PCEPCloseMessage}
29  */
30 public class PCEPCloseMessageParser extends AbstractMessageParser {
31
32         public static final int TYPE = 7;
33
34         public PCEPCloseMessageParser(final ObjectHandlerRegistry registry) {
35                 super(registry);
36         }
37
38         @Override
39         public void serializeMessage(final Message message, final ByteBuf buffer) {
40                 if (!(message instanceof CloseMessage)) {
41                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
42                                         + ". Nedded CloseMessage.");
43                 }
44                 final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
45
46                 if (close.getCClose() == null) {
47                         throw new IllegalArgumentException("Close Object must be present in Close Message.");
48                 }
49                 buffer.writeBytes(serializeObject(close.getCClose()));
50         }
51
52         @Override
53         public CloseMessage parseMessage(final byte[] buffer) throws PCEPDeserializerException, PCEPDocumentedException {
54                 if (buffer == null || buffer.length == 0) {
55                         throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
56                 }
57                 final List<Object> objs = parseObjects(buffer);
58
59                 return validate(objs);
60         }
61
62         private Close validate(final List<Object> objects) throws PCEPDeserializerException {
63                 if (objects == null) {
64                         throw new IllegalArgumentException("Passed list can't be null.");
65                 }
66
67                 if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
68                         throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
69                 }
70
71                 final Object o = objects.get(0);
72                 final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
73                 objects.remove(0);
74
75                 if (!objects.isEmpty()) {
76                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
77                 }
78
79                 return new CloseBuilder().setCCloseMessage(msg).build();
80         }
81
82         @Override
83         public int getMessageType() {
84                 return TYPE;
85         }
86 }