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.List;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.ObjectParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Lists;
25 public abstract class AbstractXROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
27 private static final Logger logger = LoggerFactory.getLogger(AbstractXROWithSubobjectsParser.class);
29 private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
30 private static final int SUB_LENGTH_F_LENGTH = 1;
31 private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
33 public static final int TYPE_FLAG_F_OFFSET = 0;
34 public static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
35 public static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
37 protected static final int PADDED_TO = 4;
39 private final XROSubobjectHandlerRegistry subobjReg;
41 protected AbstractXROWithSubobjectsParser(final XROSubobjectHandlerRegistry subobjReg) {
42 this.subobjReg = Preconditions.checkNotNull(subobjReg);
45 protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
47 throw new IllegalArgumentException("Byte array is mandatory.");
52 byte[] soContentsBytes;
56 final List<Subobjects> subs = Lists.newArrayList();
58 while (offset < bytes.length) {
60 length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
62 final boolean mandatory = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
63 type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
65 if (length > bytes.length - offset) {
66 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
67 + (bytes.length - offset));
70 soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
71 System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
73 logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
74 final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, mandatory);
75 logger.debug("Subobject was parsed. {}", sub);
84 protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
86 final List<byte[]> result = Lists.newArrayList();
90 for (final Subobjects subobject : subobjects) {
92 final XROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject);
94 final byte[] valueBytes = serializer.serializeSubobject(subobject);
96 final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
98 final byte typeBytes = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - 1)[0] | (subobject.isMandatory() ? 1 << 7
100 final byte lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - 1)[0];
102 bytes[0] = typeBytes;
103 bytes[1] = lengthBytes;
104 System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
106 finalLength += bytes.length;
110 final byte[] resultBytes = new byte[finalLength];
112 for (final byte[] b : result) {
113 System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
114 byteOffset += b.length;