2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.object;
10 import java.util.NoSuchElementException;
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;
21 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPObjectiveFunctionObject
22 * PCEPObjectiveFunctionObject}
24 public class PCEPObjectiveFunctionObjectParser implements PCEPObjectParser {
29 public static final int OF_CODE_F_LENGTH = 2;
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
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.");
42 if (bytes.length < TLVS_OFFSET)
43 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
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.");
54 public byte[] put(PCEPObject obj) {
55 if (!(obj instanceof PCEPObjectiveFunctionObject))
56 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPObjectiveFunction.");
58 final PCEPObjectiveFunctionObject specObj = (PCEPObjectiveFunctionObject) obj;
60 final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
61 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length];
63 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
65 ByteArray.copyWhole(ByteArray.shortToBytes((short) PCEPOFCodesMapping.getInstance().getFromOFCodesEnum(specObj.getCode())), retBytes, OF_CODE_F_OFFSET);