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.protocol.util.BitArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.Lspa;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.LspaBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.lspa.Tlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.lspa.object.lspa.TlvsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.AttributeFilter;
29 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
32 * Parser for {@link Lspa}.
34 public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
35 private static final int CLASS = 9;
36 private static final int TYPE = 1;
39 * lengths of fields in bytes
41 private static final int FLAGS_SIZE = 8;
43 * offsets of flags inside flags field in bits
45 private static final int L_FLAG_OFFSET = 7;
46 private static final int RESERVED = 1;
48 public PCEPLspaObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
49 super(tlvReg, viTlvReg, CLASS, TYPE);
53 public Lspa parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
54 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
55 final LspaBuilder builder = new LspaBuilder()
56 .setIgnore(header.isIgnore())
57 .setProcessingRule(header.isProcessingRule())
58 .setExcludeAny(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
59 .setIncludeAll(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
60 .setIncludeAny(new AttributeFilter(ByteBufUtils.readUint32(bytes)))
61 .setSetupPriority(ByteBufUtils.readUint8(bytes))
62 .setHoldPriority(ByteBufUtils.readUint8(bytes));
64 final BitArray flags = BitArray.valueOf(bytes.readByte());
65 builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
66 final TlvsBuilder tbuilder = new TlvsBuilder();
67 bytes.skipBytes(RESERVED);
68 parseTlvs(tbuilder, bytes.slice());
69 return builder.setTlvs(tbuilder.build()).build();
73 public void serializeObject(final Object object, final ByteBuf buffer) {
74 checkArgument(object instanceof Lspa,
75 "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
76 final Lspa lspaObj = (Lspa) object;
77 final ByteBuf body = Unpooled.buffer();
78 writeAttributeFilter(lspaObj.getExcludeAny(), body);
79 writeAttributeFilter(lspaObj.getIncludeAny(), body);
80 writeAttributeFilter(lspaObj.getIncludeAll(), body);
81 ByteBufUtils.writeOrZero(body, lspaObj.getSetupPriority());
82 ByteBufUtils.writeOrZero(body, lspaObj.getHoldPriority());
83 final BitArray flags = new BitArray(FLAGS_SIZE);
84 flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
85 flags.toByteBuf(body);
86 body.writeZero(RESERVED);
87 serializeTlvs(lspaObj.getTlvs(), body);
88 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
91 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
93 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
97 private static void writeAttributeFilter(final AttributeFilter attributeFilter, final ByteBuf body) {
98 if (attributeFilter != null) {
99 ByteBufUtils.write(body, attributeFilter.getValue());
106 protected final void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
107 if (!tlvs.isEmpty()) {
108 builder.setVendorInformationTlv(tlvs);