2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.impl.subobject;
10 import java.util.Arrays;
11 import java.util.BitSet;
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;
24 import com.google.common.primitives.UnsignedInts;
27 * Parser for {@link UnnumberedCase}
29 public class RROUnnumberedInterfaceSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
31 public static final int TYPE = 4;
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;
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;
40 private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
42 private static final int LPA_F_OFFSET = 7;
43 private static final int LPIU_F_OFFSET = 6;
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.");
50 if (buffer.length != CONTENT_LENGTH) {
51 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
52 + CONTENT_LENGTH + ".");
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();
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.");
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);
86 public int getType() {