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