BUG-612 : switched ERO to ByteBuf.
[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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.ByteBufUtil;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
17 import org.opendaylight.protocol.pcep.spi.ObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.base.Preconditions;
25 import com.google.common.collect.Lists;
26 import com.google.common.primitives.UnsignedBytes;
27
28 public abstract class AbstractEROWithSubobjectsParser implements ObjectParser, ObjectSerializer {
29
30         private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
31
32         private static final int HEADER_LENGTH = 2;
33
34         private final EROSubobjectRegistry subobjReg;
35
36         protected AbstractEROWithSubobjectsParser(final EROSubobjectRegistry subobjReg) {
37                 this.subobjReg = Preconditions.checkNotNull(subobjReg);
38         }
39
40         protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
41                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42                 final List<Subobject> subs = new ArrayList<>();
43                 while (buffer.isReadable()) {
44                         boolean loose = ((buffer.getByte(buffer.readerIndex()) & (1 << 7)) != 0) ? true : false;
45                         int type = (buffer.readByte() & 0xff) & ~(1 << 7);
46                         int length = UnsignedBytes.toInt(buffer.readByte()) - HEADER_LENGTH;
47                         if (length > buffer.readableBytes()) {
48                                 throw new PCEPDeserializerException("Wrong length specified. Passed: " + length + "; Expected: <= "
49                                                 + buffer.readableBytes());
50                         }
51                         LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
52                         final Subobject sub = this.subobjReg.parseSubobject(type, buffer.slice(buffer.readerIndex(), length), loose);
53                         if (sub == null) {
54                                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
55                         } else {
56                                 LOG.debug("Subobject was parsed. {}", sub);
57                                 subs.add(sub);
58                         }
59                         buffer.readerIndex(buffer.readerIndex() + length);
60                 }
61                 return subs;
62         }
63
64         protected final byte[] serializeSubobject(final List<Subobject> subobjects) {
65                 final List<byte[]> result = Lists.newArrayList();
66                 int finalLength = 0;
67                 for (final Subobject subobject : subobjects) {
68                         final byte[] bytes = this.subobjReg.serializeSubobject(subobject);
69                         if (bytes == null) {
70                                 LOG.warn("Could not find serializer for subobject type: {}. Skipping subobject.", subobject.getSubobjectType());
71                         } else  {
72                                 finalLength += bytes.length;
73                                 result.add(bytes);
74                         }
75                 }
76                 final byte[] resultBytes = new byte[finalLength];
77                 int byteOffset = 0;
78                 for (final byte[] b : result) {
79                         System.arraycopy(b, 0, resultBytes, byteOffset, b.length);
80                         byteOffset += b.length;
81                 }
82                 return resultBytes;
83         }
84 }