Bug-2225 Update PCEP segment routing according draft version 00.
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvParser;
17 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
18 import org.opendaylight.protocol.pcep.spi.TlvUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.pce.capability.tlv.SrPceCapability;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev150112.sr.pce.capability.tlv.SrPceCapabilityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
22
23 public class SrPceCapabilityTlvParser implements TlvParser, TlvSerializer {
24
25     public static final int TYPE = 26;
26
27     private static final int MSD_LENGTH = 1;
28     private static final int CONTENT_LENGTH = 4;
29     private static final int OFFSET = CONTENT_LENGTH - MSD_LENGTH;
30
31     @Override
32     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
33         Preconditions.checkArgument(tlv instanceof SrPceCapability, "SrPceCapability is mandatory.");
34         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
35         body.writerIndex(OFFSET);
36         writeUnsignedByte(((SrPceCapability) tlv).getMsd(), body);
37         TlvUtil.formatTlv(TYPE, body, buffer);
38     }
39
40     @Override
41     public Tlv parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
42         if (buffer == null) {
43             return null;
44         }
45         final short msd = buffer.readerIndex(OFFSET).readUnsignedByte();
46         return new SrPceCapabilityBuilder().setMsd(msd).build();
47     }
48 }