BUG-612 : switched Labels to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / Type1LabelParser.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.Type1LabelCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.Type1LabelCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.type1.label._case.Type1LabelBuilder;
21
22 import com.google.common.base.Preconditions;
23
24 /**
25  * Parser for {@link Type1LabelCase}
26  */
27 public class Type1LabelParser implements LabelParser, LabelSerializer {
28
29         public static final int CTYPE = 1;
30
31         public static final int LABEL_LENGTH = 4;
32
33         @Override
34         public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
35                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
36                 if (buffer.readableBytes() != LABEL_LENGTH) {
37                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + "; Expected: " + LABEL_LENGTH
38                                         + ".");
39                 }
40                 return new Type1LabelCaseBuilder().setType1Label(
41                                 new Type1LabelBuilder().setType1Label(buffer.readUnsignedInt()).build()).build();
42         }
43
44         @Override
45         public byte[] serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject) {
46                 if (!(subobject instanceof Type1LabelCase)) {
47                         throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
48                                         + ". Needed Type1LabelCase.");
49                 }
50                 return LabelUtil.formatLabel(CTYPE, unidirectional, global, ByteArray.longToBytes(((Type1LabelCase) subobject).getType1Label().getType1Label().longValue(), LABEL_LENGTH));
51         }
52 }