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.spi.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
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.RequestId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.SvecBuilder;
24 import com.google.common.collect.Lists;
27 * Parser for {@link Svec}
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 TlvRegistry tlvReg) {
64 public Svec parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
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 byte[] serializeObject(final Object object) {
95 if (!(object instanceof Svec)) {
96 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
99 final Svec svecObj = (Svec) object;
100 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
101 final List<RequestId> requestIDs = svecObj.getRequestsIds();
102 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
103 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
104 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
105 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
107 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
109 for (int i = 0; i < requestIDs.size(); i++) {
110 System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
111 * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
113 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
114 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);