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