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.impl.object;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
16 import java.util.BitSet;
18 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.LspaBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.Tlvs;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AttributeFilter;
32 * Parser for {@link Lspa}
34 public class PCEPLspaObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36 public static final int CLASS = 9;
38 public static final int TYPE = 1;
41 * lengths of fields in bytes
43 private static final int FLAGS_F_LENGTH = 1;
46 * offsets of flags inside flags field in bits
48 private static final int L_FLAG_OFFSET = 7;
50 private static final int RESERVED = 1;
52 public PCEPLspaObjectParser(final TlvRegistry tlvReg) {
57 public Lspa parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
59 final LspaBuilder builder = new LspaBuilder();
60 builder.setIgnore(header.isIgnore());
61 builder.setProcessingRule(header.isProcessingRule());
63 builder.setExcludeAny(new AttributeFilter(bytes.readUnsignedInt()));
64 builder.setIncludeAll(new AttributeFilter(bytes.readUnsignedInt()));
65 builder.setIncludeAny(new AttributeFilter(bytes.readUnsignedInt()));
66 builder.setSetupPriority((short) UnsignedBytes.toInt(bytes.readByte()));
67 builder.setHoldPriority((short) UnsignedBytes.toInt(bytes.readByte()));
69 final BitSet flags = ByteArray.bytesToBitSet(new byte[] { bytes.readByte() });
70 builder.setLocalProtectionDesired(flags.get(L_FLAG_OFFSET));
71 final TlvsBuilder tbuilder = new TlvsBuilder();
72 bytes.readerIndex(bytes.readerIndex() + RESERVED);
73 parseTlvs(tbuilder, bytes.slice());
74 builder.setTlvs(tbuilder.build());
75 return builder.build();
79 public void serializeObject(final Object object, final ByteBuf buffer) {
80 Preconditions.checkArgument(object instanceof Lspa, "Wrong instance of PCEPObject. Passed %s. Needed LspaObject.", object.getClass());
81 final Lspa lspaObj = (Lspa) object;
82 final ByteBuf body = Unpooled.buffer();
83 body.writeInt(lspaObj.getExcludeAny().getValue().intValue());
84 body.writeInt(lspaObj.getIncludeAny().getValue().intValue());
85 body.writeInt(lspaObj.getIncludeAll().getValue().intValue());
86 body.writeByte(lspaObj.getSetupPriority());
87 body.writeByte(lspaObj.getHoldPriority());
88 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
89 if (lspaObj.isLocalProtectionDesired() != null) {
90 flags.set(L_FLAG_OFFSET, lspaObj.isLocalProtectionDesired());
92 body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
93 body.writeZero(RESERVED);
94 // FIXME: switch to ByteBuf
95 final byte[] tlvs = serializeTlvs(lspaObj.getTlvs());
96 if (tlvs.length != 0) {
97 body.writeBytes(tlvs);
99 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
102 public byte[] serializeTlvs(final Tlvs tlvs) {