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