BUG-8156 : conflicting listener fix
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / subobjects / RROSubobjectListParser.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
9 package org.opendaylight.protocol.rsvp.parser.spi.subobjects;
10
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;
21
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;
26
27     public RROSubobjectListParser(final RROSubobjectRegistry subobjReg) {
28         this.subobjReg = Preconditions.checkNotNull(subobjReg);
29     }
30
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());
41             }
42             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
43             final SubobjectContainer sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length));
44             if (sub == null) {
45                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
46             } else {
47                 LOG.debug("Subobject was parsed. {}", sub);
48                 subs.add(sub);
49             }
50         }
51         return subs;
52     }
53
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);
59         }
60     }
61 }