2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.pcep.auto.bandwidth.extension;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
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;
29 public final class BandwidthUsageObjectCodec extends CommonObjectParser implements ObjectSerializer {
31 private static final int CLASS = 5;
33 private static final int BW_LENGTH = 4;
35 public BandwidthUsageObjectCodec(final int type) {
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 + ".");
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)));
53 builder.setBwSample(bwSamples);
54 return builder.build();
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);
66 ObjectUtil.formatSubobject(getObjectType(), getObjectClass(), object.getProcessingRule(), object.getIgnore(),