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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.xro.SubobjectsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedBuilder;
20 import com.google.common.primitives.UnsignedBytes;
21 import com.google.common.primitives.UnsignedInts;
24 * Parser for {@link UnnumberedSubobject}
26 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
28 public static final int TYPE = 4;
30 private static final int ATTRIBUTE_LENGTH = 1;
31 private static final int ROUTER_ID_NUMBER_LENGTH = 4;
32 private static final int INTERFACE_ID_NUMBER_LENGTH = 4;
34 private static final int ATTRIBUTE_OFFSET = 1;
35 private static final int ROUTER_ID_NUMBER_OFFSET = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
36 private static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
38 private static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
41 public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
42 if (buffer == null || buffer.length == 0) {
43 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
45 if (buffer.length != CONTENT_LENGTH) {
46 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
47 + CONTENT_LENGTH + ".");
50 final SubobjectsBuilder builder = new SubobjectsBuilder();
51 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
52 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
53 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
54 INTERFACE_ID_NUMBER_LENGTH))));
55 builder.setSubobjectType(ubuilder.build());
56 builder.setMandatory(mandatory);
57 builder.setAttribute(Attribute.forValue(UnsignedBytes.toInt(buffer[ATTRIBUTE_OFFSET])));
58 return builder.build();
62 public byte[] serializeSubobject(final Subobjects subobject) {
63 if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject)) {
64 throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
65 + ". Needed UnnumberedSubobject.");
68 final byte[] retBytes = new byte[CONTENT_LENGTH];
69 final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
71 retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
72 ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getRouterId()), 4, ROUTER_ID_NUMBER_LENGTH), retBytes,
73 ROUTER_ID_NUMBER_OFFSET);
74 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
75 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
80 public int getType() {