Adjusted byte array method to remove most of magic numbers.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROUnnumberedInterfaceSubobjectParser.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 java.util.Arrays;
11 import java.util.BitSet;
12
13 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobjects;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectsBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.UnnumberedBuilder;
21
22 import com.google.common.primitives.UnsignedInts;
23
24 /**
25  * Parser for {@link UnnumberedSubobject}
26  */
27 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
28
29         public static final int TYPE = 4;
30
31         private static final int FLAGS_F_LENGTH = 1;
32         private static final int ROUTER_ID_NUMBER_LENGTH = 4;
33         private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
34
35         private static final int ROUTER_ID_NUMBER_OFFSET = 2;
36         private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
37
38         private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
39
40         private static final int LPA_F_OFFSET = 7;
41         private static final int LPIU_F_OFFSET = 6;
42
43         @Override
44         public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
45                 if (buffer == null || buffer.length == 0) {
46                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
47                 }
48                 if (buffer.length != CONTENT_LENGTH) {
49                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
50                                         + CONTENT_LENGTH + ".");
51                 }
52
53                 final SubobjectsBuilder builder = new SubobjectsBuilder();
54                 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, 0, FLAGS_F_LENGTH));
55                 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
56                 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
57                 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
58                 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
59                 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
60                                 INTERFACE_ID_NUMBER_LENGTH))));
61                 builder.setSubobjectType(ubuilder.build());
62                 return builder.build();
63         }
64
65         @Override
66         public byte[] serializeSubobject(final Subobjects subobject) {
67                 if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject)) {
68                         throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
69                                         + ". Needed UnnumberedSubobject.");
70                 }
71                 final byte[] retBytes = new byte[CONTENT_LENGTH];
72                 final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
73                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
74                 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
75                 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
76                 retBytes[0] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
77                 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
78                 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
79                                 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
80                 return retBytes;
81         }
82
83         @Override
84         public int getType() {
85                 return TYPE;
86         }
87 }