Bump versions to 0.21.6-SNAPSHOT
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.object;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.Bandwidth;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.BandwidthBuilder;
23
24 /**
25  * Parser for Bandwidth.
26  */
27 public final class PCEPBandwidthObjectParser extends CommonObjectParser implements ObjectSerializer {
28
29     private static final int CLASS = 5;
30     private static final int TYPE = 1;
31
32     public static final int BANDWIDTH_F_LENGTH = 4;
33
34     public PCEPBandwidthObjectParser() {
35         super(CLASS, TYPE);
36     }
37
38     @Override
39     public Bandwidth parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
40         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
41             "Array of bytes is mandatory. Can't be null or empty.");
42         if (bytes.readableBytes() != BANDWIDTH_F_LENGTH) {
43             throw new PCEPDeserializerException(
44                 "Wrong length of array of bytes. Passed: " + bytes.readableBytes()
45                 + "; Expected: " + BANDWIDTH_F_LENGTH + ".");
46         }
47         return new BandwidthBuilder()
48                 .setIgnore(header.getIgnore())
49                 .setProcessingRule(header.getProcessingRule())
50                 .setBandwidth(
51                     new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125
52                     .Bandwidth(ByteArray.getAllBytes(bytes)))
53                 .build();
54     }
55
56     @Override
57     public void serializeObject(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types
58         .rev181109.Object object, final ByteBuf buffer) {
59         Preconditions.checkArgument(object instanceof Bandwidth,
60             "Wrong instance of PCEPObject. Passed %s. Needed BandwidthObject.", object.getClass());
61         final ByteBuf body = Unpooled.buffer();
62         writeFloat32(((Bandwidth) object).getBandwidth(), body);
63         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
64     }
65 }