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.impl.subobject;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
15 import java.util.BitSet;
17 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
18 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
19 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
20 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
21 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
31 public class EROLabelSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33 public static final int TYPE = 3;
35 private static final int RES_F_LENGTH = 1;
37 private static final int C_TYPE_F_LENGTH = 1;
39 private static final int RES_F_OFFSET = 0;
41 private static final int C_TYPE_F_OFFSET = RES_F_OFFSET + RES_F_LENGTH;
43 private static final int HEADER_LENGTH = C_TYPE_F_OFFSET + C_TYPE_F_LENGTH;
45 private static final int U_FLAG_OFFSET = 0;
47 private final LabelRegistry registry;
49 public EROLabelSubobjectParser(final LabelRegistry labelReg) {
50 this.registry = Preconditions.checkNotNull(labelReg);
54 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
55 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
56 if (buffer.readableBytes() < HEADER_LENGTH) {
57 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: >"
58 + HEADER_LENGTH + ".");
60 final BitSet reserved = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, RES_F_LENGTH));
61 final short cType = (short) UnsignedBytes.toInt(buffer.readByte());
63 final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
64 if (labelType == null) {
65 throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
67 final LabelBuilder builder = new LabelBuilder();
68 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
69 builder.setLabelType(labelType);
70 return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
74 public byte[] serializeSubobject(final Subobject subobject) {
75 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
76 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
77 final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
78 if (labelbytes == null) {
79 throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
80 + label.getLabelType().getImplementedInterface());
82 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);