enforce check-style for rsvp impl
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROIpv4PrefixSubobjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.subobject.ero;
9
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainer;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.list.SubobjectContainerBuilder;
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 PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
37
38     private static final int RESERVED = 1;
39
40     private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + RESERVED + 1;
41
42     @Override
43     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
44         Preconditions.checkArgument(buffer != null && buffer.isReadable(),
45             "Array of bytes is mandatory. Can't be null or empty.");
46         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
47         builder.setLoose(loose);
48         if (buffer.readableBytes() != CONTENT4_LENGTH) {
49             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
50         }
51         final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
52         final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(
53             Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length)));
54         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
55         return builder.build();
56     }
57
58     @Override
59     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
60         Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
61             "Unknown subobject instance. Passed %s. Needed IpPrefixCase.",
62             subobject.getSubobjectType().getClass());
63         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
64         final IpPrefix prefix = specObj.getIpPrefix();
65         Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null,
66             "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
67         if (prefix.getIpv6Prefix() != null) {
68             new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
69         } else {
70             final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
71             Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
72             writeIpv4Prefix(prefix.getIpv4Prefix(), body);
73             body.writeZero(RESERVED);
74             EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
75         }
76     }
77 }