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 io.netty.buffer.ByteBuf;
12 import java.util.BitSet;
14 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.LabelCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.Label;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.label._case.LabelBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
28 import com.google.common.base.Preconditions;
29 import com.google.common.primitives.UnsignedBytes;
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 //FIXME: switch to ByteBuf
64 final LabelType labelType = this.registry.parseLabel(cType, ByteArray.readAllBytes(buffer));
65 if (labelType == null) {
66 throw new PCEPDeserializerException("Unknown C-TYPE for ero label subobject. Passed: " + cType);
68 final LabelBuilder builder = new LabelBuilder();
69 builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
70 builder.setLabelType(labelType);
71 return new SubobjectBuilder().setLoose(loose).setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build()).build();
75 public byte[] serializeSubobject(final Subobject subobject) {
76 Preconditions.checkNotNull(subobject.getSubobjectType(), "Subobject type cannot be empty.");
77 final Label label = ((LabelCase) subobject.getSubobjectType()).getLabel();
78 final byte[] labelbytes = this.registry.serializeLabel(label.isUniDirectional(), false, label.getLabelType());
79 if (labelbytes == null) {
80 throw new IllegalArgumentException("Unknown EROLabelSubobject instance. Passed "
81 + label.getLabelType().getImplementedInterface());
83 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), labelbytes);