2 * Copyright (c) 2014 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.ietf.stateful02;
10 import java.util.BitSet;
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.crabbe.stateful._02.rev140110.PlspId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.Lsp;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.LspBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.Tlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.object.lsp.TlvsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.rsvp.error.spec.tlv.RsvpErrorSpec;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.symbolic.path.name.tlv.SymbolicPathName;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
30 * Parser for {@link Lsp}
32 public final class Stateful02LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
34 public static final int CLASS = 32;
36 public static final int TYPE = 1;
39 * offset of TLVs offset of other fields are not defined as constants
40 * because of non-standard mapping of bits
42 private static final int TLVS_OFFSET = 4;
45 * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
47 private static final int DELEGATE_FLAG_OFFSET = 15;
48 private static final int SYNC_FLAG_OFFSET = 14;
49 private static final int REMOVE_FLAG_OFFSET = 12;
50 private static final int OPERATIONAL_FLAG_OFFSET = 13;
52 public Stateful02LspObjectParser(final TlvHandlerRegistry tlvReg) {
57 public Lsp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
58 if (bytes == null || bytes.length == 0) {
59 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
61 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
63 final LspBuilder builder = new LspBuilder();
64 builder.setIgnore(header.isIgnore());
65 builder.setProcessingRule(header.isProcessingRule());
67 builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
68 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
69 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
70 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
71 builder.setOperational(flags.get(OPERATIONAL_FLAG_OFFSET));
72 final TlvsBuilder b = new TlvsBuilder();
73 parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
74 builder.setTlvs(b.build());
75 return builder.build();
79 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
80 if (tlv instanceof RsvpErrorSpec) {
81 builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
82 } else if (tlv instanceof SymbolicPathName) {
83 builder.setSymbolicPathName((SymbolicPathName) tlv);
84 } else if (tlv instanceof LspDbVersion) {
85 builder.setLspDbVersion((LspDbVersion) tlv);
90 public byte[] serializeObject(final Object object) {
91 if (!(object instanceof Lsp)) {
92 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
94 final Lsp specObj = (Lsp) object;
96 final byte[] tlvs = serializeTlvs(specObj.getTlvs());
97 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
99 final int lspID = specObj.getPlspId().getValue().intValue();
100 retBytes[0] = (byte) (lspID >> 12);
101 retBytes[1] = (byte) (lspID >> 4);
102 retBytes[2] = (byte) (lspID << 4);
103 if (specObj.isDelegate() != null && specObj.isDelegate()) {
104 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
106 if (specObj.isRemove() != null && specObj.isRemove()) {
107 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
109 if (specObj.isSync() != null && specObj.isSync()) {
110 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
112 if (specObj.isOperational() != null && specObj.isOperational()) {
113 retBytes[3] |= 1 << (Byte.SIZE - (OPERATIONAL_FLAG_OFFSET - Byte.SIZE) - 1);
115 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
116 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
119 public byte[] serializeTlvs(final Tlvs tlvs) {
124 byte[] rsvpErrBytes = null;
125 byte[] symbBytes = null;
126 byte[] dbvBytes = null;
127 if (tlvs.getRsvpErrorSpec() != null) {
128 rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
129 finalLength += rsvpErrBytes.length;
131 if (tlvs.getSymbolicPathName() != null) {
132 symbBytes = serializeTlv(tlvs.getSymbolicPathName());
133 finalLength += symbBytes.length;
135 if (tlvs.getLspDbVersion() != null) {
136 dbvBytes = serializeTlv(tlvs.getLspDbVersion());
137 finalLength += dbvBytes.length;
140 final byte[] result = new byte[finalLength];
141 if (rsvpErrBytes != null) {
142 ByteArray.copyWhole(rsvpErrBytes, result, offset);
143 offset += rsvpErrBytes.length;
145 if (symbBytes != null) {
146 ByteArray.copyWhole(symbBytes, result, offset);
147 offset += symbBytes.length;
149 if (dbvBytes != null) {
150 ByteArray.copyWhole(dbvBytes, result, offset);
151 offset += dbvBytes.length;
157 public int getObjectType() {
162 public int getObjectClass() {