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