Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / rro / RROIpv4PrefixSubobjectParser.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.rro;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
16 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
17 import org.opendaylight.protocol.util.BitArray;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.protocol.util.Ipv4Util;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
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.record.route.subobjects.list.SubobjectContainer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
28
29 /**
30  * Parser for {@link IpPrefixCase}.
31  */
32 public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33
34     public static final int TYPE = 1;
35
36     private static final int PREFIX_F_LENGTH = 1;
37     private static final int FLAGS_SIZE = 8;
38
39     private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
40
41     private static final int CONTENT4_LENGTH = Ipv4Util.IP4_LENGTH + PREFIX_F_LENGTH + FLAGS_SIZE / Byte.SIZE;
42
43     private static final int LPA_F_OFFSET = 7;
44     private static final int LPIU_F_OFFSET = 6;
45
46     @Override
47     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
48         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
49         if (buffer.readableBytes() != CONTENT4_LENGTH) {
50             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
51         }
52         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
53         final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
54         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects
55             .subobject.type.ip.prefix._case.IpPrefix prefix = new IpPrefixBuilder().setIpPrefix(
56             new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))).build();
57         buffer.skipBytes(PREFIX_F_LENGTH);
58         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
59         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
60         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
61         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix).build());
62         return builder.build();
63     }
64
65     @Override
66     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
67         checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
68             "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
69         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
70         final IpPrefix prefix = specObj.getIpPrefix();
71         final Ipv6Prefix ipv6Prefix = prefix.getIpv6Prefix();
72         if (ipv6Prefix != null) {
73             RROIpv6PrefixSubobjectParser.serializeSubobject(buffer, subobject, ipv6Prefix);
74             return;
75         }
76
77         final BitArray flags = new BitArray(FLAGS_SIZE);
78         flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
79         flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
80         final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
81         checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
82         Ipv4Util.writeIpv4Prefix(prefix.getIpv4Prefix(), body);
83         flags.toByteBuf(body);
84         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
85     }
86 }