BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROSRLGSubobjectParser.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.XROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.SrlgBuilder;
20
21 /**
22  * Parser for {@link SrlgSubobject}
23  */
24 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
25
26         public static final int TYPE = 34;
27
28         public static final int SRLG_ID_NUMBER_LENGTH = 4;
29         public static final int ATTRIBUTE_LENGTH = 1;
30
31         public static final int SRLG_ID_NUMBER_OFFSET = 0;
32         public static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH + 1; // added reserved
33                                                                                                                                                                                                         // field of size 1
34
35         public static final int CONTENT_LENGTH = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
36
37         @Override
38         public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
39                 if (buffer == null || buffer.length == 0)
40                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
41                 if (buffer.length != CONTENT_LENGTH)
42                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
43                                         + CONTENT_LENGTH + ".");
44
45                 final SubobjectsBuilder builder = new SubobjectsBuilder();
46                 builder.setMandatory(mandatory);
47                 builder.setAttribute(Attribute.Srlg);
48                 builder.setSubobjectType(new SrlgBuilder().setSrlgId(
49                                 new SrlgId(ByteArray.bytesToLong(ByteArray.subByte(buffer, SRLG_ID_NUMBER_OFFSET, SRLG_ID_NUMBER_LENGTH)))).build());
50                 return builder.build();
51         }
52
53         @Override
54         public byte[] serializeSubobject(final Subobjects subobject) {
55                 if (!(subobject.getSubobjectType() instanceof SrlgSubobject))
56                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
57                                         + ". Needed SrlgSubobject.");
58
59                 byte[] retBytes;
60                 retBytes = new byte[CONTENT_LENGTH];
61                 final SrlgSubobject specObj = (SrlgSubobject) subobject.getSubobjectType();
62
63                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue()), retBytes, SRLG_ID_NUMBER_OFFSET);
64                 retBytes[ATTRIBUTE_OFFSET] = (byte) subobject.getAttribute().getIntValue();
65
66                 return retBytes;
67         }
68
69         @Override
70         public int getType() {
71                 return TYPE;
72         }
73 }