BUG-64 : initial rewrite, XROSubobjectRegistry.
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / crabbe / initiated00 / PCEPOpenObjectParser.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.crabbe.initiated00;
9
10 import org.opendaylight.protocol.pcep.ietf.stateful02.Stateful02OpenObjectParser;
11 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.Tlvs1Builder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated._00.rev140113.lsp.cleanup.tlv.LspCleanup;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
23
24 /**
25  * Parser for {@link Open}
26  */
27 public final class PCEPOpenObjectParser extends Stateful02OpenObjectParser {
28
29         public PCEPOpenObjectParser(final TlvHandlerRegistry tlvReg) {
30                 super(tlvReg);
31         }
32
33         @Override
34         public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
35                 super.addTlv(tbuilder, tlv);
36                 final Tlvs2Builder statefulBuilder = new Tlvs2Builder();
37                 if (tbuilder.getAugmentation(Tlvs2.class) != null) {
38                         final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
39                         if (t.getStateful() != null) {
40                                 statefulBuilder.setStateful(t.getStateful());
41                         }
42                 }
43                 final Tlvs1Builder cleanupBuilder = new Tlvs1Builder();
44                 if (tbuilder.getAugmentation(Tlvs1.class) != null) {
45                         final Tlvs1 t = tbuilder.getAugmentation(Tlvs1.class);
46                         if (t.getLspCleanup() != null) {
47                                 cleanupBuilder.setLspCleanup(t.getLspCleanup());
48                         }
49                 }
50                 if (tlv instanceof Stateful) {
51                         statefulBuilder.setStateful((Stateful) tlv);
52                 } else if (tlv instanceof LspCleanup) {
53                         cleanupBuilder.setLspCleanup((LspCleanup) tlv);
54                 }
55                 tbuilder.addAugmentation(Tlvs2.class, statefulBuilder.build());
56                 tbuilder.addAugmentation(Tlvs1.class, cleanupBuilder.build());
57         }
58
59         @Override
60         public byte[] serializeTlvs(final Tlvs tlvs) {
61                 if (tlvs == null) {
62                         return new byte[0];
63                 }
64                 final byte[] prev = super.serializeTlvs(tlvs);
65                 int finalLength = prev.length;
66                 byte[] cleanupBytes = null;
67                 if (tlvs.getAugmentation(Tlvs1.class) != null) {
68                         final Tlvs1 cleanupTlv = tlvs.getAugmentation(Tlvs1.class);
69                         if (cleanupTlv.getLspCleanup() != null) {
70                                 cleanupBytes = serializeTlv(cleanupTlv.getLspCleanup());
71                                 finalLength += cleanupBytes.length;
72                         }
73                 }
74                 final byte[] result = new byte[finalLength];
75                 ByteArray.copyWhole(prev, result, 0);
76                 int offset = prev.length;
77                 if (cleanupBytes != null) {
78                         ByteArray.copyWhole(cleanupBytes, result, offset);
79                         offset += cleanupBytes.length;
80                 }
81                 return result;
82         }
83 }