Update MRI projects for Aluminium
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / EROSubobjectListParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.spi.subobjects;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectRegistry;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public abstract class EROSubobjectListParser extends AbstractRSVPObjectParser {
25     private static final Logger LOG = LoggerFactory.getLogger(EROSubobjectListParser.class);
26     private static final int HEADER_LENGTH = 2;
27     private final EROSubobjectRegistry subobjReg;
28
29     public EROSubobjectListParser(final EROSubobjectRegistry subobjReg) {
30         this.subobjReg = requireNonNull(subobjReg);
31     }
32
33     public List<SubobjectContainer> parseList(final ByteBuf buffer) throws RSVPParsingException {
34         // Explicit approval of empty ERO
35         Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
36         final List<SubobjectContainer> subs = new ArrayList<>();
37         while (buffer.isReadable()) {
38             final boolean loose = (buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET))
39                 != 0;
40             final int type = (buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~(1 << Values
41                 .FIRST_BIT_OFFSET);
42             final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
43             if (length > buffer.readableBytes()) {
44                 throw new RSVPParsingException("Wrong length specified. Passed: " + length + "; Expected: <= "
45                     + buffer.readableBytes());
46             }
47             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
48             final SubobjectContainer sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), loose);
49             if (sub == null) {
50                 LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type);
51             } else {
52                 LOG.debug("Subobject was parsed. {}", sub);
53                 subs.add(sub);
54             }
55         }
56         return subs;
57     }
58
59     public final void serializeList(final List<SubobjectContainer> subobjects, final ByteBuf buffer) {
60         Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(),
61             "RRO must contain at least one subobject.");
62         for (final SubobjectContainer subobject : subobjects) {
63             this.subobjReg.serializeSubobject(subobject, buffer);
64         }
65     }
66 }