2 * Copyright (c) 2013 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
8 package org.opendaylight.protocol.pcep.parser.subobject;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
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.subobject.type.LabelCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.LabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.Label;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.label._case.LabelBuilder;
27 public class RROLabelSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
29 public static final int TYPE = 3;
31 public static final int FLAGS_SIZE = 8;
33 public static final int C_TYPE_F_LENGTH = 1;
35 public static final int C_TYPE_F_OFFSET = FLAGS_SIZE / Byte.SIZE;
37 public static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
39 public static final int U_FLAG_OFFSET = 0;
40 public static final int G_FLAG_OFFSET = 7;
42 private final LabelRegistry registry;
44 public RROLabelSubobjectParser(final LabelRegistry labelReg) {
45 this.registry = Preconditions.checkNotNull(labelReg);
49 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
50 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
51 if (buffer.readableBytes() < HEADER_LENGTH) {
52 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
53 + HEADER_LENGTH + ".");
55 final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
57 final short cType = buffer.readUnsignedByte();
59 final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
60 if (labelType == null) {
61 throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
63 final LabelBuilder builder = new LabelBuilder();
64 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
65 builder.setGlobal(reserved.get(G_FLAG_OFFSET));
66 builder.setLabelType(labelType);
67 return new SubobjectBuilder().setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
71 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
72 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
73 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
74 final ByteBuf body = Unpooled.buffer();
75 this.registry.serializeLabel(label.isUniDirectional(), label.isGlobal(), label.getLabelType(), body);
76 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);