Removed checkstyle warnings.
[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.TlvRegistry;
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.Tlvs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
22
23 /**
24  * Parser for Open object
25  */
26 public final class PCEPOpenObjectParser extends Stateful02OpenObjectParser {
27
28     public PCEPOpenObjectParser(final TlvRegistry tlvReg) {
29         super(tlvReg);
30     }
31
32     @Override
33     public void addTlv(final TlvsBuilder tbuilder, final Tlv tlv) {
34         super.addTlv(tbuilder, tlv);
35         final Tlvs2Builder statefulBuilder = new Tlvs2Builder();
36         if (tbuilder.getAugmentation(Tlvs2.class) != null) {
37             final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
38             if (t.getStateful() != null) {
39                 statefulBuilder.setStateful(t.getStateful());
40             }
41         }
42         final Tlvs1Builder cleanupBuilder = new Tlvs1Builder();
43         if (tbuilder.getAugmentation(Tlvs1.class) != null) {
44             final Tlvs1 t = tbuilder.getAugmentation(Tlvs1.class);
45             if (t.getLspCleanup() != null) {
46                 cleanupBuilder.setLspCleanup(t.getLspCleanup());
47             }
48         }
49         if (tlv instanceof Stateful) {
50             statefulBuilder.setStateful((Stateful) tlv);
51         } else if (tlv instanceof LspCleanup) {
52             cleanupBuilder.setLspCleanup((LspCleanup) tlv);
53         }
54         tbuilder.addAugmentation(Tlvs2.class, statefulBuilder.build());
55         tbuilder.addAugmentation(Tlvs1.class, cleanupBuilder.build());
56     }
57
58     @Override
59     public byte[] serializeTlvs(final Tlvs tlvs) {
60         if (tlvs == null) {
61             return new byte[0];
62         }
63         final byte[] prev = super.serializeTlvs(tlvs);
64         int finalLength = prev.length;
65         byte[] cleanupBytes = null;
66         if (tlvs.getAugmentation(Tlvs1.class) != null) {
67             final Tlvs1 cleanupTlv = tlvs.getAugmentation(Tlvs1.class);
68             if (cleanupTlv.getLspCleanup() != null) {
69                 cleanupBytes = serializeTlv(cleanupTlv.getLspCleanup());
70                 finalLength += cleanupBytes.length;
71             }
72         }
73         final byte[] result = new byte[finalLength];
74         ByteArray.copyWhole(prev, result, 0);
75         int offset = prev.length;
76         if (cleanupBytes != null) {
77             ByteArray.copyWhole(cleanupBytes, result, offset);
78             offset += cleanupBytes.length;
79         }
80         return result;
81     }
82 }