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