Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPGlobalConstraintsObjectParser.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.PCEPObject;
13 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
14 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
15 import org.opendaylight.protocol.pcep.object.PCEPGlobalConstraintsObject;
16 import org.opendaylight.protocol.util.ByteArray;
17
18 /**
19  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPGlobalConstraintsObject
20  * PCEPGlobalConstraints}
21  */
22 public class PCEPGlobalConstraintsObjectParser implements PCEPObjectParser {
23
24         private final static int MAX_HOP_F_LENGTH = 1;
25         private final static int MAX_UTIL_F_LENGTH = 1;
26         private final static int MIN_UTIL_F_LENGTH = 1;
27         private final static int OVER_BOOKING_FACTOR_F_LENGTH = 1;
28
29         private final static int MAX_HOP_F_OFFSET = 0;
30         private final static int MAX_UTIL_F_OFFSET = MAX_HOP_F_OFFSET + MAX_HOP_F_LENGTH;
31         private final static int MIN_UTIL_F_OFFSET = MAX_UTIL_F_OFFSET + MAX_UTIL_F_LENGTH;
32         private final static int OVER_BOOKING_FACTOR_F_OFFSET = MIN_UTIL_F_OFFSET + MIN_UTIL_F_LENGTH;
33
34         private final static int TLVS_OFFSET = OVER_BOOKING_FACTOR_F_OFFSET + OVER_BOOKING_FACTOR_F_LENGTH;
35
36         @Override
37         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException, PCEPDocumentedException {
38                 if (bytes == null || bytes.length == 0)
39                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
40
41                 if (bytes.length < TLVS_OFFSET)
42                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
43
44                 return new PCEPGlobalConstraintsObject((short) (bytes[MAX_HOP_F_OFFSET] & 0xFF), (short) (bytes[MAX_UTIL_F_OFFSET] & 0xFF),
45                                 (short) (bytes[MIN_UTIL_F_OFFSET] & 0xFF), (short) (bytes[OVER_BOOKING_FACTOR_F_OFFSET] & 0xFF), PCEPTlvParser.parse(ByteArray.cutBytes(bytes,
46                                                 TLVS_OFFSET)), processed, ignored);
47         }
48
49         @Override
50         public byte[] put(PCEPObject obj) {
51                 if (!(obj instanceof PCEPGlobalConstraintsObject))
52                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPGlobalConstraints.");
53
54                 final PCEPGlobalConstraintsObject specObj = (PCEPGlobalConstraintsObject) obj;
55
56                 final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
57                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
58
59                 retBytes[MAX_HOP_F_OFFSET] = (byte) specObj.getMaxHop();
60                 retBytes[MAX_UTIL_F_OFFSET] = (byte) specObj.getMaxUtilization();
61                 retBytes[MIN_UTIL_F_OFFSET] = (byte) specObj.getMinUtilization();
62                 retBytes[OVER_BOOKING_FACTOR_F_OFFSET] = (byte) specObj.getOverBookingFactor();
63
64                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
65
66                 return retBytes;
67         }
68
69 }