BUG-64 : initial rewrite, MessageRegistry.
[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 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.Close;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
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.close.message.CCloseMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose;
27
28 /**
29  * Parser for {@link CloseMessage}
30  */
31 public class PCEPCloseMessageParser extends AbstractMessageParser {
32
33         public static final int TYPE = 7;
34
35         public PCEPCloseMessageParser(final ObjectRegistry registry) {
36                 super(registry);
37         }
38
39         @Override
40         public void serializeMessage(final Message message, final ByteBuf out) {
41                 if (!(message instanceof CloseMessage)) {
42                         throw new IllegalArgumentException("Wrong instance of Message. Passed instance of " + message.getClass()
43                                         + ". Nedded CloseMessage.");
44                 }
45                 final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
46
47                 if (close.getCClose() == null) {
48                         throw new IllegalArgumentException("Close Object must be present in Close Message.");
49                 }
50                 ByteBuf buffer = Unpooled.buffer();
51                 buffer.writeBytes(serializeObject(close.getCClose()));
52                 MessageUtil.formatMessage(TYPE, buffer, out);
53         }
54
55         @Override
56         protected Close validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
57                 if (objects == null) {
58                         throw new IllegalArgumentException("Passed list can't be null.");
59                 }
60                 if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
61                         throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
62                 }
63                 final Object o = objects.get(0);
64                 final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
65                 objects.remove(0);
66                 if (!objects.isEmpty()) {
67                         throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
68                 }
69                 return new CloseBuilder().setCCloseMessage(msg).build();
70         }
71
72         @Override
73         public int getMessageType() {
74                 return TYPE;
75         }
76 }