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