BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPCloseObjectParser.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.impl.object;
9
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.impl.Util;
13 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
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.close.message.c.close.message.CClose;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.c.close.message.CCloseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.Tlvs;
23
24 /**
25  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPCloseObject PCEPCloseObject}
26  */
27 public class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<CCloseBuilder> {
28
29         public static final int CLASS = 15;
30
31         public static final int TYPE = 1;
32
33         /*
34          * lengths of fields in bytes
35          */
36         public static final int FLAGS_F_LENGTH = 1;
37         public static final int REASON_F_LENGTH = 1;
38
39         /*
40          * offsets of fields in bytes
41          */
42         public static final int FLAGS_F_OFFSET = 2; // added reserved field of size 2 bytes
43         public static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
44
45         /*
46          * total size of object in bytes
47          */
48         public static final int TLVS_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
49
50         public PCEPCloseObjectParser(final TlvHandlerRegistry tlvReg) {
51                 super(tlvReg);
52         }
53
54         @Override
55         public CloseObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
56                 if (bytes == null) {
57                         throw new IllegalArgumentException("Byte array is mandatory.");
58                 }
59
60                 final CCloseBuilder builder = new CCloseBuilder();
61
62                 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
63
64                 builder.setIgnore(header.isIgnore());
65                 builder.setProcessingRule(header.isProcessingRule());
66
67                 builder.setReason((short) (bytes[REASON_F_OFFSET] & 0xFF));
68
69                 return builder.build();
70         }
71
72         @Override
73         public void addTlv(final CCloseBuilder builder, final Tlv tlv) {
74                 // No tlvs defined
75         }
76
77         @Override
78         public byte[] serializeObject(final Object object) {
79                 if (!(object instanceof CloseObject)) {
80                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed CloseObject.");
81                 }
82
83                 final CloseObject obj = (CloseObject) object;
84
85                 final byte[] tlvs = serializeTlvs(obj.getTlvs());
86                 int tlvsLength = 0;
87                 if (tlvs != null) {
88                         tlvsLength = tlvs.length;
89                 }
90                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + Util.getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
91
92                 if (tlvs != null) {
93                         ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
94                 }
95
96                 final int reason = ((CClose) obj).getReason().intValue();
97
98                 retBytes[REASON_F_OFFSET] = (byte) reason;
99
100                 return retBytes;
101         }
102
103         public byte[] serializeTlvs(final Tlvs tlvs) {
104                 // No tlvs defined
105                 return new byte[0];
106         }
107
108         @Override
109         public int getObjectType() {
110                 return TYPE;
111         }
112
113         @Override
114         public int getObjectClass() {
115                 return CLASS;
116         }
117 }