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