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 com.google.common.primitives.UnsignedBytes;
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.RSVPParsingException;
20 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectRegistry;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class XROSubobjectListParser extends AbstractRSVPObjectParser {
27 private static final Logger LOG = LoggerFactory.getLogger(XROSubobjectListParser.class);
28 private static final short HEADER_LENGHT = 2;
29 private final XROSubobjectRegistry subobjReg;
31 protected XROSubobjectListParser(final XROSubobjectRegistry subobjReg) {
32 this.subobjReg = requireNonNull(subobjReg);
35 public List<SubobjectContainer> parseList(final ByteBuf byteBuf) throws RSVPParsingException {
36 final List<SubobjectContainer> subs = new ArrayList<>();
37 while (byteBuf.isReadable()) {
38 final boolean mandatory = (byteBuf.getUnsignedByte(byteBuf.readerIndex()) & (1 << Values
39 .FIRST_BIT_OFFSET)) != 0;
40 final int type = UnsignedBytes.checkedCast((byteBuf.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~
41 (1 << Values.FIRST_BIT_OFFSET));
42 final int length = byteBuf.readUnsignedByte() - HEADER_LENGHT;
43 if (length > byteBuf.readableBytes()) {
44 throw new RSVPParsingException("Wrong length specified. Passed: " + length
45 + "; Expected: <= " + byteBuf.readableBytes());
47 LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(byteBuf));
48 final SubobjectContainer sub = this.subobjReg.parseSubobject(type, byteBuf.readSlice(length), mandatory);
51 LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type);
53 LOG.debug("Subobject was parsed. {}", sub);
60 public void serializeList(final List<SubobjectContainer> subobjects, final ByteBuf buffer) {
61 Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(),
62 "XRO must contain at least one subobject.");
63 for (final SubobjectContainer subobject : subobjects) {
64 this.subobjReg.serializeSubobject(subobject, buffer);