BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROUnnumberedInterfaceSubobjectParser.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.EROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedBuilder;
18
19 import com.google.common.primitives.UnsignedInts;
20
21 /**
22  * Parser for {@link UnnumberedSubobject}
23  */
24 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
25
26         public static final int TYPE = 4;
27
28         public static final int ROUTER_ID_NUMBER_LENGTH = 4;
29         public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
30
31         public static final int ROUTER_ID_NUMBER_OFFSET = 2; // added reserved field of size 2
32         public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
33
34         public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
35
36         @Override
37         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
38                 if (buffer == null || buffer.length == 0)
39                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
40                 if (buffer.length != CONTENT_LENGTH)
41                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
42                                         + CONTENT_LENGTH + ".");
43                 final SubobjectsBuilder builder = new SubobjectsBuilder();
44                 builder.setLoose(loose);
45                 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
46                 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
47                 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
48                                 INTERFACE_ID_NUMBER_LENGTH))));
49                 builder.setSubobjectType(ubuilder.build());
50                 return builder.build();
51         }
52
53         @Override
54         public byte[] serializeSubobject(final Subobjects subobject) {
55                 if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject))
56                         throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
57                                         + ". Needed UnnumberedSubobject.");
58
59                 byte[] retBytes;
60                 retBytes = new byte[CONTENT_LENGTH];
61                 final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
62
63                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId()), retBytes, ROUTER_ID_NUMBER_OFFSET);
64                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
65                                 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
66
67                 return retBytes;
68         }
69
70         @Override
71         public int getType() {
72                 return TYPE;
73         }
74 }