BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPBandwidthObjectParser.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.message.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.BandwidthObject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.p2p.reported.route.BandwidthBuilder;
20
21 /**
22  * Parser for {@link BandwidthObject}
23  */
24 public class PCEPBandwidthObjectParser extends AbstractObjectWithTlvsParser<BandwidthBuilder> {
25
26         public static final int E_CLASS = 5;
27
28         public static final int E_TYPE = 1;
29
30         public static final int CLASS = 5;
31
32         public static final int TYPE = 2;
33
34         private static final int BANDWIDTH_F_LENGTH = 4;
35
36         public PCEPBandwidthObjectParser(final TlvHandlerRegistry tlvReg) {
37                 super(tlvReg);
38         }
39
40         @Override
41         public BandwidthObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
42         PCEPDocumentedException {
43                 if (bytes == null || bytes.length == 0) {
44                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
45                 }
46                 if (bytes.length != BANDWIDTH_F_LENGTH) {
47                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: "
48                                         + BANDWIDTH_F_LENGTH + ".");
49                 }
50
51                 final BandwidthBuilder builder = new BandwidthBuilder();
52
53                 builder.setIgnore(header.isIgnore());
54                 builder.setProcessingRule(header.isProcessingRule());
55
56                 builder.setBandwidth(new Float32(bytes));
57
58                 return builder.build();
59         }
60
61         @Override
62         public void addTlv(final BandwidthBuilder builder, final Tlv tlv) {
63                 // No tlvs defined
64         }
65
66         @Override
67         public byte[] serializeObject(final Object object) {
68                 if (!(object instanceof BandwidthObject)) {
69                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed BandwidthObject.");
70                 }
71
72                 return ((BandwidthObject) object).getBandwidth().getValue();
73         }
74
75         @Override
76         public int getObjectType() {
77                 return TYPE;
78         }
79
80         @Override
81         public int getObjectClass() {
82                 return CLASS;
83         }
84
85         public int getEObjectType() {
86                 return E_TYPE;
87         }
88
89         public int getEObjectClass() {
90                 return E_CLASS;
91         }
92 }