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;
11 import java.util.List;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
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.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.RequestId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
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.pcreq.message.pcreq.message.SvecBuilder;
24 import com.google.common.collect.Lists;
27 * Parser for {@link SvecObject}
29 public class PCEPSvecObjectParser extends AbstractObjectWithTlvsParser<SvecBuilder> {
31 public static final int CLASS = 11;
33 public static final int TYPE = 1;
36 * field lengths in bytes
38 private static final int FLAGS_F_LENGTH = 3;
39 private static final int REQ_LIST_ITEM_LENGTH = 4;
42 * fields offsets in bytes
44 private static final int FLAGS_F_OFFSET = 1;
45 private static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
48 * flags offsets inside flags field in bits
50 private static final int S_FLAG_OFFSET = 21;
51 private static final int N_FLAG_OFFSET = 22;
52 private static final int L_FLAG_OFFSET = 23;
57 private static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
59 public PCEPSvecObjectParser(final TlvHandlerRegistry tlvReg) {
64 public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
65 if (bytes == null || bytes.length == 0) {
66 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
68 if (bytes.length < MIN_SIZE) {
69 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
72 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
73 final List<RequestId> requestIDs = Lists.newArrayList();
75 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
76 requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
78 if (requestIDs.isEmpty()) {
79 throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
81 final SvecBuilder builder = new SvecBuilder();
83 builder.setIgnore(header.isIgnore());
84 builder.setProcessingRule(header.isProcessingRule());
86 builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
87 builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
88 builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
89 builder.setRequestsIds(requestIDs);
90 return builder.build();
94 public void addTlv(final SvecBuilder builder, final Tlv tlv) {
99 public byte[] serializeObject(final Object object) {
100 if (!(object instanceof SvecObject)) {
101 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
104 final SvecObject svecObj = (SvecObject) object;
105 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
106 final List<RequestId> requestIDs = svecObj.getRequestsIds();
107 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
108 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
109 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
110 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
112 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
114 for (int i = 0; i < requestIDs.size(); i++) {
115 System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
116 + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
118 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
123 public int getObjectType() {
128 public int getObjectClass() {