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