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.concepts.Ipv6Util;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
13 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
17 import com.google.common.primitives.UnsignedBytes;
20 * Parser for {@link org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject XROIPPrefixSubobject<IPv6Prefix>}
22 public class XROIPv6PrefixSubobjectParser {
23 public static final int IP_F_LENGTH = 16;
24 public static final int PREFIX_F_LENGTH = 1;
25 public static final int ATTRIBUTE_LENGTH = 1;
27 public static final int IP_F_OFFSET = 0;
28 public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
29 public static final int ATTRIBUTE_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
31 public static final int CONTENT_LENGTH = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
33 public static XROIPPrefixSubobject parse(final byte[] soContentsBytes, final boolean mandatory) throws PCEPDeserializerException {
34 if (soContentsBytes == null || soContentsBytes.length == 0)
35 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
36 if (soContentsBytes.length != CONTENT_LENGTH)
37 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + soContentsBytes.length + "; Expected: "
38 + CONTENT_LENGTH + ".");
40 final int length = UnsignedBytes.toInt(soContentsBytes[PREFIX_F_OFFSET]);
42 return new XROIPPrefixSubobject(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(soContentsBytes, IP_F_OFFSET, IP_F_LENGTH),
43 length)), mandatory, XROSubobjectAttributeMapping.getInstance().getFromAttributeIdentifier(
44 (short) (soContentsBytes[ATTRIBUTE_OFFSET] & 0xFF)));
47 public static byte[] put(final ExcludeRouteSubobject objToSerialize) {
48 if (!(objToSerialize instanceof XROIPPrefixSubobject))
49 throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + objToSerialize.getClass()
50 + ". Needed XROIPPrefixSubobject.");
52 final XROIPPrefixSubobject specObj = (XROIPPrefixSubobject) objToSerialize;
53 final IpPrefix prefix = specObj.getPrefix();
55 if (prefix.getIpv4Prefix() != null)
56 throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ". Needed IPv6Prefix.");
58 final byte[] retBytes = new byte[CONTENT_LENGTH];
60 ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
61 // retBytes[PREFIX_F_OFFSET] = (byte) prefix.getLength();
62 retBytes[ATTRIBUTE_OFFSET] = (byte) XROSubobjectAttributeMapping.getInstance().getFromAttributeEnum(specObj.getAttribute());