2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CCloseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.TlvsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
27 import org.opendaylight.yangtools.yang.common.Uint8;
28 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
32 * org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose
36 public final class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
37 private static final int CLASS = 15;
38 private static final int TYPE = 1;
41 * lengths of fields in bytes
43 private static final int RESERVED = 2;
44 private static final int FLAGS_F_LENGTH = 1;
46 public PCEPCloseObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
47 super(tlvReg, viTlvReg, CLASS, TYPE);
51 public CClose parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
52 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
53 bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
54 final Uint8 reason = ByteBufUtils.readUint8(bytes);
55 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
56 parseTlvs(tlvsBuilder, bytes.slice());
57 return new CCloseBuilder()
58 .setIgnore(header.getIgnore())
59 .setProcessingRule(header.getProcessingRule())
61 .setTlvs(tlvsBuilder.build())
66 public void serializeObject(final Object object, final ByteBuf buffer) {
67 checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.",
69 final CClose obj = (CClose) object;
70 final ByteBuf body = Unpooled.buffer();
71 body.writeZero(RESERVED + FLAGS_F_LENGTH);
72 ByteBufUtils.writeMandatory(body, obj.getReason(), "Reason");
73 serializeTlvs(obj.getTlvs(), body);
74 ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
77 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
79 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
84 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
85 if (!tlvs.isEmpty()) {
86 builder.setVendorInformationTlv(tlvs);