2 * Copyright (c) 2014 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.segment.routing;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvParser;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.pcep.spi.TlvUtil;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.sr.pce.capability.tlv.SrPceCapability;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev200720.sr.pce.capability.tlv.SrPceCapabilityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
22 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
24 public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
25 public static final int TYPE = 26;
27 private static final int BITSET_LENGTH = 8;
28 private static final int N_FLAG_POSITION = 7;
29 private static final int X_FLAG_POSITION = 6;
30 private static final int CONTENT_LENGTH = 4;
31 private static final int OFFSET = 2;
34 public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
35 checkArgument(tlv instanceof SrPceCapability, "SrPceCapability is mandatory.");
36 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
38 /* Reserved 2 bytes */
39 body.writerIndex(OFFSET);
42 final SrPceCapability srPceCapability = (SrPceCapability) tlv;
43 final BitArray bits = new BitArray(BITSET_LENGTH);
44 bits.set(N_FLAG_POSITION, srPceCapability.getNFlag());
45 bits.set(X_FLAG_POSITION, srPceCapability.getXFlag());
49 ByteBufUtils.writeOrZero(body, srPceCapability.getMsd());
51 TlvUtil.formatTlv(TYPE, body, buffer);
55 public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
59 final BitArray bitSet = BitArray.valueOf(buffer.readerIndex(OFFSET).readByte());
60 final boolean n = bitSet.get(N_FLAG_POSITION);
61 final boolean x = bitSet.get(X_FLAG_POSITION);
63 return new SrPceCapabilityBuilder()
66 .setMsd(ByteBufUtils.readUint8(buffer))