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