Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / AbstractEROWithSubobjectsParser.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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
20 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public abstract class AbstractEROWithSubobjectsParser extends CommonObjectParser implements ObjectSerializer {
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class);
28
29     private static final int HEADER_LENGTH = 2;
30
31     private final EROSubobjectRegistry subobjReg;
32
33     protected AbstractEROWithSubobjectsParser(final EROSubobjectRegistry 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         // Explicit approval of empty ERO
41         checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null.");
42         final List<Subobject> subs = new ArrayList<>();
43         while (buffer.isReadable()) {
44             final boolean loose = (buffer.getUnsignedByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0;
45             final int type = buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET);
46             final int length = buffer.readUnsignedByte() - HEADER_LENGTH;
47             if (length > buffer.readableBytes()) {
48                 throw new PCEPDeserializerException(
49                         "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes());
50             }
51             LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer));
52             final Subobject sub = subobjReg.parseSubobject(type, buffer.readSlice(length), loose);
53             if (sub == null) {
54                 LOG.warn("Parsing failed for subobject type: {}. Ignoring subobject.", type);
55             } else {
56                 LOG.debug("Subobject was parsed. {}", sub);
57                 subs.add(sub);
58             }
59         }
60         return subs;
61     }
62
63     protected final void serializeSubobject(final List<Subobject> subobjects, final ByteBuf buffer) {
64         if (subobjects != null) {
65             for (final Subobject subobject : subobjects) {
66                 subobjReg.serializeSubobject(subobject, buffer);
67             }
68         }
69     }
70 }