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.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.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.UnsignedInts;
23 * Parser for {@link UnnumberedSubobject}
25 public class XROUnnumberedInterfaceSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
27 public static final int TYPE = 4;
29 public static final int ATTRIBUTE_LENGTH = 1;
30 public static final int ROUTER_ID_NUMBER_LENGTH = 4;
31 public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
33 public static final int ATTRIBUTE_OFFSET = 1;// added reserved field of size 1
34 public static final int ROUTER_ID_NUMBER_OFFSET = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
35 public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
37 public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
40 public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
41 if (buffer == null || buffer.length == 0)
42 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
43 if (buffer.length != CONTENT_LENGTH)
44 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
45 + CONTENT_LENGTH + ".");
47 final SubobjectsBuilder builder = new SubobjectsBuilder();
48 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
49 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
50 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
51 INTERFACE_ID_NUMBER_LENGTH))));
52 builder.setSubobjectType(ubuilder.build());
53 builder.setMandatory(mandatory);
54 builder.setAttribute(Attribute.forValue(buffer[ATTRIBUTE_OFFSET] & 0xFF));
55 return builder.build();
59 public byte[] serializeSubobject(final Subobjects subobject) {
60 if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject))
61 throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
62 + ". Needed UnnumberedSubobject.");
65 retBytes = new byte[CONTENT_LENGTH];
66 final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
68 retBytes[ATTRIBUTE_OFFSET] = (byte) subobject.getAttribute().getIntValue();
69 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId()), retBytes, ROUTER_ID_NUMBER_OFFSET);
70 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
71 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
76 public int getType() {