BUG-64 : initial rewrite, EROSubobjectRegistry.
[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.EROSubobjectRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectParser;
14 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.Lists;
23
24 public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
25
26         private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
27
28         private static final int SUB_TYPE_FLAG_F_LENGTH = 1;
29         private static final int SUB_LENGTH_F_LENGTH = 1;
30
31         private static final int TYPE_FLAG_F_OFFSET = 0;
32         private static final int LENGTH_F_OFFSET = TYPE_FLAG_F_OFFSET + SUB_TYPE_FLAG_F_LENGTH;
33         private static final int SO_CONTENTS_OFFSET = LENGTH_F_OFFSET + SUB_LENGTH_F_LENGTH;
34
35         private final EROSubobjectRegistry subobjReg;
36
37         protected AbstractEROWithSubobjectsParser(final EROSubobjectRegistry subobjReg) {
38                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
39         }
40
41         protected List<Subobject> parseSubobjects(final byte[] bytes) throws PCEPDeserializerException {
42                 if (bytes == null) {
43                         throw new IllegalArgumentException("Byte array is mandatory.");
44                 }
45
46                 boolean loose = false;
47                 int type;
48                 byte[] soContentsBytes;
49                 int length;
50                 int offset = 0;
51
52                 final List<Subobject> subs = Lists.newArrayList();
53
54                 while (offset < bytes.length) {
55
56                         loose = ((bytes[offset + TYPE_FLAG_F_OFFSET] & (1 << 7)) != 0) ? true : false;
57                         length = ByteArray.bytesToInt(ByteArray.subByte(bytes, offset + LENGTH_F_OFFSET, SUB_LENGTH_F_LENGTH));
58
59                         type = (bytes[offset + TYPE_FLAG_F_OFFSET] & 0xff) & ~(1 << 7);
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 Subobject sub = this.subobjReg.parseSubobject(type, soContentsBytes, loose);
71                         if (sub == null) {
72                                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
73                         } else {
74                                 LOG.debug("Subobject was parsed. {}", sub);
75                                 subs.add(sub);
76                         }
77                         offset += length;
78                 }
79                 return subs;
80         }
81
82         protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
83                 final List<byte[]> result = Lists.newArrayList();
84                 int finalLength = 0;
85                 for (final Subobject subobject : subobjects) {
86                         final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
87                         if (bytes == null) {
88                                 LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
89                         } else  {
90                                 finalLength += bytes.length;
91                                 result.add(bytes);
92                         }
93                 }
94                 final byte[] resultBytes = new byte[finalLength];
95                 int byteOffset = 0;
96                 for (final byte[] b : result) {
97                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
98                         byteOffset += b.length;
99                 }
100                 return resultBytes;
101         }
102 }