Bug-607: Segment Routing TLVs parsers/serializers
[bgpcep.git] / pcep / segment-routing / src / main / java / org / opendaylight / protocol / pcep / lsp / setup / type01 / CInitiated00SrpObjectWithPstTlvParser.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
9 package org.opendaylight.protocol.pcep.lsp.setup.type01;
10
11 import org.opendaylight.protocol.pcep.ietf.initiated00.CInitiated00SrpObjectParser;
12 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.SrpBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.Tlvs;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.srp.object.srp.TlvsBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.Tlvs8Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.lsp.setup.type._01.rev140507.path.setup.type.tlv.PathSetupType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21
22 public class CInitiated00SrpObjectWithPstTlvParser extends CInitiated00SrpObjectParser {
23
24     public CInitiated00SrpObjectWithPstTlvParser(TlvRegistry tlvReg) {
25         super(tlvReg);
26     }
27
28     @Override
29     public void addTlv(SrpBuilder builder, Tlv tlv) {
30         super.addTlv(builder, tlv);
31         final Tlvs8Builder tlvBuilder = new Tlvs8Builder();
32         if (builder.getTlvs() != null) {
33             if (builder.getTlvs().getAugmentation(Tlvs8.class) != null) {
34                 final Tlvs8 t = builder.getTlvs().getAugmentation(Tlvs8.class);
35                 if (t.getPathSetupType() != null) {
36                     tlvBuilder.setPathSetupType(t.getPathSetupType());
37                 }
38             }
39         }
40         if (tlv instanceof PathSetupType) {
41             tlvBuilder.setPathSetupType((PathSetupType) tlv);
42         }
43         builder.setTlvs(new TlvsBuilder().addAugmentation(Tlvs8.class, tlvBuilder.build()).build());
44     }
45
46     @Override
47     public byte[] serializeTlvs(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(Tlvs8.class) != null) {
55             final Tlvs8 nameTlvs = tlvs.getAugmentation(Tlvs8.class);
56             if (nameTlvs.getPathSetupType() != null) {
57                 nameBytes = serializeTlv(nameTlvs.getPathSetupType());
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
71 }