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