BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROIpPrefixSubobjectParser.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.Ipv4Util;
11 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
12 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
13 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
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 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.Subobjects;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.exclude.route.object.SubobjectsBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.ExcludeRouteSubobjects.Attribute;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixBuilder;
21
22 import com.google.common.primitives.UnsignedBytes;
23
24 /**
25  * Parser for {@link IpPrefixSubobject}
26  */
27 public class XROIpPrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
28
29         public static final int TYPE = 1;
30
31         public static final int TYPE6 = 2;
32
33         public static final int IP4_F_LENGTH = 4;
34         public static final int PREFIX_F_LENGTH = 1;
35         public static final int ATTRIBUTE_LENGTH = 1;
36
37         public static final int IP_F_OFFSET = 0;
38         public static final int PREFIX4_F_OFFSET = IP_F_OFFSET + IP4_F_LENGTH;
39         public static final int ATTRIBUTE4_OFFSET = PREFIX4_F_OFFSET + PREFIX_F_LENGTH;
40
41         public static final int CONTENT4_LENGTH = ATTRIBUTE4_OFFSET + ATTRIBUTE_LENGTH;
42
43         public static final int IP6_F_LENGTH = 16;
44         public static final int PREFIX6_F_OFFSET = IP_F_OFFSET + IP6_F_LENGTH;
45         public static final int ATTRIBUTE6_OFFSET = PREFIX6_F_OFFSET + PREFIX_F_LENGTH;
46
47         public static final int CONTENT6_LENGTH = ATTRIBUTE6_OFFSET + ATTRIBUTE_LENGTH;
48
49         @Override
50         public Subobjects parseSubobject(final byte[] buffer, final boolean mandatory) throws PCEPDeserializerException {
51                 if (buffer == null || buffer.length == 0)
52                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
53                 if (buffer.length != CONTENT4_LENGTH && buffer.length != CONTENT6_LENGTH)
54                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
55
56                 final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
57
58                 final SubobjectsBuilder builder = new SubobjectsBuilder();
59                 builder.setMandatory(mandatory);
60                 builder.setAttribute(Attribute.forValue(buffer[ATTRIBUTE4_OFFSET] & 0xFF));
61                 builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
62                                 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, IP_F_OFFSET, IP4_F_LENGTH), length))).build());
63                 return builder.build();
64         }
65
66         @Override
67         public byte[] serializeSubobject(final Subobjects subobject) {
68                 if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject))
69                         throw new IllegalArgumentException("Unknown PCEPXROSubobject instance. Passed " + subobject.getSubobjectType().getClass()
70                                         + ". Needed IpPrefixSubobject.");
71
72                 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
73                 final IpPrefix prefix = specObj.getIpPrefix();
74
75                 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null)
76                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
77
78                 if (prefix.getIpv4Prefix() != null) {
79                         final byte[] retBytes = new byte[CONTENT4_LENGTH];
80                         ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
81                         retBytes[PREFIX4_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
82                         return retBytes;
83                 } else {
84                         final byte[] retBytes = new byte[CONTENT6_LENGTH];
85                         ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, IP_F_OFFSET);
86                         retBytes[PREFIX6_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
87                         return retBytes;
88                 }
89         }
90
91         @Override
92         public int getType() {
93                 return TYPE;
94         }
95
96         public int getType6() {
97                 return TYPE6;
98         }
99 }