Removed checkstyle warnings.
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / Stateful02OpenObjectParser.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.ietf.stateful02;
9
10 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
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.stateful._02.rev140110.Tlvs2;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.Tlvs2Builder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.node.identifier.tlv.NodeIdentifier;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.stateful.capability.tlv.Stateful;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.Tlvs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
20
21 /**
22  * Parser for Open Object
23  */
24 public class Stateful02OpenObjectParser extends PCEPOpenObjectParser {
25
26     public Stateful02OpenObjectParser(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 statefulBuilder = new Tlvs2Builder();
34         if (tbuilder.getAugmentation(Tlvs2.class) != null) {
35             final Tlvs2 t = tbuilder.getAugmentation(Tlvs2.class);
36             if (t.getStateful() != null) {
37                 statefulBuilder.setStateful(t.getStateful());
38             }
39             if (t.getNodeIdentifier() != null) {
40                 statefulBuilder.setNodeIdentifier(t.getNodeIdentifier());
41             }
42         }
43         if (tlv instanceof Stateful) {
44             statefulBuilder.setStateful((Stateful) tlv);
45         } else if (tlv instanceof NodeIdentifier) {
46             statefulBuilder.setNodeIdentifier((NodeIdentifier) tlv);
47         }
48         tbuilder.addAugmentation(Tlvs2.class, statefulBuilder.build());
49     }
50
51     @Override
52     public byte[] serializeTlvs(final Tlvs tlvs) {
53         if (tlvs == null) {
54             return new byte[0];
55         }
56         final byte[] prev = super.serializeTlvs(tlvs);
57         int finalLength = prev.length;
58         byte[] statefulBytes = null;
59         byte[] nodeIdBytes = null;
60         if (tlvs.getAugmentation(Tlvs2.class) != null) {
61             final Tlvs2 statefulTlvs = tlvs.getAugmentation(Tlvs2.class);
62             if (statefulTlvs.getStateful() != null) {
63                 statefulBytes = serializeTlv(statefulTlvs.getStateful());
64                 finalLength += statefulBytes.length;
65             }
66             if (statefulTlvs.getNodeIdentifier() != null) {
67                 nodeIdBytes = serializeTlv(statefulTlvs.getNodeIdentifier());
68                 finalLength += nodeIdBytes.length;
69             }
70         }
71
72         final byte[] result = new byte[finalLength];
73         ByteArray.copyWhole(prev, result, 0);
74         int offset = prev.length;
75         if (statefulBytes != null) {
76             ByteArray.copyWhole(statefulBytes, result, offset);
77             offset += statefulBytes.length;
78         }
79         if (nodeIdBytes != null) {
80             ByteArray.copyWhole(nodeIdBytes, result, offset);
81             offset += nodeIdBytes.length;
82         }
83         return result;
84     }
85 }