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