Merge "BUG-130: introduced EROSubobjectUtil."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / AbstractEROWithSubobjectsParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.object;
9
10 import java.util.List;
11
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.ObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobjects;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.Preconditions;
23 import com.google.common.collect.Lists;
24
25 public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
26
27         private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
28
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;
32
33         private static final int TYPE_FLAG_F_OFFSET = 0;
34         private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
35         private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
36
37         private final EROSubobjectHandlerRegistry subobjReg;
38
39         protected AbstractEROWithSubobjectsParser(final EROSubobjectHandlerRegistry subobjReg) {
40                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
41         }
42
43         protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
44                 if (bytes == null) {
45                         throw new IllegalArgumentException("Byte array is mandatory.");
46                 }
47
48                 boolean loose = false;
49                 int type;
50                 byte[] soContentsBytes;
51                 int length;
52                 int offset = 0;
53
54                 final List<Subobjects> subs = Lists.newArrayList();
55
56                 while (offset < bytes.length) {
57
58                         loose = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
59                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
60
61                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
62
63                         if (length > bytes.length - offset) {
64                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
65                                                 + (bytes.length - offset));
66                         }
67
68                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
69                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
70
71                         LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
72                         final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, loose);
73                         LOG.debug("Subobject was parsed. {}", sub);
74
75                         subs.add(sub);
76
77                         offset += length;
78                 }
79                 return subs;
80         }
81
82         protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
83
84                 final List<byte[]> result = Lists.newArrayList();
85
86                 int finalLength = 0;
87
88                 for (final Subobjects subobject : subobjects) {
89
90                         final EROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType());
91
92                         final byte[] bytes = serializer.serializeSubobject(subobject);
93
94                         finalLength += bytes.length;
95                         result.add(bytes);
96                 }
97
98                 final byte[] resultBytes = new byte[finalLength];
99                 int byteOffset = 0;
100                 for (final byte[] b : result) {
101                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
102                         byteOffset += b.length;
103                 }
104                 return resultBytes;
105         }
106 }