Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPLoadBalancingObjectParser.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.network.concepts.rev131125.Bandwidth;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.load.balancing.object.LoadBalancing;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.load.balancing.object.LoadBalancingBuilder;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26
27 /**
28  * Parser for {@link LoadBalancing}.
29  */
30 public final class PCEPLoadBalancingObjectParser extends CommonObjectParser implements ObjectSerializer {
31     private static final int CLASS = 14;
32     private static final int TYPE = 1;
33     private static final int RESERVED = 2;
34     private static final int FLAGS_F_LENGTH = 1;
35     private static final int SIZE = 8;
36
37     public PCEPLoadBalancingObjectParser() {
38         super(CLASS, TYPE);
39     }
40
41     @Override
42     public LoadBalancing parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
43         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
44         if (bytes.readableBytes() != SIZE) {
45             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: "
46                 + bytes.readableBytes() + "; Expected: " + SIZE + ".");
47         }
48         bytes.skipBytes(RESERVED + FLAGS_F_LENGTH);
49         return new LoadBalancingBuilder()
50                 .setIgnore(header.getIgnore())
51                 .setProcessingRule(header.getProcessingRule())
52                 .setMaxLsp(ByteBufUtils.readUint8(bytes))
53                 .setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)))
54                 .build();
55     }
56
57     @Override
58     public void serializeObject(final Object object, final ByteBuf buffer) {
59         checkArgument(object instanceof LoadBalancing,
60             "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.", object.getClass());
61         final LoadBalancing specObj = (LoadBalancing) object;
62         final ByteBuf body = Unpooled.buffer(SIZE);
63         body.writeZero(RESERVED + FLAGS_F_LENGTH);
64         ByteBufUtils.writeOrZero(body, specObj.getMaxLsp());
65         writeFloat32(specObj.getMinBandwidth(), body);
66         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
67     }
68 }