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