BUG-47 : switched subobjects to generated source code.
[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.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LoadBalancingObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.requests.segment.computation.p2p.LoadBalancingBuilder;
21
22 /**
23  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject PCEPLoadBalancingObject}
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         public static final int FLAGS_F_LENGTH = 1;
32         public static final int MAX_LSP_F_LENGTH = 1;
33         public static final int MIN_BAND_F_LENGTH = 4;
34
35         public static final int FLAGS_F_OFFSET = 2;
36         public static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
37         public static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
38
39         public 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 LoadBalancingObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
47         PCEPDocumentedException {
48                 if (bytes == null || bytes.length == 0) {
49                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
50                 }
51
52                 if (bytes.length != SIZE) {
53                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
54                 }
55
56                 final LoadBalancingBuilder builder = new LoadBalancingBuilder();
57
58                 builder.setIgnore(header.isIgnore());
59                 builder.setProcessingRule(header.isProcessingRule());
60
61                 builder.setMaxLsp((short) (bytes[MAX_LSP_F_OFFSET] & 0xFF));
62                 builder.setMinBandwidth(new Float32(ByteArray.subByte(bytes, MIN_BAND_F_OFFSET, MIN_BAND_F_LENGTH)));
63
64                 return builder.build();
65         }
66
67         @Override
68         public void addTlv(final LoadBalancingBuilder builder, final Tlv tlv) {
69                 // No tlvs defined
70         }
71
72         @Override
73         public byte[] serializeObject(final Object object) {
74                 if (!(object instanceof LoadBalancingObject)) {
75                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass()
76                                         + ". Needed LoadBalancingObject.");
77                 }
78
79                 final LoadBalancingObject specObj = (LoadBalancingObject) object;
80
81                 final byte[] retBytes = new byte[SIZE];
82
83                 retBytes[MAX_LSP_F_OFFSET] = ByteArray.shortToBytes(specObj.getMaxLsp())[1];
84                 ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
85
86                 return retBytes;
87         }
88
89         @Override
90         public int getObjectType() {
91                 return TYPE;
92         }
93
94         @Override
95         public int getObjectClass() {
96                 return CLASS;
97         }
98 }