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.impl.object;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancing;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancingBuilder;
28 * Parser for {@link LoadBalancing}
30 public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<LoadBalancingBuilder> {
32 public static final int CLASS = 14;
34 public static final int TYPE = 1;
36 private static final int RESERVED = 2;
37 private static final int FLAGS_F_LENGTH = 1;
39 private static final int SIZE = 8;
41 public PCEPLoadBalancingObjectParser(final TlvRegistry tlvReg) {
46 public LoadBalancing parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
47 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
48 if (bytes.readableBytes() != SIZE) {
49 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
52 final LoadBalancingBuilder builder = new LoadBalancingBuilder();
53 builder.setIgnore(header.isIgnore());
54 builder.setProcessingRule(header.isProcessingRule());
55 bytes.readerIndex(bytes.readerIndex() + RESERVED + FLAGS_F_LENGTH);
56 builder.setMaxLsp((short) UnsignedBytes.toInt(bytes.readByte()));
57 builder.setMinBandwidth(new Bandwidth(ByteArray.readAllBytes(bytes)));
58 return builder.build();
62 public void serializeObject(final Object object, final ByteBuf buffer) {
63 Preconditions.checkArgument(object instanceof LoadBalancing, "Wrong instance of PCEPObject. Passed %s. Needed LoadBalancingObject.", object.getClass());
64 final LoadBalancing specObj = (LoadBalancing) object;
65 final ByteBuf body = Unpooled.buffer(SIZE);
66 body.writeZero(RESERVED + FLAGS_F_LENGTH);
67 body.writeByte(specObj.getMaxLsp());
68 body.writeBytes(specObj.getMinBandwidth().getValue());
69 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);