Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPObjectiveFunctionObjectParser.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 java.util.NoSuchElementException;
11
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPObject;
14 import org.opendaylight.protocol.pcep.impl.PCEPOFCodesMapping;
15 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
16 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
17 import org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject;
18 import org.opendaylight.protocol.util.ByteArray;
19
20 /**
21  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject
22  * PCEPObjectiveFunctionObject}
23  */
24 public class PCEPObjectiveFunctionObjectParser implements PCEPObjectParser {
25
26         /*
27          * lengths of fields
28          */
29         public static final int OF_CODE_F_LENGTH = 2;
30
31         /*
32          * offsets of fields
33          */
34         public static final int OF_CODE_F_OFFSET = 0;
35         public static final int TLVS_OFFSET = OF_CODE_F_OFFSET + OF_CODE_F_LENGTH + 2; // added reserved field of size 2
36
37         @Override
38         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
39                 if (bytes == null || bytes.length == 0)
40                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
41
42                 if (bytes.length < TLVS_OFFSET)
43                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
44                 try {
45                         return new PCEPObjectiveFunctionObject(PCEPOFCodesMapping.getInstance().getFromCodeIdentifier(
46                                         ByteArray.bytesToShort(ByteArray.subByte(bytes, OF_CODE_F_OFFSET, OF_CODE_F_LENGTH)) & 0xFFFF), PCEPTlvParser.parse(ByteArray.cutBytes(
47                                         bytes, TLVS_OFFSET)), processed, ignored);
48                 } catch (final NoSuchElementException e) {
49                         throw new PCEPDeserializerException(e, "Objective function object has unknown identifier.");
50                 }
51         }
52
53         @Override
54         public byte[] put(PCEPObject obj) {
55                 if (!(obj instanceof PCEPObjectiveFunctionObject))
56                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPObjectiveFunction.");
57
58                 final PCEPObjectiveFunctionObject specObj = (PCEPObjectiveFunctionObject) obj;
59
60                 final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
61                 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
62
63                 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
64
65                 ByteArray.copyWhole(ByteArray.shortToBytes((short) PCEPOFCodesMapping.getInstance().getFromOFCodesEnum(specObj.getCode())), retBytes, OF_CODE_F_OFFSET);
66
67                 return retBytes;
68         }
69
70 }