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.AbstractObjectParser;
16 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.SvecBuilder;
26 import com.google.common.collect.Lists;
29 * Parser for {@link SvecObject}
31 public class PCEPSvecObjectParser extends AbstractObjectParser<SvecBuilder> {
33 public static final int CLASS = 11;
35 public static final int TYPE = 1;
38 * field lengths in bytes
40 public static final int FLAGS_F_LENGTH = 3;
41 public static final int REQ_LIST_ITEM_LENGTH = 4;
44 * fields offsets in bytes
46 public static final int FLAGS_F_OFFSET = 1; // aded reserved field of size 1
47 public static final int REQ_ID_LIST_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
50 * flags offsets inside flags field in bits
52 public static final int S_FLAG_OFFSET = 21;
53 public static final int N_FLAG_OFFSET = 22;
54 public static final int L_FLAG_OFFSET = 23;
59 public static final int MIN_SIZE = FLAGS_F_LENGTH + FLAGS_F_OFFSET;
61 public PCEPSvecObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
62 super(subobjReg, tlvReg);
66 public SvecObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
67 if (bytes == null || bytes.length == 0) {
68 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
71 if (bytes.length < MIN_SIZE) {
72 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: >=" + MIN_SIZE
76 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.subByte(bytes, FLAGS_F_OFFSET, FLAGS_F_LENGTH));
77 final List<RequestId> requestIDs = Lists.newArrayList();
79 for (int i = REQ_ID_LIST_OFFSET; i < bytes.length; i += REQ_LIST_ITEM_LENGTH) {
80 requestIDs.add(new RequestId(ByteArray.bytesToLong(ByteArray.subByte(bytes, i, REQ_LIST_ITEM_LENGTH))));
83 if (requestIDs.isEmpty()) {
84 throw new PCEPDeserializerException("Empty Svec Object - no request ids.");
87 final SvecBuilder builder = new SvecBuilder();
89 builder.setIgnore(header.isIgnore());
90 builder.setProcessingRule(header.isProcessingRule());
92 builder.setLinkDiverse(flags.get(L_FLAG_OFFSET));
93 builder.setNodeDiverse(flags.get(N_FLAG_OFFSET));
94 builder.setSrlgDiverse(flags.get(S_FLAG_OFFSET));
95 builder.setRequestsIds(requestIDs);
96 return builder.build();
100 public void addTlv(final SvecBuilder builder, final Tlv tlv) {
105 public byte[] serializeObject(final Object object) {
106 if (!(object instanceof SvecObject)) {
107 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed SvecObject.");
110 final SvecObject svecObj = (SvecObject) object;
111 final byte[] retBytes = new byte[svecObj.getRequestsIds().size() * REQ_LIST_ITEM_LENGTH + REQ_ID_LIST_OFFSET];
112 final List<RequestId> requestIDs = svecObj.getRequestsIds();
113 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
114 flags.set(L_FLAG_OFFSET, svecObj.isLinkDiverse());
115 flags.set(N_FLAG_OFFSET, svecObj.isNodeDiverse());
116 flags.set(S_FLAG_OFFSET, svecObj.isSrlgDiverse());
118 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
120 for (int i = 0; i < requestIDs.size(); i++) {
121 System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
122 + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
125 assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
131 public int getObjectType() {
136 public int getObjectClass() {