Add new revision for pcep types model
[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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
12
13 import com.google.common.base.Preconditions;
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.spi.CommonObjectParser;
19 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
21 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
42                 "Array of bytes is mandatory. Can't be null or empty.");
43         if (bytes.readableBytes() % BW_LENGTH != 0) {
44             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
45                     + "; Expected multiple of " + BW_LENGTH + ".");
46         }
47         final BandwidthUsageBuilder builder = new BandwidthUsageBuilder();
48         builder.setIgnore(header.isIgnore());
49         builder.setProcessingRule(header.isProcessingRule());
50         final List<Bandwidth> bwSamples = new ArrayList<>(bytes.readableBytes() / BW_LENGTH);
51         while (bytes.isReadable()) {
52             bwSamples.add(new Bandwidth(ByteArray.readBytes(bytes, BW_LENGTH)));
53         }
54         builder.setBwSample(bwSamples);
55         return builder.build();
56     }
57
58     @Override
59     public void serializeObject(final Object object, final ByteBuf buffer) {
60         Preconditions.checkArgument(object instanceof BandwidthUsage,
61                 "Wrong instance of PCEPObject. Passed %s. Needed BandwidthUsage.", object.getClass());
62         final List<Bandwidth> bwSample = ((BandwidthUsage) object).getBwSample();
63         final ByteBuf body = Unpooled.buffer(bwSample.size() * BW_LENGTH);
64         for (final Bandwidth bw : bwSample) {
65             writeFloat32(bw, body);
66         }
67         ObjectUtil.formatSubobject(getObjectType(), getObjectClass(), object.isProcessingRule(), object.isIgnore(),
68             body, buffer);
69     }
70 }