Merge "BUG-50 : added tests for ERO subobjects."
[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.CLabel;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.GeneralizedLabel;
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.GeneralizedLabelBuilder;
17
18 /**
19  * Parser for {@link GeneralizedLabel}
20  */
21 public class GeneralizedLabelParser implements LabelParser, LabelSerializer {
22
23         public static final int CTYPE = 2;
24
25         @Override
26         public LabelType parseLabel(final byte[] buffer) throws PCEPDeserializerException {
27                 if (buffer == null || buffer.length == 0) {
28                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
29                 }
30
31                 return new GeneralizedLabelBuilder().setGeneralizedLabel(buffer).build();
32         }
33
34         @Override
35         public byte[] serializeLabel(final CLabel subobject) {
36                 if (!(subobject instanceof GeneralizedLabel)) {
37                         throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass()
38                                         + ". Needed GeneralizedLabel.");
39                 }
40                 return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.label.subobject.label.type.GeneralizedLabel) subobject).getGeneralizedLabel();
41         }
42
43         @Override
44         public int getType() {
45                 return CTYPE;
46         }
47 }