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