BUG-47 : PCEP migration to generated DTOs.
[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.util.ByteArray;
11 import org.opendaylight.protocol.concepts.Ipv4Util;
12 import org.opendaylight.protocol.concepts.Ipv6Util;
13 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
14 import org.opendaylight.protocol.pcep.spi.SubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.SubobjectSerializer;
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.rsvp.rev130820.CSubobject;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.route.subobjects.subobject.type.IpPrefixBuilder;
20
21 import com.google.common.primitives.UnsignedBytes;
22
23 /**
24  * Parser for {@link IpPrefixSubobject}
25  */
26 public class EROIpPrefixSubobjectParser implements SubobjectParser, SubobjectSerializer {
27         
28         public static final int TYPE = 32;
29         
30         public static final int IP4_F_LENGTH = 4;
31         public static final int PREFIX4_F_LENGTH = 1;
32
33         public static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
34
35         public static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1; //added reserved field of size 1 byte
36         
37         public static final int IP_F_LENGTH = 16;
38         public static final int PREFIX_F_LENGTH = 1;
39
40         public static final int IP_F_OFFSET = 0;
41         public static final int PREFIX_F_OFFSET = IP_F_OFFSET + IP_F_LENGTH;
42
43         public static final int CONTENT_LENGTH = PREFIX_F_OFFSET + PREFIX_F_LENGTH + 1; // added reserved field of size 1 byte
44
45         public IpPrefixSubobject parseSubobject(byte[] buffer) throws PCEPDeserializerException {
46                 if (buffer == null || buffer.length == 0)
47                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
48
49                 if (buffer.length == CONTENT4_LENGTH) {
50                         final int length = UnsignedBytes.toInt(buffer[PREFIX4_F_OFFSET]);
51                         return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP4_F_LENGTH), length))).build();
52                 } else if (buffer.length == CONTENT_LENGTH) {
53                         final int length = UnsignedBytes.toInt(buffer[PREFIX_F_OFFSET]);
54                         return new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.subByte(buffer, 0, IP_F_LENGTH), length))).build();
55                 } else
56                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.length + ";");
57         }
58
59         public byte[] serializeSubobject(CSubobject subobject) {
60                 if (!(subobject instanceof IpPrefixSubobject))
61                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getClass()
62                                         + ". Needed IpPrefixSubobject.");
63
64                 final IpPrefixSubobject specObj = (IpPrefixSubobject) subobject;
65                 final IpPrefix prefix = specObj.getIpPrefix();
66
67                 if (prefix.getIpv4Prefix() != null) {
68                         final byte[] retBytes = new byte[CONTENT4_LENGTH];
69                         ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
70                         return retBytes;
71                 } else if (prefix.getIpv6Prefix() != null) {
72                         final byte[] retBytes = new byte[CONTENT4_LENGTH];
73                         ByteArray.copyWhole(prefix.getIpv4Prefix().getValue().getBytes(), retBytes, 0);
74                         return retBytes;
75                 //retBytes[PREFIX_F_OFFSET] = ByteArray.intToBytes(prefix.getLength())[Integer.SIZE / Byte.SIZE - 1];
76                 } 
77                 return new byte[0];
78         }
79
80         @Override
81         public int getType() {
82                 return TYPE;
83         }
84 }