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 static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.LabelParser;
15 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
16 import org.opendaylight.protocol.pcep.spi.LabelUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.Type1LabelCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1Label;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26 * Parser for {@link Type1LabelCase}.
28 public class Type1LabelParser implements LabelParser, LabelSerializer {
29 public static final int CTYPE = 1;
30 public static final int LABEL_LENGTH = 4;
33 public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
34 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
35 if (buffer.readableBytes() != LABEL_LENGTH) {
36 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
37 + "; Expected: " + LABEL_LENGTH + ".");
39 return new Type1LabelCaseBuilder()
40 .setType1Label(new Type1LabelBuilder().setType1Label(ByteBufUtils.readUint32(buffer)).build())
45 public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject,
46 final ByteBuf buffer) {
47 checkArgument(subobject instanceof Type1LabelCase,
48 "Unknown Label Subobject instance. Passed {}. Needed Type1LabelCase.", subobject.getClass());
49 final ByteBuf body = Unpooled.buffer(LABEL_LENGTH);
50 final Type1Label type1Label = ((Type1LabelCase) subobject).getType1Label();
51 checkArgument(type1Label != null, "Type1Label is mandatory.");
52 ByteBufUtils.writeOrZero(body, type1Label.getType1Label());
53 LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);