Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / segment / routing / SrPceCapabilityTlvParser.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.segment.routing;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
23
24 public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
25     public static final int TYPE = 26;
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;
32
33     @Override
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);
37
38         /* Reserved 2 bytes */
39         body.writerIndex(OFFSET);
40
41         /* Flags */
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());
46         bits.toByteBuf(body);
47
48         /* MSD */
49         ByteBufUtils.writeOrZero(body, srPceCapability.getMsd());
50
51         TlvUtil.formatTlv(TYPE, body, buffer);
52     }
53
54     @Override
55     public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
56         if (buffer == null) {
57             return null;
58         }
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);
62
63         return new SrPceCapabilityBuilder()
64                 .setNFlag(n)
65                 .setXFlag(x)
66                 .setMsd(ByteBufUtils.readUint8(buffer))
67                 .build();
68     }
69 }