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.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.impl.message.AbstractObjectWithTlvsParser;
15 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspObject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcinitiate.message.pcinitiate.message.requests.LspBuilder;
24 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPLspObject PCEPLspObject}
26 public class PCEPLspObjectParser extends AbstractObjectWithTlvsParser<LspBuilder> {
28 public static final int CLASS = 32;
30 public static final int TYPE = 1;
33 * offset of TLVs offset of other fields are not defined as constants
34 * because of non-standard mapping of bits
36 public static final int TLVS_OFFSET = 4;
39 * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
41 private static final int DELEGATE_FLAG_OFFSET = 15;
42 private static final int OPERATIONAL_FLAG_OFFSET = 13;
43 private static final int SYNC_FLAG_OFFSET = 14;
44 private static final int REMOVE_FLAG_OFFSET = 12;
46 public PCEPLspObjectParser(final TlvHandlerRegistry tlvReg) {
51 public LspObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
52 if (bytes == null || bytes.length == 0) {
53 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
56 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
58 final LspBuilder builder = new LspBuilder();
60 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
62 builder.setIgnore(header.isIgnore());
63 builder.setProcessingRule(header.isProcessingRule());
65 // builder.setPlspId(new PlspId(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2]
67 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
68 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
69 // builder.setOperational(Operational.flags.get(OPERATIONAL_FLAG_OFFSET));
70 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
72 return builder.build();
76 public void addTlv(final LspBuilder builder, final Tlv tlv) {
81 public byte[] serializeObject(final Object object) {
82 if (!(object instanceof LspObject)) {
83 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
86 final LspObject specObj = (LspObject) object;
88 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
90 final byte[] retBytes = new byte[0 + TLVS_OFFSET];
92 final int lspID = specObj.getPlspId().getValue().intValue();
93 retBytes[0] = (byte) (lspID >> 12);
94 retBytes[1] = (byte) (lspID >> 4);
95 retBytes[2] = (byte) (lspID << 4);
96 if (specObj.isDelegate()) {
97 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
100 // if (specObj.isOperational())
101 // retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
102 if (specObj.isRemove()) {
103 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
105 if (specObj.isSync()) {
106 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
109 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
115 public int getObjectType() {
120 public int getObjectClass() {