BUG-50 : added tests for IRO and RRO subobjects.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / RROIpPrefixSubobjectParser.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.Ipv4Util;
14 import org.opendaylight.protocol.concepts.Ipv6Util;
15 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReportedRouteObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.Subobjects;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.SubobjectsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixBuilder;
25
26 import com.google.common.primitives.UnsignedBytes;
27
28 /**
29  * Parser for {@link ReportedRouteObject}
30  */
31 public class RROIpPrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
32
33         public static final int TYPE = 1;
34
35         public static final int TYPE6 = 2;
36
37         private static final int IP4_F_LENGTH = 4;
38         private static final int IP_F_LENGTH = 16;
39
40         private static final int PREFIX_F_LENGTH = 1;
41         private static final int FLAGS_F_LENGTH = 1;
42
43         private static final int IP_F_OFFSET = 0;
44
45         private static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
46         private static final int FLAGS4_F_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
47
48         private static final int CONTENT4_LENGTH = IP4_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
49
50         private static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
51         private static final int FLAGS_F_OFFSET = PREFIX_F_OFFSET + PREFIX_F_LENGTH;
52
53         private static final int CONTENT_LENGTH = IP_F_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
54
55         private static final int LPA_F_OFFSET = 7;
56         private static final int LPIU_F_OFFSET = 6;
57
58         @Override
59         public Subobjects parseSubobject(final byte[] buffer) throws PCEPDeserializerException {
60                 if (buffer == null || buffer.length == 0) {
61                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
62                 }
63                 final SubobjectsBuilder builder = new SubobjectsBuilder();
64                 if (buffer.length == CONTENT4_LENGTH) {
65                         final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
66                         final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS4_F_OFFSET, FLAGS4_F_OFFSET + FLAGS_F_LENGTH));
67                         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
68                         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
69                         builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
70                                         new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build());
71                 } else if (buffer.length == CONTENT_LENGTH) {
72                         final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
73                         final BitSet flags = ByteArray.bytesToBitSet(Arrays.copyOfRange(buffer, FLAGS_F_OFFSET, FLAGS_F_OFFSET + FLAGS_F_LENGTH));
74                         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
75                         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
76                         builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
77                                         new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP_F_LENGTH), length))).build());
78                 } else {
79                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
80                 }
81                 return builder.build();
82         }
83
84         @Override
85         public byte[] serializeSubobject(final Subobjects subobject) {
86                 if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject)) {
87                         throw new IllegalArgumentException("Unknown ReportedRouteSubobject instance. Passed " + subobject.getClass()
88                                         + ". Needed RROIPAddressSubobject.");
89                 }
90
91                 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
92                 final IpPrefix prefix = specObj.getIpPrefix();
93
94                 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
95                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
96                 }
97
98                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
99                 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
100                 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
101
102                 if (prefix.getIpv4Prefix() != null) {
103                         final byte[] retBytes = new byte[CONTENT4_LENGTH];
104                         ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
105                         retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
106                         ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS4_F_OFFSET);
107                         return retBytes;
108                 } else {
109                         final byte[] retBytes = new byte[CONTENT_LENGTH];
110                         ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
111                         retBytes[PREFIX_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
112                         ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
113                         return retBytes;
114                 }
115         }
116
117         @Override
118         public int getType() {
119                 return TYPE;
120         }
121
122         public int getType6() {
123                 return TYPE6;
124         }
125 }