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.BitSet;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPObject;
14 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
15 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
16 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
17 import org.opendaylight.protocol.util.ByteArray;
20 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLspObject PCEPLspObject}
22 public class PCEPLspObjectParser implements PCEPObjectParser {
25 * offset of TLVs offset of other fields are not defined as constants
26 * because of non-standard mapping of bits
28 public static final int TLVS_OFFSET = 4;
31 * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
33 private static final int DELEGATE_FLAG_OFFSET = 15;
34 private static final int OPERATIONAL_FLAG_OFFSET = 13;
35 private static final int SYNC_FLAG_OFFSET = 14;
36 private static final int REMOVE_FLAG_OFFSET = 12;
39 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
40 if (bytes == null || bytes.length == 0)
41 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
43 if (bytes.length < TLVS_OFFSET)
44 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + TLVS_OFFSET + ".");
46 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
48 return new PCEPLspObject((ByteArray.bytesToShort(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4,
49 flags.get(DELEGATE_FLAG_OFFSET), flags.get(SYNC_FLAG_OFFSET), flags.get(OPERATIONAL_FLAG_OFFSET), flags.get(REMOVE_FLAG_OFFSET),
50 PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_OFFSET)));
54 public byte[] put(PCEPObject obj) {
55 if (!(obj instanceof PCEPLspObject))
56 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPLspObject.");
58 final PCEPLspObject specObj = (PCEPLspObject) obj;
60 final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
62 final byte[] retBytes = new byte[tlvs.length + TLVS_OFFSET];
64 final int lspID = specObj.getLspID();
65 retBytes[0] = (byte) (lspID >> 12);
66 retBytes[1] = (byte) (lspID >> 4);
67 retBytes[2] = (byte) (lspID << 4);
68 if (specObj.isDelegate())
69 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
70 if (specObj.isOperational())
71 retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
72 if (specObj.isRemove())
73 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
75 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
77 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);