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.concepts.Ipv6Util;
14 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject;
16 import org.opendaylight.protocol.pcep.subobject.ReportedRouteSubobject;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import com.google.common.primitives.UnsignedBytes;
23 * Parser for {@link org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject RROIPAddressSubobject<IPv6Prefix>}
25 public class RROIPv6AddressSubobjectParser {
26 public static final int IP_F_LENGTH = 16;
27 public static final int PREFIX_F_LENGTH = 1;
28 public static final int FLAGS_F_LENGTH = 1;
30 public static final int IP_F_OFFSET = 0;
31 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
32 public static final int FLAGS_F_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
34 public static final int CONTENT_LENGTH = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
37 * flags offset in bits
39 public static final int LPA_F_OFFSET = 7;
40 public static final int LPIU_F_OFFSET = 6;
42 public static RROIPAddressSubobject parse(final byte[] soContentsBytes) throws PCEPDeserializerException {
43 if (soContentsBytes == null || soContentsBytes.length == 0)
44 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
45 if (soContentsBytes.length != CONTENT_LENGTH)
46 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
47 + CONTENT_LENGTH + ".");
49 final int length = UnsignedBytes.toInt(soContentsBytes[PREFIX_F_OFFSET]);
51 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(soContentsBytes, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
53 return new RROIPAddressSubobject(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(soContentsBytes, IP_F_OFFSET, IP_F_LENGTH),
54 length)), flags.get(LPA_F_OFFSET), flags.get(LPIU_F_OFFSET));
57 public static byte[] put(final 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 IpPrefix prefix = specObj.getPrefix();
65 if (prefix.getIpv4Prefix() != null)
66 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ". Needed IPv6Prefix.");
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.getIpv4Prefix().getValue().getBytes(), 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);