BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROIPv6PrefixSubobjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.subobject;
9
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;
16
17 import com.google.common.primitives.UnsignedBytes;
18
19 /**
20  * Parser for {@link org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject XROIPPrefixSubobject<IPv6Prefix>}
21  */
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;
26
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;
30
31         public static final int CONTENT_LENGTH = ATTRIBUTE_OFFSET + ATTRIBUTE_LENGTH;
32
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 + ".");
39
40                 final int length = UnsignedBytes.toInt(soContentsBytes[PREFIX_F_OFFSET]);
41
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)));
45         }
46
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.");
51
52                 final XROIPPrefixSubobject specObj = (XROIPPrefixSubobject) objToSerialize;
53                 final IpPrefix prefix = specObj.getPrefix();
54
55                 if (prefix.getIpv4Prefix() != null)
56                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ". Needed IPv6Prefix.");
57
58                 final byte[] retBytes = new byte[CONTENT_LENGTH];
59
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());
63
64                 return retBytes;
65         }
66 }