BUG-64 : reformat TlvRegistry, to skip using getType method.
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07LspaObjectParser.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 org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
11 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs2Builder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.Lspa;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.Tlvs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lspa.object.lspa.TlvsBuilder;
20
21 /**
22  * Parser for {@link Lspa}
23  */
24 public final class Stateful07LspaObjectParser extends PCEPLspaObjectParser {
25
26         public Stateful07LspaObjectParser(final TlvRegistry tlvReg) {
27                 super(tlvReg);
28         }
29
30         @Override
31         public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
32                 super.addTlv(tbuilder, tlv);
33                 final Tlvs2Builder nameBuilder = new Tlvs2Builder();
34                 if (tbuilder.getAugmentation(Tlvs2.class) != null) {
35                         final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
36                         if (t.getSymbolicPathName() != null) {
37                                 nameBuilder.setSymbolicPathName(t.getSymbolicPathName());
38                         }
39                 }
40                 if (tlv instanceof SymbolicPathName) {
41                         nameBuilder.setSymbolicPathName((SymbolicPathName) tlv);
42                 }
43                 tbuilder.addAugmentation(Tlvs2.class, nameBuilder.build());
44         }
45
46         @Override
47         public byte[] serializeTlvs(final Tlvs tlvs) {
48                 if (tlvs == null) {
49                         return new byte[0];
50                 }
51                 final byte[] prev = super.serializeTlvs(tlvs);
52                 int finalLength = prev.length;
53                 byte[] nameBytes = null;
54                 if (tlvs.getAugmentation(Tlvs2.class) != null) {
55                         final Tlvs2 nameTlvs = tlvs.getAugmentation(Tlvs2.class);
56                         if (nameTlvs.getSymbolicPathName() != null) {
57                                 nameBytes = serializeTlv(nameTlvs.getSymbolicPathName());
58                                 finalLength += nameBytes.length;
59                         }
60                 }
61                 final byte[] result = new byte[finalLength];
62                 ByteArray.copyWhole(prev, result, 0);
63                 int offset = prev.length;
64                 if (nameBytes != null) {
65                         ByteArray.copyWhole(nameBytes, result, offset);
66                         offset += nameBytes.length;
67                 }
68                 return result;
69         }
70 }