Merge "BUG-218: expose instruction status in MD-SAL"
[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 org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancing;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.load.balancing.object.LoadBalancingBuilder;
19
20 import com.google.common.primitives.UnsignedBytes;
21
22 /**
23  * Parser for {@link LoadBalancing}
24  */
25 public class PCEPLoadBalancingObjectParser extends AbstractObjectWithTlvsParser<LoadBalancingBuilder> {
26
27         public static final int CLASS = 14;
28
29         public static final int TYPE = 1;
30
31         private static final int FLAGS_F_LENGTH = 1;
32         private static final int MAX_LSP_F_LENGTH = 1;
33         private static final int MIN_BAND_F_LENGTH = 4;
34
35         private static final int FLAGS_F_OFFSET = 2;
36         private static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
37         private static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
38
39         private static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
40
41         public PCEPLoadBalancingObjectParser(final TlvHandlerRegistry tlvReg) {
42                 super(tlvReg);
43         }
44
45         @Override
46         public LoadBalancing parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
47                 if (bytes == null || bytes.length == 0) {
48                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
49                 }
50                 if (bytes.length != SIZE) {
51                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
52                 }
53                 final LoadBalancingBuilder builder = new LoadBalancingBuilder();
54                 builder.setIgnore(header.isIgnore());
55                 builder.setProcessingRule(header.isProcessingRule());
56                 builder.setMaxLsp((short) UnsignedBytes.toInt(bytes[MAX_LSP_F_OFFSET]));
57                 builder.setMinBandwidth(new Bandwidth(ByteArray.subByte(bytes, MIN_BAND_F_OFFSET, MIN_BAND_F_LENGTH)));
58                 return builder.build();
59         }
60
61         @Override
62         public void addTlv(final LoadBalancingBuilder builder, final Tlv tlv) {
63                 // No tlvs defined
64         }
65
66         @Override
67         public byte[] serializeObject(final Object object) {
68                 if (!(object instanceof LoadBalancing)) {
69                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
70                                         + ". Needed LoadBalancingObject.");
71                 }
72                 final LoadBalancing specObj = (LoadBalancing) object;
73                 final byte[] retBytes = new byte[SIZE];
74                 retBytes[MAX_LSP_F_OFFSET] = UnsignedBytes.checkedCast(specObj.getMaxLsp());
75                 ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
76                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
77         }
78
79         @Override
80         public int getObjectType() {
81                 return TYPE;
82         }
83
84         @Override
85         public int getObjectClass() {
86                 return CLASS;
87         }
88 }