2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
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.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.load.balancing.object.LoadBalancing;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.load.balancing.object.LoadBalancingBuilder;
29 * Parser for {@link LoadBalancing}.
31 public final class PCEPLoadBalancingObjectParser extends CommonObjectParser implements ObjectSerializer {
33 private static final int CLASS = 14;
34 private static final int TYPE = 1;
35 private static final int RESERVED = 2;
36 private static final int FLAGS_F_LENGTH = 1;
37 private static final int SIZE = 8;
39 public PCEPLoadBalancingObjectParser() {
44 public LoadBalancing parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
45 Preconditions.checkArgument(bytes != null && bytes.isReadable(),
46 "Array of bytes is mandatory. Can't be null or empty.");
47 if (bytes.readableBytes() != SIZE) {
48 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: "
49 + bytes.readableBytes() + "; Expected: " + SIZE + ".");
51 bytes.skipBytes(RESERVED + FLAGS_F_LENGTH);
52 final LoadBalancingBuilder builder = new LoadBalancingBuilder()
53 .setIgnore(header.isIgnore())
54 .setProcessingRule(header.isProcessingRule())
55 .setMaxLsp(ByteBufUtils.readUint8(bytes))
56 .setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)));
57 return builder.build();
61 public void serializeObject(final Object object, final ByteBuf buffer) {
62 Preconditions.checkArgument(object instanceof LoadBalancing,
63 "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.",
65 final LoadBalancing specObj = (LoadBalancing) object;
66 final ByteBuf body = Unpooled.buffer(SIZE);
67 body.writeZero(RESERVED + FLAGS_F_LENGTH);
68 writeUnsignedByte(specObj.getMaxLsp(), body);
69 writeFloat32(specObj.getMinBandwidth(), body);
70 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);