Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / WavebandSwitchingLabelParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.parser.subobject;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
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.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.WavebandSwitchingLabelCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
24
25 /**
26  * Parser for {@link WavebandSwitchingLabelCase}.
27  */
28 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
29     public static final int CTYPE = 3;
30
31     private static final int WAVEB_F_LENGTH = 4;
32     private static final int START_F_LENGTH = 4;
33     private static final int END_F_LENGTH = 4;
34     private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
35
36     @Override
37     public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
38         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
39         if (buffer.readableBytes() != CONTENT_LENGTH) {
40             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
41                 + "; Expected: " + CONTENT_LENGTH + ".");
42         }
43         return new WavebandSwitchingLabelCaseBuilder()
44                 .setWavebandSwitchingLabel(new WavebandSwitchingLabelBuilder()
45                     .setWavebandId(ByteBufUtils.readUint32(buffer))
46                     .setStartLabel(ByteBufUtils.readUint32(buffer))
47                     .setEndLabel(ByteBufUtils.readUint32(buffer))
48                     .build())
49                 .build();
50     }
51
52     @Override
53     public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject,
54             final ByteBuf buffer) {
55         checkArgument(subobject instanceof WavebandSwitchingLabelCase,
56             "Unknown Label Subobject instance. Passed {}. Needed WavebandSwitchingLabelCase.", subobject.getClass());
57         final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
58         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
59         ByteBufUtils.writeMandatory(body, obj.getWavebandId(), "WavebandId");
60         ByteBufUtils.writeMandatory(body, obj.getStartLabel(), "StartLabel");
61         ByteBufUtils.writeMandatory(body, obj.getEndLabel(), "EndLabel");
62         LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
63     }
64 }