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