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;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.ByteBufUtil;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectRegistry;
19 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public abstract class RROSubobjectListParser extends AbstractRSVPObjectParser {
25 private static final Logger LOG = LoggerFactory.getLogger(RROSubobjectListParser.class);
26 private static final int HEADER_LENGTH = 2;
27 private final RROSubobjectRegistry subobjReg;
29 public RROSubobjectListParser(final RROSubobjectRegistry subobjReg) {
30 this.subobjReg = requireNonNull(subobjReg);
33 public List<SubobjectContainer> parseList(final ByteBuf buffer) throws RSVPParsingException {
34 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
35 "Array of bytes is mandatory. Can't be null or empty.");
36 final List<SubobjectContainer> subs = new ArrayList<>();
37 while (buffer.isReadable()) {
38 final int type = buffer.readUnsignedByte();
39 final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
40 if (length > buffer.readableBytes()) {
41 throw new RSVPParsingException("Wrong length specified. Passed: " + length + "; Expected: <= "
42 + buffer.readableBytes());
44 LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
45 final SubobjectContainer sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length));
47 LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type);
49 LOG.debug("Subobject was parsed. {}", sub);
56 public final void serializeList(final List<SubobjectContainer> subobjects, final ByteBuf buffer) {
57 Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(),
58 "RRO must contain at least one subobject.");
59 for (final SubobjectContainer subobject : subobjects) {
60 this.subobjReg.serializeSubobject(subobject, buffer);