c0559ec264672165f90f9a4c89969abf48901f4e
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.message;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import java.util.Queue;
16 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
17 import org.opendaylight.protocol.pcep.spi.MessageUtil;
18 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Close;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.CloseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.CloseMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.message.CCloseMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.message.CCloseMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose;
28
29 /**
30  * Parser for {@link CloseMessage}.
31  */
32 public class PCEPCloseMessageParser extends AbstractMessageParser {
33
34     public static final int TYPE = 7;
35
36     public PCEPCloseMessageParser(final ObjectRegistry registry) {
37         super(registry);
38     }
39
40     @Override
41     public void serializeMessage(final Message message, final ByteBuf out) {
42         checkArgument(message instanceof CloseMessage,
43                 "Wrong instance of Message. Passed instance of %s. Need CloseMessage.", message.getClass());
44         final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
45         checkArgument(close.getCClose() != null, "Close Object must be present in Close Message.");
46         final ByteBuf buffer = Unpooled.buffer();
47         serializeObject(close.getCClose(), buffer);
48         MessageUtil.formatMessage(TYPE, buffer, out);
49     }
50
51     @Override
52     protected Close validate(final Queue<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
53         checkArgument(objects != null, "Passed list can't be null.");
54         final Object o = objects.poll();
55         if (!(o instanceof CClose)) {
56             throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
57         }
58         final CCloseMessage msg = new CCloseMessageBuilder().setCClose((CClose) o).build();
59         if (!objects.isEmpty()) {
60             throw new PCEPDeserializerException("Unprocessed Objects: " + objects);
61         }
62         return new CloseBuilder().setCCloseMessage(msg).build();
63     }
64 }