*/
package org.opendaylight.protocol.pcep.impl.subobject;
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.UnsignedBytes;
+
import io.netty.buffer.ByteBuf;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
-import com.google.common.base.Preconditions;
-import com.google.common.primitives.UnsignedBytes;
-
/**
* Parser for {@link IpPrefixCase}
*/
public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
- public static final int TYPE = 1;
+ public static final int TYPE = 1;
- private static final int IP4_F_LENGTH = 4;
- private static final int PREFIX4_F_LENGTH = 1;
+ private static final int IP4_F_LENGTH = 4;
+ private static final int PREFIX4_F_LENGTH = 1;
- private static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
+ private static final int PREFIX4_F_OFFSET = IP4_F_LENGTH;
- private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1;
+ private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + PREFIX4_F_LENGTH + 1;
- @Override
- public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
- Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
- final SubobjectBuilder builder = new SubobjectBuilder();
- builder.setLoose(loose);
- if (buffer.readableBytes() != CONTENT4_LENGTH) {
- throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
- }
- final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
- IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, IP4_F_LENGTH), length)));
- builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
- return builder.build();
- }
+ @Override
+ public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
+ Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+ final SubobjectBuilder builder = new SubobjectBuilder();
+ builder.setLoose(loose);
+ if (buffer.readableBytes() != CONTENT4_LENGTH) {
+ throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
+ }
+ final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
+ IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
+ IP4_F_LENGTH), length)));
+ builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
+ return builder.build();
+ }
- @Override
- public byte[] serializeSubobject(final Subobject subobject) {
- if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
- throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
- + ". Needed IpPrefixCase.");
- }
- final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
- final IpPrefix prefix = specObj.getIpPrefix();
+ @Override
+ public byte[] serializeSubobject(final Subobject subobject) {
+ if (!(subobject.getSubobjectType() instanceof IpPrefixCase)) {
+ throw new IllegalArgumentException("Unknown subobject instance. Passed " + subobject.getSubobjectType().getClass()
+ + ". Needed IpPrefixCase.");
+ }
+ final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
+ final IpPrefix prefix = specObj.getIpPrefix();
- if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
- throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
- }
- if (prefix.getIpv6Prefix() != null) {
- return new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
- }
- final byte[] retBytes = new byte[CONTENT4_LENGTH];
- ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, 0);
- retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
- return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
- }
+ if (prefix.getIpv4Prefix() == null && prefix.getIpv6Prefix() == null) {
+ throw new IllegalArgumentException("Unknown AbstractPrefix instance. Passed " + prefix.getClass() + ".");
+ }
+ if (prefix.getIpv6Prefix() != null) {
+ return new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject);
+ }
+ final byte[] retBytes = new byte[CONTENT4_LENGTH];
+ ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, 0);
+ retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
+ return EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), retBytes);
+ }
}