BUG 2230: RSVP RRO Subobjects
[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
9 package org.opendaylight.protocol.rsvp.parser.impl.subobject.rro;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Prefix;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.rsvp.parser.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.Ipv6Util;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainer;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.list.SubobjectContainerBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
29
30 /**
31  * Parser for {@link IpPrefixCase}
32  */
33 public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
34
35     public static final int TYPE = 2;
36
37     private static final int PREFIX_F_LENGTH = 1;
38     private static final int FLAGS_SIZE = 8;
39
40     private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
41
42     private static final int CONTENT_LENGTH = Ipv6Util.IPV6_LENGTH + PREFIX_F_LENGTH + FLAGS_SIZE / Byte.SIZE;
43
44     private static final int LPA_F_OFFSET = 7;
45     private static final int LPIU_F_OFFSET = 6;
46
47     @Override
48     public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException {
49         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
50         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
51         if (buffer.readableBytes() != CONTENT_LENGTH) {
52             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
53         }
54         final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET);
55         final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer,
56             Ipv6Util.IPV6_LENGTH), length)));
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()).build());
62         return builder.build();
63     }
64
65     @Override
66     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
67         Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
68         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
69         final IpPrefix prefix = specObj.getIpPrefix();
70         final BitArray flags = new BitArray(FLAGS_SIZE);
71         flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
72         flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
73         final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
74         Preconditions.checkArgument(prefix.getIpv6Prefix() != null, "Ipv6Prefix is mandatory.");
75         writeIpv6Prefix(prefix.getIpv6Prefix(), body);
76         flags.toByteBuf(body);
77         RROSubobjectUtil.formatSubobject(TYPE, body, buffer);
78     }
79 }