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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.LabelParser;
16 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
17 import org.opendaylight.protocol.pcep.spi.LabelUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
26 * Parser for {@link WavebandSwitchingLabelCase}
28 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
30 public static final int CTYPE = 3;
32 private static final int WAVEB_F_LENGTH = 4;
33 private static final int START_F_LENGTH = 4;
34 private static final int END_F_LENGTH = 4;
36 private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
39 public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
40 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
41 if (buffer.readableBytes() != CONTENT_LENGTH) {
42 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: "
43 + CONTENT_LENGTH + ".");
45 final WavebandSwitchingLabelBuilder builder = new WavebandSwitchingLabelBuilder();
46 builder.setWavebandId(buffer.readUnsignedInt());
47 builder.setStartLabel(buffer.readUnsignedInt());
48 builder.setEndLabel(buffer.readUnsignedInt());
49 return new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(builder.build()).build();
53 public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject, final ByteBuf buffer) {
54 Preconditions.checkArgument(subobject instanceof WavebandSwitchingLabelCase, "Unknown Label Subobject instance. Passed {}. Needed WavebandSwitchingLabelCase.", subobject.getClass());
55 final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
56 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
57 Preconditions.checkArgument(obj.getWavebandId() != null, "WavebandId is mandatory.");
58 writeUnsignedInt(obj.getWavebandId(), body);
59 Preconditions.checkArgument(obj.getStartLabel() != null, "StartLabel is mandatory.");
60 writeUnsignedInt(obj.getStartLabel(), body);
61 Preconditions.checkArgument(obj.getEndLabel() != null, "EndLabel is mandatory.");
62 writeUnsignedInt(obj.getEndLabel(), body);
63 LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);