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 java.util.Arrays;
11 import java.util.BitSet;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.protocol.concepts.IPv4Address;
15 import org.opendaylight.protocol.concepts.IPv4Prefix;
16 import org.opendaylight.protocol.concepts.Prefix;
17 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
19 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
20 import com.google.common.primitives.UnsignedBytes;
23 * Parser for {@link org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject
24 * RROIPAddressSubobject<IPv4Prefix>}
26 public class RROIPv4AddressSubobjectParser {
27 public static final int IP_F_LENGTH = 4;
28 public static final int PREFIX_F_LENGTH = 1;
29 public static final int FLAGS_F_LENGTH = 1;
31 public static final int IP_F_OFFSET = 0;
32 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
33 public static final int FLAGS_F_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
35 public static final int CONTENT_LENGTH = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
38 * flags offset in bits
40 public static final int LPA_F_OFFSET = 7;
41 public static final int LPIU_F_OFFSET = 6;
43 public static RROIPAddressSubobject<IPv4Prefix> parse(byte[] soContentsBytes) throws PCEPDeserializerException {
44 if (soContentsBytes == null || soContentsBytes.length == 0)
45 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
46 if (soContentsBytes.length != CONTENT_LENGTH)
47 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: " + CONTENT_LENGTH + ".");
49 final IPv4Address address = new IPv4Address(ByteArray.subByte(soContentsBytes, IP_F_OFFSET, IP_F_LENGTH));
50 final int length = UnsignedBytes.toInt(soContentsBytes[PREFIX_F_OFFSET]);
52 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(soContentsBytes, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
54 return new RROIPAddressSubobject<IPv4Prefix>(new IPv4Prefix(address, length), flags.get(LPA_F_OFFSET), flags.get(LPIU_F_OFFSET));
57 public static byte[] put(ReportedRouteSubobject objToSerialize) {
58 if (!(objToSerialize instanceof RROIPAddressSubobject))
59 throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + objToSerialize.getClass()
60 + ". Needed RROIPAddressSubobject.");
62 final RROIPAddressSubobject<?> specObj = (RROIPAddressSubobject<?>) objToSerialize;
63 final Prefix<?> prefix = specObj.getPrefix();
65 if (!(prefix instanceof IPv4Prefix))
66 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ". Needed IPv4Prefix.");
68 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
70 flags.set(LPA_F_OFFSET, specObj.isLocalProtectionAvailable());
71 flags.set(LPIU_F_OFFSET, specObj.isLocalProtectionInUse());
73 final byte[] retBytes = new byte[CONTENT_LENGTH];
74 ByteArray.copyWhole(prefix.getAddress().getAddress(), retBytes, IP_F_OFFSET);
75 retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(prefix.getLength())[Integer.SIZE / Byte.SIZE - 1];
76 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);