BUG-130 : introduced RROSubobjectUtil.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / AbstractRROWithSubobjectsParser.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.ObjectParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.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 AbstractRROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
26
27         private static final Logger LOG = LoggerFactory.getLogger(AbstractRROWithSubobjectsParser.class);
28
29         private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
30         private static final int SUB_LENGTH_F_LENGTH = 1;
31
32         private static final int TYPE_FLAG_F_OFFSET = 0;
33         private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
34         private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
35
36         private final RROSubobjectHandlerRegistry subobjReg;
37
38         protected AbstractRROWithSubobjectsParser(final RROSubobjectHandlerRegistry subobjReg) {
39                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
40         }
41
42         protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
43                 if (bytes == null) {
44                         throw new IllegalArgumentException("Byte array is mandatory.");
45                 }
46
47                 int type;
48
49                 byte[] soContentsBytes;
50                 int length;
51                 int offset = 0;
52
53                 final List<Subobjects> subs = Lists.newArrayList();
54
55                 while (offset < bytes.length) {
56
57                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
58
59                         type = bytes[offset + TYPE_FLAG_F_OFFSET];
60
61                         if (length > bytes.length - offset) {
62                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
63                                                 + (bytes.length - offset));
64                         }
65
66                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
67                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
68
69                         LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
70                         final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes);
71                         LOG.debug("Subobject was parsed. {}", sub);
72
73                         subs.add(sub);
74
75                         offset += length;
76                 }
77                 return subs;
78         }
79
80         protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
81
82                 final List<byte[]> result = Lists.newArrayList();
83
84                 int finalLength = 0;
85
86                 for (final Subobjects subobject : subobjects) {
87
88                         final RROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType());
89
90                         final byte[] bytes = serializer.serializeSubobject(subobject);
91                         finalLength += bytes.length;
92                         result.add(bytes);
93                 }
94
95                 final byte[] resultBytes = new byte[finalLength];
96                 int byteOffset = 0;
97                 for (final byte[] b : result) {
98                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
99                         byteOffset += b.length;
100                 }
101                 return resultBytes;
102         }
103 }