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