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