d10a5b01c56fb374170a701f16c5d6fc99e36a15
[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.xro.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.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 import com.google.common.primitives.UnsignedBytes;
22
23 /**
24  * Parser for {@link SrlgSubobject}
25  */
26 public class XROSRLGSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
27
28         public static final int TYPE = 34;
29
30         private static final int SRLG_ID_NUMBER_LENGTH = 4;
31         private static final int ATTRIBUTE_LENGTH = 1;
32
33         private static final int SRLG_ID_NUMBER_OFFSET = 0;
34         private static final int ATTRIBUTE_OFFSET = SRLG_ID_NUMBER_OFFSET + SRLG_ID_NUMBER_LENGTH;
35
36         private static final int CONTENT_LENGTH = SRLG_ID_NUMBER_LENGTH + ATTRIBUTE_LENGTH;
37
38         @Override
39         public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
40                 if (buffer == null || buffer.length == 0) {
41                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
42                 }
43                 if (buffer.length != CONTENT_LENGTH) {
44                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
45                                         + CONTENT_LENGTH + ".");
46                 }
47
48                 final SubobjectsBuilder builder = new SubobjectsBuilder();
49                 builder.setMandatory(mandatory);
50                 builder.setAttribute(Attribute.Srlg);
51                 builder.setSubobjectType(new SrlgBuilder().setSrlgId(
52                                 new SrlgId(ByteArray.bytesToLong(ByteArray.subByte(buffer, SRLG_ID_NUMBER_OFFSET, SRLG_ID_NUMBER_LENGTH)))).build());
53                 return builder.build();
54         }
55
56         @Override
57         public byte[] serializeSubobject(final Subobjects subobject) {
58                 if (!(subobject.getSubobjectType() instanceof SrlgSubobject)) {
59                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
60                                         + ". Needed SrlgSubobject.");
61                 }
62
63                 byte[] retBytes;
64                 retBytes = new byte[CONTENT_LENGTH];
65                 final SrlgSubobject specObj = (SrlgSubobject) subobject.getSubobjectType();
66
67                 ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getSrlgId().getValue()), 4, SRLG_ID_NUMBER_LENGTH), retBytes,
68                                 SRLG_ID_NUMBER_OFFSET);
69                 retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
70
71                 return retBytes;
72         }
73
74         @Override
75         public int getType() {
76                 return TYPE;
77         }
78 }