Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[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.PCEPObject;
12 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
13 import org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nps.concepts.rev130930.Bandwidth;
16
17 /**
18  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLoadBalancingObject PCEPLoadBalancingObject}
19  */
20 public class PCEPLoadBalancingObjectParser implements PCEPObjectParser {
21
22         public static final int FLAGS_F_LENGTH = 1;
23         public static final int MAX_LSP_F_LENGTH = 1;
24         public static final int MIN_BAND_F_LENGTH = 4;
25
26         public static final int FLAGS_F_OFFSET = 2;
27         public static final int MAX_LSP_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
28         public static final int MIN_BAND_F_OFFSET = MAX_LSP_F_OFFSET + MAX_LSP_F_LENGTH;
29
30         public static final int SIZE = MIN_BAND_F_OFFSET + MIN_BAND_F_LENGTH;
31
32         @Override
33         public PCEPObject parse(final byte[] bytes, final boolean processed, final boolean ignored) throws PCEPDeserializerException {
34                 if (bytes == null || bytes.length == 0)
35                         throw new IllegalArgumentException("Byte array is mandatory. Can't be null or empty.");
36
37                 if (bytes.length != SIZE)
38                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
39
40                 return new PCEPLoadBalancingObject(bytes[MAX_LSP_F_OFFSET] & 0xFF, new Bandwidth(ByteArray.subByte(bytes, MIN_BAND_F_OFFSET,
41                                 MIN_BAND_F_LENGTH)), processed);
42         }
43
44         @Override
45         public byte[] put(final PCEPObject obj) {
46                 if (!(obj instanceof PCEPLoadBalancingObject))
47                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass()
48                                         + ". Needed PCEPLoadBalancingObject.");
49
50                 final PCEPLoadBalancingObject specObj = (PCEPLoadBalancingObject) obj;
51
52                 final byte[] retBytes = new byte[SIZE];
53
54                 retBytes[MAX_LSP_F_OFFSET] = ByteArray.intToBytes(specObj.getMaxLSP())[Integer.SIZE / Byte.SIZE - 1];
55                 ByteArray.copyWhole(specObj.getMinBandwidth().getValue(), retBytes, MIN_BAND_F_OFFSET);
56
57                 return retBytes;
58         }
59 }