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.spi.AbstractObjectParser;
15 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
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 AbstractObjectParser<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 HandlerRegistry registry) {
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.");
55 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
57 final LspBuilder builder = new LspBuilder();
59 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
61 builder.setIgnore(header.isIgnore());
62 builder.setProcessingRule(header.isProcessingRule());
64 // builder.setPlspId(new PlspId(ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2]
66 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
67 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
68 // builder.setOperational(Operational.flags.get(OPERATIONAL_FLAG_OFFSET));
69 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
71 return builder.build();
75 public void addTlv(final LspBuilder builder, final Tlv tlv) {
80 public byte[] serializeObject(final Object object) {
81 if (!(object instanceof LspObject))
82 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
84 final LspObject specObj = (LspObject) object;
86 // final byte[] tlvs = PCEPTlvParser.put(specObj.getTlvs());
88 final byte[] retBytes = new byte[0 + TLVS_OFFSET];
90 final int lspID = specObj.getPlspId().getValue().intValue();
91 retBytes[0] = (byte) (lspID >> 12);
92 retBytes[1] = (byte) (lspID >> 4);
93 retBytes[2] = (byte) (lspID << 4);
94 if (specObj.isDelegate())
95 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
97 // if (specObj.isOperational())
98 // retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
99 if (specObj.isRemove())
100 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
101 if (specObj.isSync())
102 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
104 // ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
110 public int getObjectType() {
115 public int getObjectClass() {