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 / RROIpv6PrefixSubobjectParser.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.Ipv6Util;
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 RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
33
34     public static final int TYPE = 2;
35
36     private static final int PREFIX_F_LENGTH = 1;
37     private static final int FLAGS_SIZE = 8;
38
39     private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
40
41     private static final int CONTENT_LENGTH = Ipv6Util.IPV6_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         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
50         if (buffer.readableBytes() != CONTENT_LENGTH) {
51             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
52         }
53         final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET);
54         final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(
55             Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
56         buffer.skipBytes(PREFIX_F_LENGTH);
57         final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
58         builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
59         builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
60         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
61         return builder.build();
62     }
63
64     @Override
65     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
66         checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
67             "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
68         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
69         final Ipv6Prefix ipv6Prefix = specObj.getIpPrefix().getIpv6Prefix();
70         checkArgument(ipv6Prefix != null, "Ipv6Prefix is mandatory.");
71         serializeSubobject(buffer, subobject, ipv6Prefix);
72     }
73
74     static void serializeSubobject(final ByteBuf buffer, final SubobjectContainer subobject,
75             final Ipv6Prefix ipv6Prefix) {
76         final BitArray flags = new BitArray(FLAGS_SIZE);
77         flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
78         flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
79         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
80         Ipv6Util.writeIpv6Prefix(ipv6Prefix, body);
81         flags.toByteBuf(body);
82         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
83     }
84 }