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.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OperationalStatus;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PlspId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.error.code.tlv.LspErrorCode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.identifiers.tlv.LspIdentifiers;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.Lsp;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.LspBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.lsp.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.object.lsp.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rsvp.error.spec.tlv.RsvpErrorSpec;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.symbolic.path.name.tlv.SymbolicPathName;
32 * Parser for {@link LspObject}
34 public class PCEPLspObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
36 public static final int CLASS = 32;
38 public static final int TYPE = 1;
41 * offset of TLVs offset of other fields are not defined as constants
42 * because of non-standard mapping of bits
44 private static final int TLVS_OFFSET = 4;
47 * 12b extended to 16b so first 4b are restricted (belongs to LSP ID)
49 private static final int DELEGATE_FLAG_OFFSET = 15;
50 private static final int SYNC_FLAG_OFFSET = 14;
51 private static final int REMOVE_FLAG_OFFSET = 13;
52 private static final int ADMINISTRATIVE_FLAG_OFFSET = 12;
53 private static final int OPERATIONAL_OFFSET = 9;
55 public PCEPLspObjectParser(final TlvHandlerRegistry tlvReg) {
60 public Lsp parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
61 if (bytes == null || bytes.length == 0) {
62 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
64 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, 2, 2));
66 final LspBuilder builder = new LspBuilder();
67 builder.setIgnore(header.isIgnore());
68 builder.setProcessingRule(header.isProcessingRule());
70 builder.setPlspId(new PlspId((ByteArray.bytesToLong(ByteArray.subByte(bytes, 0, 2)) & 0xFFFF) << 4 | (bytes[2] & 0xFF) >> 4));
71 builder.setDelegate(flags.get(DELEGATE_FLAG_OFFSET));
72 builder.setSync(flags.get(SYNC_FLAG_OFFSET));
73 builder.setRemove(flags.get(REMOVE_FLAG_OFFSET));
74 builder.setAdministrative(flags.get(ADMINISTRATIVE_FLAG_OFFSET));
76 s |= flags.get(OPERATIONAL_OFFSET + 2) ? 1 : 0;
77 s |= (flags.get(OPERATIONAL_OFFSET + 1) ? 1 : 0) << 1;
78 s |= (flags.get(OPERATIONAL_OFFSET) ? 1 : 0) << 2;
79 builder.setOperational(OperationalStatus.forValue(s));
80 final TlvsBuilder b = new TlvsBuilder();
81 parseTlvs(b, ByteArray.cutBytes(bytes, TLVS_OFFSET));
82 builder.setTlvs(b.build());
83 return builder.build();
87 public void addTlv(final TlvsBuilder builder, final Tlv tlv) {
88 if (tlv instanceof LspErrorCode) {
89 builder.setLspErrorCode((LspErrorCode) tlv);
90 } else if (tlv instanceof LspIdentifiers) {
91 builder.setLspIdentifiers((LspIdentifiers) tlv);
92 } else if (tlv instanceof RsvpErrorSpec) {
93 builder.setRsvpErrorSpec((RsvpErrorSpec) tlv);
94 } else if (tlv instanceof SymbolicPathName) {
95 builder.setSymbolicPathName((SymbolicPathName) tlv);
100 public byte[] serializeObject(final Object object) {
101 if (!(object instanceof Lsp)) {
102 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed LspObject.");
104 final Lsp specObj = (Lsp) object;
106 final byte[] tlvs = serializeTlvs(specObj.getTlvs());
107 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
109 final int lspID = specObj.getPlspId().getValue().intValue();
110 retBytes[0] = (byte) (lspID >> 12);
111 retBytes[1] = (byte) (lspID >> 4);
112 retBytes[2] = (byte) (lspID << 4);
113 if (specObj.isDelegate()) {
114 retBytes[3] |= 1 << (Byte.SIZE - (DELEGATE_FLAG_OFFSET - Byte.SIZE) - 1);
116 if (specObj.isRemove()) {
117 retBytes[3] |= 1 << (Byte.SIZE - (REMOVE_FLAG_OFFSET - Byte.SIZE) - 1);
119 if (specObj.isSync()) {
120 retBytes[3] |= 1 << (Byte.SIZE - (SYNC_FLAG_OFFSET - Byte.SIZE) - 1);
122 if (specObj.isAdministrative()) {
123 retBytes[3] |= 1 << (Byte.SIZE - (ADMINISTRATIVE_FLAG_OFFSET - Byte.SIZE) - 1);
125 final int op = specObj.getOperational().getIntValue();
126 retBytes[3] |= (op & 7) << 4;
127 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
131 public byte[] serializeTlvs(final Tlvs tlvs) {
136 byte[] lspErrBytes = null;
137 byte[] lspIdBytes = null;
138 byte[] rsvpErrBytes = null;
139 byte[] symbBytes = null;
140 if (tlvs.getLspErrorCode() != null) {
141 lspErrBytes = serializeTlv(tlvs.getLspErrorCode());
142 finalLength += lspErrBytes.length;
144 if (tlvs.getLspIdentifiers() != null) {
145 lspIdBytes = serializeTlv(tlvs.getLspIdentifiers());
146 finalLength += lspIdBytes.length;
148 if (tlvs.getRsvpErrorSpec() != null) {
149 rsvpErrBytes = serializeTlv(tlvs.getRsvpErrorSpec());
150 finalLength += rsvpErrBytes.length;
152 if (tlvs.getSymbolicPathName() != null) {
153 symbBytes = serializeTlv(tlvs.getSymbolicPathName());
154 finalLength += symbBytes.length;
157 final byte[] result = new byte[finalLength];
158 if (lspErrBytes != null) {
159 ByteArray.copyWhole(lspErrBytes, result, offset);
160 offset += lspErrBytes.length;
162 if (lspIdBytes != null) {
163 ByteArray.copyWhole(lspIdBytes, result, offset);
164 offset += lspIdBytes.length;
166 if (rsvpErrBytes != null) {
167 ByteArray.copyWhole(rsvpErrBytes, result, offset);
168 offset += rsvpErrBytes.length;
170 if (symbBytes != null) {
171 ByteArray.copyWhole(symbBytes, result, offset);
172 offset += symbBytes.length;
178 public int getObjectType() {
183 public int getObjectClass() {