BUG-612 : switched ERO to ByteBuf.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / EROIpv4PrefixSubobjectParser.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 io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.protocol.concepts.Ipv4Util;
13 import org.opendaylight.protocol.pcep.impl.object.EROSubobjectUtil;
14 import org.opendaylight.protocol.pcep.spi.EROSubobjectParser;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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.explicit.route.object.ero.Subobject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
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.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.primitives.UnsignedBytes;
28
29 /**
30  * Parser for {@link IpPrefixCase}
31  */
32 public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33
34         public static final int TYPE = 1;
35
36         private static final int IP4_F_LENGTH = 4;
37         private static final int PREFIX4_F_LENGTH = 1;
38
39         private static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
40
41         private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1;
42
43         @Override
44         public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
45                 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
46                 final SubobjectBuilder builder = new SubobjectBuilder();
47                 builder.setLoose(loose);
48                 if (buffer.readableBytes() != CONTENT4_LENGTH) {
49                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
50                 }
51                 final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
52                 IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH), length)));
53                 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
54                 return builder.build();
55         }
56
57         @Override
58         public byte[] serializeSubobject(final Subobject subobject) {
59                 if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
60                         throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
61                                         + ". Needed IpPrefixCase.");
62                 }
63                 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
64                 final IpPrefix prefix = specObj.getIpPrefix();
65
66                 if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
67                         throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
68                 }
69                 if (prefix.getIpv6Prefix() != null) {
70                         return new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
71                 }
72                 final byte[] retBytes = new byte[CONTENT4_LENGTH];
73                 ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, 0);
74                 retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
75                 return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
76         }
77 }