Enable restart of CSS modules on blueprint container restart
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPCloseObjectParser.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.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
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.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CCloseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.Tlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.c.close.TlvsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
28
29 /**
30  * Parser for {@link org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CClose PCEPCloseObject}
31  */
32 public class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
33
34     public static final int CLASS = 15;
35
36     public static final int TYPE = 1;
37
38     /*
39      * lengths of fields in bytes
40      */
41     private static final int RESERVED = 2;
42     private static final int FLAGS_F_LENGTH = 1;
43
44     public PCEPCloseObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
45         super(tlvReg, viTlvReg);
46     }
47
48
49     @Override
50     public CClose parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52         final CCloseBuilder builder = new CCloseBuilder();
53         builder.setIgnore(header.isIgnore());
54         builder.setProcessingRule(header.isProcessingRule());
55         bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
56         builder.setReason(bytes.readUnsignedByte());
57         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
58         parseTlvs(tlvsBuilder, bytes.slice());
59         builder.setTlvs(tlvsBuilder.build());
60         return builder.build();
61     }
62
63     @Override
64     public void serializeObject(final Object object, final ByteBuf buffer) {
65         Preconditions.checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.", object.getClass());
66         final CClose obj = (CClose) object;
67         final ByteBuf body = Unpooled.buffer();
68         body.writeZero(RESERVED + FLAGS_F_LENGTH);
69         Preconditions.checkArgument(obj.getReason() != null, "Reason is mandatory.");
70         writeUnsignedByte(obj.getReason(), body);
71         serializeTlvs(obj.getTlvs(), body);
72         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
73     }
74
75     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
76         if (tlvs == null) {
77             return;
78         }
79         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
80     }
81
82
83     @Override
84     protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
85         if (!tlvs.isEmpty()) {
86             builder.setVendorInformationTlv(tlvs);
87         }
88     }
89 }