Clean pcep/base-parser code
[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 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12
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.protocol.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
25
26 /**
27  * Parser for {@link WavebandSwitchingLabelCase}.
28  */
29 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
30
31     public static final int CTYPE = 3;
32
33     private static final int WAVEB_F_LENGTH = 4;
34     private static final int START_F_LENGTH = 4;
35     private static final int END_F_LENGTH = 4;
36
37     private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
38
39     @Override
40     public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
41         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
42         if (buffer.readableBytes() != CONTENT_LENGTH) {
43             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes()
44                 + "; Expected: " + CONTENT_LENGTH + ".");
45         }
46         return new WavebandSwitchingLabelCaseBuilder()
47                 .setWavebandSwitchingLabel(new WavebandSwitchingLabelBuilder()
48                     .setWavebandId(ByteBufUtils.readUint32(buffer))
49                     .setStartLabel(ByteBufUtils.readUint32(buffer))
50                     .setEndLabel(ByteBufUtils.readUint32(buffer))
51                     .build())
52                 .build();
53     }
54
55     @Override
56     public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject,
57             final ByteBuf buffer) {
58         checkArgument(subobject instanceof WavebandSwitchingLabelCase,
59             "Unknown Label Subobject instance. Passed {}. Needed WavebandSwitchingLabelCase.", subobject.getClass());
60         final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
61         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
62         checkArgument(obj.getWavebandId() != null, "WavebandId is mandatory.");
63         writeUnsignedInt(obj.getWavebandId(), body);
64         checkArgument(obj.getStartLabel() != null, "StartLabel is mandatory.");
65         writeUnsignedInt(obj.getStartLabel(), body);
66         checkArgument(obj.getEndLabel() != null, "EndLabel is mandatory.");
67         writeUnsignedInt(obj.getEndLabel(), body);
68         LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
69     }
70 }