BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIPv6AddressSubobjectParser.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 java.util.Arrays;
11 import java.util.BitSet;
12
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;
19
20 import com.google.common.primitives.UnsignedBytes;
21
22 /**
23  * Parser for {@link org.opendaylight.protocol.pcep.subobject.RROIPAddressSubobject RROIPAddressSubobject<IPv6Prefix>}
24  */
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;
29
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;
33
34         public static final int CONTENT_LENGTH = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
35
36         /*
37          * flags offset in bits
38          */
39         public static final int LPA_F_OFFSET = 7;
40         public static final int LPIU_F_OFFSET = 6;
41
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 + ".");
48
49                 final int length = UnsignedBytes.toInt(soContentsBytes[PREFIX_F_OFFSET]);
50
51                 final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(soContentsBytes, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
52
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));
55         }
56
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.");
61
62                 final RROIPAddressSubobject specObj = (RROIPAddressSubobject) objToSerialize;
63                 final IpPrefix prefix = specObj.getPrefix();
64
65                 if (prefix.getIpv4Prefix() != null)
66                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ". Needed IPv6Prefix.");
67
68                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
69
70                 flags.set(LPA_F_OFFSET, specObj.isLocalProtectionAvailable());
71                 flags.set(LPIU_F_OFFSET, specObj.isLocalProtectionInUse());
72
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);
77
78                 return retBytes;
79         }
80 }