BUG-612 : switched Labels to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.subobject;
9
10 import io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.pcep.spi.LabelParser;
13 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
14 import org.opendaylight.protocol.pcep.spi.LabelUtil;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
22
23 import com.google.common.base.Preconditions;
24
25 /**
26  * Parser for {@link WavebandSwitchingLabelCase}
27  */
28 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
29
30         public static final int CTYPE = 3;
31
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;
35
36         private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
37
38         @Override
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 + ".");
44                 }
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();
50         }
51
52         @Override
53         public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
54                 if (!(subobject instanceof WavebandSwitchingLabelCase)) {
55                         throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
56                                         + ". Needed WavebandSwitchingLabelCase.");
57                 }
58                 final byte[] retBytes = new byte[CONTENT_LENGTH];
59                 final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
60                 System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue(), WAVEB_F_LENGTH), 0, retBytes, 0, WAVEB_F_LENGTH);
61                 System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue(), START_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
62                 System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue(), END_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH,
63                                 END_F_LENGTH);
64                 return LabelUtil.formatLabel(CTYPE, unidirectional, global, retBytes);
65         }
66 }