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.ietf.stateful07;
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.ietf.stateful.rev131222.OperationalStatus;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.error.code.tlv.LspErrorCode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.rsvp.error.spec.tlv.RsvpErrorSpec;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
31 import com.google.common.base.Preconditions;
34 * Parser for {@link Lsp}
36 public class Stateful07LspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38 public static final int CLASS = 32;
40 public static final int TYPE = 1;
43 * offset of TLVs offset of other fields are not defined as constants
44 * because of non-standard mapping of bits
46 protected static final int TLVS_OFFSET = 4;
49 * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
51 protected static final int DELEGATE_FLAG_OFFSET = 15;
52 protected static final int SYNC_FLAG_OFFSET = 14;
53 protected static final int REMOVE_FLAG_OFFSET = 13;
54 protected static final int ADMINISTRATIVE_FLAG_OFFSET = 12;
55 protected static final int OPERATIONAL_OFFSET = 9;
57 public Stateful07LspObjectParser(final TlvHandlerRegistry tlvReg) {
62 public Lsp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
63 if (bytes == null || bytes.length == 0) {
64 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
66 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
68 final LspBuilder builder = new LspBuilder();
69 builder.setIgnore(header.isIgnore());
70 builder.setProcessingRule(header.isProcessingRule());
72 builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
73 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
74 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
75 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
76 builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
78 s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
79 s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
80 s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
81 builder.setOperational(OperationalStatus.forValue(s));
82 final TlvsBuilder b = new TlvsBuilder();
83 parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
84 builder.setTlvs(b.build());
85 return builder.build();
89 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
90 if (tlv instanceof LspErrorCode) {
91 builder.setLspErrorCode((LspErrorCode) tlv);
92 } else if (tlv instanceof LspIdentifiers) {
93 builder.setLspIdentifiers((LspIdentifiers) tlv);
94 } else if (tlv instanceof RsvpErrorSpec) {
95 builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
96 } else if (tlv instanceof SymbolicPathName) {
97 builder.setSymbolicPathName((SymbolicPathName) tlv);
102 public byte[] serializeObject(final Object object) {
103 if (!(object instanceof Lsp)) {
104 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
106 final Lsp specObj = (Lsp) object;
108 final byte[] tlvs = serializeTlvs(specObj.getTlvs());
109 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
111 Preconditions.checkArgument(specObj.getPlspId() != null, "PLSP-ID not present");
112 final int lspID = specObj.getPlspId().getValue().intValue();
113 retBytes[0] = (byte) (lspID >> 12);
114 retBytes[1] = (byte) (lspID >> 4);
115 retBytes[2] = (byte) (lspID << 4);
116 if (specObj.isDelegate() != null && specObj.isDelegate()) {
117 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
119 if (specObj.isRemove() != null && specObj.isRemove()) {
120 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
122 if (specObj.isSync() != null && specObj.isSync()) {
123 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
125 if (specObj.isAdministrative() != null && specObj.isAdministrative()) {
126 retBytes[3] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
128 if (specObj.getOperational() != null) {
129 final int op = specObj.getOperational().getIntValue();
130 retBytes[3] |= (op & 7) << 4;
132 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
133 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
136 public byte[] serializeTlvs(final Tlvs tlvs) {
141 byte[] lspErrBytes = null;
142 byte[] lspIdBytes = null;
143 byte[] rsvpErrBytes = null;
144 byte[] symbBytes = null;
145 if (tlvs.getLspErrorCode() != null) {
146 lspErrBytes = serializeTlv(tlvs.getLspErrorCode());
147 finalLength += lspErrBytes.length;
149 if (tlvs.getLspIdentifiers() != null) {
150 lspIdBytes = serializeTlv(tlvs.getLspIdentifiers());
151 finalLength += lspIdBytes.length;
153 if (tlvs.getRsvpErrorSpec() != null) {
154 rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
155 finalLength += rsvpErrBytes.length;
157 if (tlvs.getSymbolicPathName() != null) {
158 symbBytes = serializeTlv(tlvs.getSymbolicPathName());
159 finalLength += symbBytes.length;
162 final byte[] result = new byte[finalLength];
163 if (lspErrBytes != null) {
164 ByteArray.copyWhole(lspErrBytes, result, offset);
165 offset += lspErrBytes.length;
167 if (lspIdBytes != null) {
168 ByteArray.copyWhole(lspIdBytes, result, offset);
169 offset += lspIdBytes.length;
171 if (rsvpErrBytes != null) {
172 ByteArray.copyWhole(rsvpErrBytes, result, offset);
173 offset += rsvpErrBytes.length;
175 if (symbBytes != null) {
176 ByteArray.copyWhole(symbBytes, result, offset);
177 offset += symbBytes.length;
183 public int getObjectType() {
188 public int getObjectClass() {