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.util.ByteArray;
11 import org.opendaylight.protocol.concepts.IPv4Address;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.concepts.UnnumberedInterfaceIdentifier;
14 import org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject;
15 import org.opendaylight.protocol.pcep.subobject.ExplicitRouteSubobject;
16 import com.google.common.primitives.UnsignedInts;
20 * {@link org.opendaylight.protocol.pcep.subobject.EROUnnumberedInterfaceSubobject
21 * EROUnnumberedInterfaceSubobject}
23 public class EROUnnumberedInterfaceSubobjectParser {
24 public static final int ROUTER_ID_NUMBER_LENGTH = 4;
25 public static final int INTERFACE_ID_NUMBER_LENGTH = 4;
27 public static final int ROUTER_ID_NUMBER_OFFSET = 2; // added reserved field of size 2
28 public static final int INTERFACE_ID_NUMBER_OFFSET = ROUTER_ID_NUMBER_OFFSET + ROUTER_ID_NUMBER_LENGTH;
30 public static final int CONTENT_LENGTH = INTERFACE_ID_NUMBER_OFFSET + INTERFACE_ID_NUMBER_LENGTH;
32 public static EROUnnumberedInterfaceSubobject parse(byte[] soContentsBytes, boolean loose) throws PCEPDeserializerException {
33 if (soContentsBytes == null || soContentsBytes.length == 0)
34 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
35 if (soContentsBytes.length != CONTENT_LENGTH)
36 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: " + CONTENT_LENGTH + ".");
38 return new EROUnnumberedInterfaceSubobject(new IPv4Address(ByteArray.subByte(soContentsBytes, ROUTER_ID_NUMBER_OFFSET, ROUTER_ID_NUMBER_LENGTH)), new UnnumberedInterfaceIdentifier(
39 UnsignedInts.toLong(ByteArray.bytesToInt(ByteArray.subByte(soContentsBytes, INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH)))), loose);
42 public static byte[] put(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 - INTERFACE_ID_NUMBER_LENGTH, retBytes,
53 INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);