Promote MessageRegistry to pcep-api
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / subobject / GeneralizedLabelParser.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
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.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.LabelParser;
17 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
18 import org.opendaylight.protocol.pcep.spi.LabelUtil;
19 import org.opendaylight.protocol.util.ByteArray;
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.GeneralizedLabelCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.GeneralizedLabelCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.label.type.generalized.label._case.GeneralizedLabelBuilder;
25
26 /**
27  * Parser for {@link GeneralizedLabelCase}.
28  */
29 public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
30
31     public static final int CTYPE = 2;
32
33     @Override
34     public LabelType parseLabel(final ByteBuf buffer) throws PCEPDeserializerException {
35         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
36         return new GeneralizedLabelCaseBuilder()
37                 .setGeneralizedLabel(
38                         new GeneralizedLabelBuilder().setGeneralizedLabel(ByteArray.readAllBytes(buffer)).build())
39                 .build();
40     }
41
42     @Override
43     public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType subobject,
44             final ByteBuf buffer) {
45         checkArgument(subobject instanceof GeneralizedLabelCase,
46                 "Unknown Label Subobject instance. Passed {}. Needed GeneralizedLabelCase.", subobject.getClass());
47         final GeneralizedLabel gl = ((GeneralizedLabelCase) subobject).getGeneralizedLabel();
48         Preconditions.checkArgument(gl != null, "GeneralizedLabel is mandatory.");
49         final ByteBuf body = Unpooled.wrappedBuffer(gl.getGeneralizedLabel());
50         LabelUtil.formatLabel(CTYPE, unidirectional, global, body, buffer);
51     }
52 }