Removing deprecated getType() method from serializers.
[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 org.opendaylight.protocol.pcep.spi.LabelParser;
11 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
12 import org.opendaylight.protocol.pcep.spi.LabelUtil;
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.WavebandSwitchingLabelCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabel;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.waveband.switching.label._case.WavebandSwitchingLabelBuilder;
20
21 /**
22  * Parser for {@link WavebandSwitchingLabelCase}
23  */
24 public class WavebandSwitchingLabelParser implements LabelParser, LabelSerializer {
25
26         public static final int CTYPE = 3;
27
28         private static final int WAVEB_F_LENGTH = 4;
29         private static final int START_F_LENGTH = 4;
30         private static final int END_F_LENGTH = 4;
31
32         private static final int CONTENT_LENGTH = WAVEB_F_LENGTH + START_F_LENGTH + END_F_LENGTH;
33
34         @Override
35         public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
36                 if (buffer == null || buffer.length == 0) {
37                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
38                 }
39                 if (buffer.length != CONTENT_LENGTH) {
40                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
41                                         + CONTENT_LENGTH + ".");
42                 }
43                 final WavebandSwitchingLabelBuilder builder = new WavebandSwitchingLabelBuilder();
44                 int byteOffset = 0;
45                 builder.setWavebandId(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, WAVEB_F_LENGTH)));
46                 byteOffset += WAVEB_F_LENGTH;
47                 builder.setStartLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, START_F_LENGTH)));
48                 byteOffset += START_F_LENGTH;
49                 builder.setEndLabel(ByteArray.bytesToLong(ByteArray.subByte(buffer, byteOffset, END_F_LENGTH)));
50                 return new WavebandSwitchingLabelCaseBuilder().setWavebandSwitchingLabel(builder.build()).build();
51         }
52
53         @Override
54         public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
55                 if (!(subobject instanceof WavebandSwitchingLabelCase)) {
56                         throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
57                                         + ". Needed WavebandSwitchingLabelCase.");
58                 }
59                 final byte[] retBytes = new byte[CONTENT_LENGTH];
60                 final WavebandSwitchingLabel obj = ((WavebandSwitchingLabelCase) subobject).getWavebandSwitchingLabel();
61                 System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue(), WAVEB_F_LENGTH), 0, retBytes, 0, WAVEB_F_LENGTH);
62                 System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue(), START_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
63                 System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue(), END_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH,
64                                 END_F_LENGTH);
65                 return LabelUtil.formatLabel(CTYPE, unidirectional, global, retBytes);
66         }
67 }