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.subobject.EROUnnumberedInterfaceSubobject;
12 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
13 import org.opendaylight.protocol.util.ByteArray;
16 * Parser for {@link org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject
17 * EROUnnumberedInterfaceSubobject}
19 public class EROUnnumberedInterfaceSubobjectParser {
20 public static final int ROUTER_ID_NUMBER_LENGTH = 4;
21 public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
23 public static final int ROUTER_ID_NUMBER_OFFSET = 2; // added reserved field of size 2
24 public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
26 public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
28 public static EROUnnumberedInterfaceSubobject parse(final byte[] soContentsBytes, final boolean loose) throws PCEPDeserializerException {
29 if (soContentsBytes == null || soContentsBytes.length == 0)
30 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
31 if (soContentsBytes.length != CONTENT_LENGTH)
32 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
33 + CONTENT_LENGTH + ".");
35 // return new EROUnnumberedInterfaceSubobject(new IPv4Address(ByteArray.subByte(soContentsBytes,
36 // ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)), new UnnumberedInterfaceIdentifier(
37 // UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(soContentsBytes, INTERFACE_ID_NUMBER_OFFSET,
38 // INTERFACE_ID_NUMBER_LENGTH)))), loose);
42 public static byte[] put(final ExplicitRouteSubobject objToSerialize) {
43 if (!(objToSerialize instanceof EROUnnumberedInterfaceSubobject))
44 throw new IllegalArgumentException("Unknown ExplicitRouteSubobject instance. Passed " + objToSerialize.getClass()
45 + ". Needed EROUnnumberedInterfaceSubobject.");
48 retBytes = new byte[CONTENT_LENGTH];
49 final EROUnnumberedInterfaceSubobject specObj = (EROUnnumberedInterfaceSubobject) objToSerialize;
51 ByteArray.copyWhole(specObj.getRouterID().getAddress(), retBytes, ROUTER_ID_NUMBER_OFFSET);
52 System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceID().getInterfaceId()), Long.SIZE / Byte.SIZE
53 - INTERFACE_ID_NUMBER_LENGTH, retBytes, INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);