Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / 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.impl.subobject;
9
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.LabelParser;
12 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.LabelType;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabel;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabelBuilder;
16
17 /**
18  * Parser for {@link GeneralizedLabel}
19  */
20 public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
21
22         public static final int CTYPE = 2;
23
24         @Override
25         public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
26                 if (buffer == null || buffer.length == 0) {
27                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
28                 }
29
30                 return new GeneralizedLabelBuilder().setGeneralizedLabel(buffer).build();
31         }
32
33         @Override
34         public byte[] serializeLabel(final LabelType subobject) {
35                 if (!(subobject instanceof GeneralizedLabel)) {
36                         throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
37                                         + ". Needed GeneralizedLabel.");
38                 }
39                 return ((GeneralizedLabel) subobject).getGeneralizedLabel();
40         }
41
42         @Override
43         public int getType() {
44                 return CTYPE;
45         }
46 }