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