BUG-1663 : Removed check for SRP processed flag in parser.
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07StatefulCapabilityTlvParser.java
1 /*
2  * Copyright (c) 2013 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.ietf.stateful07;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.BitSet;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.TlvParser;
18 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
19 import org.opendaylight.protocol.pcep.spi.TlvUtil;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24
25 /**
26  * Parser for {@link Stateful}
27  */
28 public class Stateful07StatefulCapabilityTlvParser implements TlvParser, TlvSerializer {
29
30     public static final int TYPE = 16;
31
32     protected static final int FLAGS_F_LENGTH = 4;
33
34     protected static final int U_FLAG_OFFSET = 31;
35
36     @Override
37     public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
38         if (buffer == null) {
39             return null;
40         }
41         if (buffer.readableBytes() < FLAGS_F_LENGTH) {
42             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
43                     + FLAGS_F_LENGTH + ".");
44         }
45         final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
46
47         final StatefulBuilder sb = new StatefulBuilder();
48         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
49         return sb.build();
50     }
51
52     @Override
53     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
54         Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
55         final Stateful sct = (Stateful) tlv;
56         final ByteBuf body = Unpooled.buffer();
57         final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
58         if (sct.isLspUpdateCapability() != null) {
59             flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
60         }
61         writeBitSet(flags, FLAGS_F_LENGTH, body);
62         TlvUtil.formatTlv(TYPE, body, buffer);
63     }
64 }