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