2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.rsvp.parser.impl.subobject.rro;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.rsvp.parser.spi.LabelRegistry;
17 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
18 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
19 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
20 import org.opendaylight.protocol.util.BitArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.Label;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32 private static final Logger LOG = LoggerFactory.getLogger(RROLabelSubobjectParser.class);
34 public static final int TYPE = 3;
36 public static final int FLAGS_SIZE = 8;
38 public static final int C_TYPE_F_LENGTH = 1;
40 public static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
42 public static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
44 public static final int U_FLAG_OFFSET = 0;
45 public static final int G_FLAG_OFFSET = 7;
47 private final LabelRegistry registry;
49 public RROLabelSubobjectParser(final LabelRegistry labelReg) {
50 this.registry = requireNonNull(labelReg);
54 public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
55 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
56 "Array of bytes is mandatory. Can't be null or empty.");
57 if (buffer.readableBytes() < HEADER_LENGTH) {
58 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; "
59 + "Expected: >" + HEADER_LENGTH + ".");
61 final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
63 final short cType = buffer.readUnsignedByte();
65 final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
66 if (labelType == null) {
67 LOG.warn("Ignoring RRO label subobject with unknown C-TYPE: {}", cType);
70 final LabelBuilder builder = new LabelBuilder();
71 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
72 builder.setGlobal(reserved.get(G_FLAG_OFFSET));
73 builder.setLabelType(labelType);
74 return new SubobjectContainerBuilder().setSubobjectType(new LabelCaseBuilder().setLabel(builder.build())
79 public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
80 requireNonNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
81 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
82 final ByteBuf body = Unpooled.buffer();
83 this.registry.serializeLabel(label.isUniDirectional(), label.isGlobal(), label.getLabelType(), body);
84 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);