Merge "Support proper route redistribution"
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / initiated00 / CInitiated00StatefulCapabilityTlvParser.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.initiated00;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.ietf.stateful07.Stateful07StatefulCapabilityTlvParser;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvUtil;
16 import org.opendaylight.protocol.util.BitArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.Stateful;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
22
23 /**
24  * Parser for {@link Stateful}
25  */
26 public final class CInitiated00StatefulCapabilityTlvParser extends Stateful07StatefulCapabilityTlvParser {
27
28     private static final int I_FLAG_OFFSET = 29;
29
30     @Override
31     public Stateful parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
32         if (buffer == null) {
33             return null;
34         }
35         if (buffer.readableBytes() < FLAGS_F_LENGTH / Byte.SIZE) {
36             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >= "
37                     + FLAGS_F_LENGTH / Byte.SIZE + ".");
38         }
39         final BitArray flags = BitArray.valueOf(buffer, FLAGS_F_LENGTH);
40         final StatefulBuilder sb = new StatefulBuilder();
41         sb.setLspUpdateCapability(flags.get(U_FLAG_OFFSET));
42         if (flags.get(I_FLAG_OFFSET)) {
43             sb.addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build());
44         }
45         return sb.build();
46     }
47
48     @Override
49     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
50         Preconditions.checkArgument(tlv instanceof Stateful, "StatefulCapabilityTlv is mandatory.");
51         final Stateful sct = (Stateful) tlv;
52         final BitArray flags = new BitArray(FLAGS_F_LENGTH);
53         final Stateful1 sfi = sct.getAugmentation(Stateful1.class);
54         if (sfi != null) {
55             flags.set(I_FLAG_OFFSET, sfi.isInitiation());
56         }
57         flags.set(U_FLAG_OFFSET, sct.isLspUpdateCapability());
58         TlvUtil.formatTlv(TYPE, Unpooled.wrappedBuffer(flags.array()), buffer);
59     }
60 }