BUG-612 : switched PCEP message serializers to ByteBuf
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.object;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15
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;
26
27 /**
28  * Parser for {@link LoadBalancing}
29  */
30 public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<LoadBalancingBuilder> {
31
32     public static final int CLASS = 14;
33
34     public static final int TYPE = 1;
35
36     private static final int RESERVED = 2;
37     private static final int FLAGS_F_LENGTH = 1;
38
39     private static final int SIZE = 8;
40
41     public PCEPLoadBalancingObjectParser(final TlvRegistry tlvReg) {
42         super(tlvReg);
43     }
44
45     @Override
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
50                     + ".");
51         }
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();
59     }
60
61     @Override
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);
70     }
71 }