Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / auto-bandwidth-extension / src / main / java / org / opendaylight / protocol / pcep / auto / bandwidth / extension / BandwidthUsageObjectCodec.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.pcep.auto.bandwidth.extension;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
20 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
21 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev181109.bandwidth.usage.object.BandwidthUsage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.auto.bandwidth.rev181109.bandwidth.usage.object.BandwidthUsageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
28
29 public final class BandwidthUsageObjectCodec extends CommonObjectParser implements ObjectSerializer {
30
31     private static final int CLASS = 5;
32
33     private static final int BW_LENGTH = 4;
34
35     public BandwidthUsageObjectCodec(final int type) {
36         super(CLASS, type);
37     }
38
39     @Override
40     public BandwidthUsage parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
41         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42         if (bytes.readableBytes() % BW_LENGTH != 0) {
43             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
44                     + "; Expected multiple of " + BW_LENGTH + ".");
45         }
46         final BandwidthUsageBuilder builder = new BandwidthUsageBuilder()
47             .setIgnore(header.getIgnore())
48             .setProcessingRule(header.getProcessingRule());
49         final List<Bandwidth> bwSamples = new ArrayList<>(bytes.readableBytes() / BW_LENGTH);
50         while (bytes.isReadable()) {
51             bwSamples.add(new Bandwidth(ByteArray.readBytes(bytes, BW_LENGTH)));
52         }
53         builder.setBwSample(bwSamples);
54         return builder.build();
55     }
56
57     @Override
58     public void serializeObject(final Object object, final ByteBuf buffer) {
59         checkArgument(object instanceof BandwidthUsage,
60                 "Wrong instance of PCEPObject. Passed %s. Needed BandwidthUsage.", object.getClass());
61         final List<Bandwidth> bwSample = ((BandwidthUsage) object).getBwSample();
62         final ByteBuf body = Unpooled.buffer(bwSample.size() * BW_LENGTH);
63         for (final Bandwidth bw : bwSample) {
64             writeFloat32(bw, body);
65         }
66         ObjectUtil.formatSubobject(getObjectType(), getObjectClass(), object.getProcessingRule(), object.getIgnore(),
67             body, buffer);
68     }
69 }