2caa57788f7d1f82ed8d3700a1f46f06bc71dfe1
[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
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
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.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancing;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancingBuilder;
25
26 /**
27  * Parser for {@link LoadBalancing}
28  */
29 public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<LoadBalancingBuilder> {
30
31     public static final int CLASS = 14;
32
33     public static final int TYPE = 1;
34
35     private static final int FLAGS_F_LENGTH = 1;
36     private static final int MAX_LSP_F_LENGTH = 1;
37     private static final int MIN_BAND_F_LENGTH = 4;
38
39     private static final int FLAGS_F_OFFSET = 2;
40     private static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
41     private static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
42
43     private static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
44
45     public PCEPLoadBalancingObjectParser(final TlvRegistry tlvReg) {
46         super(tlvReg);
47     }
48
49     @Override
50     public LoadBalancing parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
51         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52         if (bytes.readableBytes() != SIZE) {
53             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
54                     + ".");
55         }
56         final LoadBalancingBuilder builder = new LoadBalancingBuilder();
57         builder.setIgnore(header.isIgnore());
58         builder.setProcessingRule(header.isProcessingRule());
59         bytes.readerIndex(bytes.readerIndex() + MAX_LSP_F_OFFSET);
60         builder.setMaxLsp((short) UnsignedBytes.toInt(bytes.readByte()));
61         builder.setMinBandwidth(new Bandwidth(ByteArray.readBytes(bytes, MIN_BAND_F_LENGTH)));
62         return builder.build();
63     }
64
65     @Override
66     public byte[] serializeObject(final Object object) {
67         if (!(object instanceof LoadBalancing)) {
68             throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
69                     + ". Needed LoadBalancingObject.");
70         }
71         final LoadBalancing specObj = (LoadBalancing) object;
72         final byte[] retBytes = new byte[SIZE];
73         retBytes[MAX_LSP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxLsp());
74         ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
75         return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
76     }
77 }