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.BitArray;
22 import org.opendaylight.protocol.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPath;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.NoPathBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVector;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
34 * Parser for {@link NoPath}
36 public class PCEPNoPathObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38 private static final int CLASS = 3;
39 private static final int TYPE = 1;
42 * lengths of fields in bytes
44 private static final int FLAGS_SIZE = 16;
45 private static final int RESERVED_F_LENGTH = 1;
50 private static final int C_FLAG_OFFSET = 0;
52 public PCEPNoPathObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
53 super(tlvReg, viTlvReg, CLASS, TYPE);
57 public NoPath parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
59 final NoPathBuilder builder = new NoPathBuilder();
60 builder.setIgnore(header.isIgnore());
61 builder.setProcessingRule(header.isProcessingRule());
63 builder.setNatureOfIssue(ByteBufUtils.readUint8(bytes));
64 final BitArray flags = BitArray.valueOf(bytes, FLAGS_SIZE);
65 builder.setUnsatisfiedConstraints(flags.get(C_FLAG_OFFSET));
66 bytes.skipBytes(RESERVED_F_LENGTH);
67 final TlvsBuilder tlvsBuilder = new TlvsBuilder();
68 parseTlvs(tlvsBuilder, bytes.slice());
69 builder.setTlvs(tlvsBuilder.build());
70 return builder.build();
74 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
75 if (tlv instanceof NoPathVector) {
76 builder.setNoPathVector((NoPathVector) tlv);
81 public void serializeObject(final Object object, final ByteBuf buffer) {
82 checkArgument(object instanceof NoPath, "Wrong instance of PCEPObject. Passed %s. Needed NoPathObject.",
84 final NoPath nPObj = (NoPath) object;
85 final ByteBuf body = Unpooled.buffer();
86 checkArgument(nPObj.getNatureOfIssue() != null, "NatureOfIssue is mandatory.");
87 writeUnsignedByte(nPObj.getNatureOfIssue(), body);
88 final BitArray flags = new BitArray(FLAGS_SIZE);
89 flags.set(C_FLAG_OFFSET, nPObj.isUnsatisfiedConstraints());
90 flags.toByteBuf(body);
91 body.writeZero(RESERVED_F_LENGTH);
92 serializeTlvs(nPObj.getTlvs(), body);
93 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
96 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
100 if (tlvs.getNoPathVector() != null) {
101 serializeTlv(tlvs.getNoPathVector(), body);
103 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
107 protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
108 if (!tlvs.isEmpty()) {
109 builder.setVendorInformationTlv(tlvs);