BUG-50 : adjusted ERO object.
[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.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 import com.google.common.primitives.UnsignedBytes;
25
26 public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
27
28         private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
29
30         private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
31         private static final int SUB_LENGTH_F_LENGTH = 1;
32         private static final int SUB_HEADER_LENGTH = SUB_TYPE_FLAG_F_LENGTH + SUB_LENGTH_F_LENGTH;
33
34         private static final int TYPE_FLAG_F_OFFSET = 0;
35         private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
36         private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
37
38         private final EROSubobjectHandlerRegistry subobjReg;
39
40         protected AbstractEROWithSubobjectsParser(final EROSubobjectHandlerRegistry subobjReg) {
41                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
42         }
43
44         protected List<Subobjects> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
45                 if (bytes == null) {
46                         throw new IllegalArgumentException("Byte array is mandatory.");
47                 }
48
49                 boolean loose = false;
50                 int type;
51                 byte[] soContentsBytes;
52                 int length;
53                 int offset = 0;
54
55                 final List<Subobjects> subs = Lists.newArrayList();
56
57                 while (offset < bytes.length) {
58
59                         loose = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
60                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
61
62                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
63
64                         if (length > bytes.length - offset) {
65                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
66                                                 + (bytes.length - offset));
67                         }
68
69                         soContentsBytes = new byte[length - SO_CONTENTS_OFFSET];
70                         System.arraycopy(bytes, offset + SO_CONTENTS_OFFSET, soContentsBytes, 0, length - SO_CONTENTS_OFFSET);
71
72                         LOG.debug("Attempt to parse subobject from bytes: {}", ByteArray.bytesToHexString(soContentsBytes));
73                         final Subobjects sub = this.subobjReg.getSubobjectParser(type).parseSubobject(soContentsBytes, loose);
74                         LOG.debug("Subobject was parsed. {}", sub);
75
76                         subs.add(sub);
77
78                         offset += length;
79                 }
80                 return subs;
81         }
82
83         protected final byte[] serializeSubobject(final List<Subobjects> subobjects) {
84
85                 final List<byte[]> result = Lists.newArrayList();
86
87                 int finalLength = 0;
88
89                 for (final Subobjects subobject : subobjects) {
90
91                         final EROSubobjectSerializer serializer = this.subobjReg.getSubobjectSerializer(subobject.getSubobjectType());
92
93                         final byte[] valueBytes = serializer.serializeSubobject(subobject);
94
95                         final byte[] bytes = new byte[SUB_HEADER_LENGTH + valueBytes.length];
96
97                         final byte typeBytes = (byte) (UnsignedBytes.checkedCast(serializer.getType()) | (subobject.isLoose() ? 1 << 7 : 0));
98                         final byte lengthBytes = UnsignedBytes.checkedCast(valueBytes.length + SUB_HEADER_LENGTH);
99
100                         bytes[0] = typeBytes;
101                         bytes[1] = lengthBytes;
102                         System.arraycopy(valueBytes, 0, bytes, SUB_HEADER_LENGTH, valueBytes.length);
103
104                         finalLength += bytes.length;
105                         result.add(bytes);
106                 }
107
108                 final byte[] resultBytes = new byte[finalLength];
109                 int byteOffset = 0;
110                 for (final byte[] b : result) {
111                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
112                         byteOffset += b.length;
113                 }
114                 return resultBytes;
115         }
116 }