BUG-47: more subobject models
[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.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectHandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.ObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
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.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 logger = 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         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;
36
37         protected static final int PADDED_TO = 4;
38
39         private final EROSubobjectHandlerRegistry subobjReg;
40
41         protected AbstractEROWithSubobjectsParser(final EROSubobjectHandlerRegistry subobjReg) {
42                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
43         }
44
45         protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
46                 if (bytes == null) {
47                         throw new IllegalArgumentException("Byte array is mandatory.");
48                 }
49
50                 boolean loose_flag = false;
51                 int type;
52
53                 byte[] soContentsBytes;
54                 int length;
55                 int offset = 0;
56
57                 final List<Subobjects> subs = Lists.newArrayList();
58
59                 while (offset < bytes.length) {
60
61                         loose_flag = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
62                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
63
64                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
65
66                         if (length > bytes.length - offset) {
67                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
68                                                 + (bytes.length - offset));
69                         }
70
71                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
72                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
73
74                         logger.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
75                         final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, loose_flag);
76                         logger.debug("Subobject was parsed. {}", sub);
77
78                         subs.add(sub);
79
80                         offset += length;
81                 }
82                 return subs;
83         }
84
85         protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
86
87                 final List<byte[]> result = Lists.newArrayList();
88
89                 int finalLength = 0;
90
91                 for (final Subobjects subobject : subobjects) {
92
93                         final EROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject);
94
95                         final byte[] valueBytes = serializer.serializeSubobject(subobject);
96
97                         final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
98
99                         final byte typeBytes = (byte) (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - 1)[0] | (subobject.isLoose() ? 1 << 7
100                                         : 0));
101                         final byte lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - 1)[0];
102
103                         bytes[0] = typeBytes;
104                         bytes[1] = lengthBytes;
105                         System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
106
107                         finalLength += bytes.length;
108                         result.add(bytes);
109                 }
110
111                 final byte[] resultBytes = new byte[finalLength];
112                 int byteOffset = 0;
113                 for (final byte[] b : result) {
114                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
115                         byteOffset += b.length;
116                 }
117                 return resultBytes;
118         }
119 }