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.EROSubobjectParser;
12 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.UnnumberedSubobject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.UnnumberedBuilder;
19 import com.google.common.primitives.UnsignedInts;
22 * Parser for {@link UnnumberedSubobject}
24 public class EROUnnumberedInterfaceSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
26 public static final int TYPE = 4;
28 public static final int ROUTER_ID_NUMBER_LENGTH = 4;
29 public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
31 public static final int ROUTER_ID_NUMBER_OFFSET = 2; // added reserved field of size 2
32 public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
34 public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
37 public Subobjects parseSubobject(final byte[] buffer, final boolean loose) throws PCEPDeserializerException {
38 if (buffer == null || buffer.length == 0)
39 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
40 if (buffer.length != CONTENT_LENGTH)
41 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + "; Expected: "
42 + CONTENT_LENGTH + ".");
43 final SubobjectsBuilder builder = new SubobjectsBuilder();
44 builder.setLoose(loose);
45 final UnnumberedBuilder ubuilder = new UnnumberedBuilder();
46 ubuilder.setRouterId(ByteArray.bytesToLong(ByteArray.subByte(buffer, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)));
47 ubuilder.setInterfaceId(UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(buffer, INTERFACE_ID_NUMBER_OFFSET,
48 INTERFACE_ID_NUMBER_LENGTH))));
49 builder.setSubobjectType(ubuilder.build());
50 return builder.build();
54 public byte[] serializeSubobject(final Subobjects subobject) {
55 if (!(subobject.getSubobjectType() instanceof UnnumberedSubobject))
56 throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + subobject.getSubobjectType().getClass()
57 + ". Needed UnnumberedSubobject.");
60 retBytes = new byte[CONTENT_LENGTH];
61 final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
63 ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId()), retBytes, ROUTER_ID_NUMBER_OFFSET);
64 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
65 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
71 public int getType() {