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;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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.protocol.util.ByteBufUtils;
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.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CCloseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
29 import org.opendaylight.yangtools.yang.common.Uint8;
33 * org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose
37 public final class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
39 private static final int CLASS = 15;
40 private static final int TYPE = 1;
43 * lengths of fields in bytes
45 private static final int RESERVED = 2;
46 private static final int FLAGS_F_LENGTH = 1;
48 public PCEPCloseObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
49 super(tlvReg, viTlvReg, CLASS, TYPE);
54 public CClose parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
55 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
56 bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
57 final Uint8 reason = ByteBufUtils.readUint8(bytes);
58 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
59 parseTlvs(tlvsBuilder, bytes.slice());
60 final CCloseBuilder builder = new CCloseBuilder()
61 .setIgnore(header.isIgnore())
62 .setProcessingRule(header.isProcessingRule())
64 .setTlvs(tlvsBuilder.build());
65 return builder.build();
69 public void serializeObject(final Object object, final ByteBuf buffer) {
70 checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.",
72 final CClose obj = (CClose) object;
73 final ByteBuf body = Unpooled.buffer();
74 body.writeZero(RESERVED + FLAGS_F_LENGTH);
75 checkArgument(obj.getReason() != null, "Reason is mandatory.");
76 writeUnsignedByte(obj.getReason(), body);
77 serializeTlvs(obj.getTlvs(), body);
78 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
81 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
85 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
90 protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
91 if (!tlvs.isEmpty()) {
92 builder.setVendorInformationTlv(tlvs);