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