Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / AbstractRROWithSubobjectsParser.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.RROSubobjectRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public abstract class AbstractRROWithSubobjectsParser extends CommonObjectParser implements ObjectSerializer {
26
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractRROWithSubobjectsParser.class);
28
29     private final RROSubobjectRegistry subobjReg;
30
31     private static final int HEADER_LENGTH = 2;
32
33     protected AbstractRROWithSubobjectsParser(final RROSubobjectRegistry subobjReg, final int objectClass,
34             final int objectType) {
35         super(objectClass, objectType);
36         this.subobjReg = requireNonNull(subobjReg);
37     }
38
39     protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException {
40         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
41                 "Array of bytes is mandatory. Can't be null or empty.");
42         final List<Subobject> subs = new ArrayList<>();
43         while (buffer.isReadable()) {
44             final int type = buffer.readUnsignedByte();
45             final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
46             if (length > buffer.readableBytes()) {
47                 throw new PCEPDeserializerException(
48                         "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
49             }
50             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
51             final Subobject sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length));
52             if (sub == null) {
53                 LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type);
54             } else {
55                 LOG.debug("Subobject was parsed. {}", sub);
56                 subs.add(sub);
57             }
58         }
59         return subs;
60     }
61
62     protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
63         Preconditions.checkArgument(subobjects != null && !subobjects.isEmpty(),
64                 "RRO must contain at least one subobject.");
65         for (final Subobject subobject : subobjects) {
66             this.subobjReg.serializeSubobject(subobject, buffer);
67         }
68     }
69 }