BUG-47 : switched subobjects to generated source code.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpPrefixSubobjectParser.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.concepts.Ipv6Util;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Subobjects;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.SubobjectsBuilder;
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 EROIpPrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
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 PREFIX4_F_LENGTH = 1;
35
36         public static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
37
38         public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; // added reserved field of size 1
39
40         public static final int IP_F_LENGTH = 16;
41         public static final int PREFIX_F_LENGTH = 1;
42
43         public static final int IP_F_OFFSET = 0;
44         public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
45
46         public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1
47                                                                                                                                                                         // byte
48
49         @Override
50         public Subobjects parseSubobject(final byte[] buffer, final boolean loose) 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                 }
54
55                 final SubobjectsBuilder builder = new SubobjectsBuilder();
56                 builder.setLoose(loose);
57
58                 if (buffer.length == CONTENT4_LENGTH) {
59                         final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
60                         builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
61                                         new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build());
62                         return builder.build();
63                 } else if (buffer.length == CONTENT_LENGTH) {
64                         final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
65                         builder.setSubobjectType(new IpPrefixBuilder().setIpPrefix(
66                                         new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build());
67                         return builder.build();
68                 } else {
69                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
70                 }
71         }
72
73         @Override
74         public byte[] serializeSubobject(final Subobjects subobject) {
75                 if (!(subobject.getSubobjectType() instanceof IpPrefixSubobject)) {
76                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
77                                         + ". Needed IpPrefixSubobject.");
78                 }
79                 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject.getSubobjectType();
80                 final IpPrefix prefix = specObj.getIpPrefix();
81
82                 if (prefix.getIpv4Prefix() != null) {
83                         final byte[] retBytes = new byte[CONTENT4_LENGTH];
84                         ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
85                         retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
86                         return retBytes;
87                 } else if (prefix.getIpv6Prefix() != null) {
88                         final byte[] retBytes = new byte[CONTENT_LENGTH];
89                         ByteArray.copyWhole(prefix.getIpv6Prefix().getValue().getBytes(), retBytes, 0);
90                         retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
91                         return retBytes;
92                 }
93                 throw new IllegalArgumentException("No valid IpPrefix");
94         }
95
96         @Override
97         public int getType() {
98                 return TYPE;
99         }
100
101         public int getType6() {
102                 return TYPE6;
103         }
104 }