Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / subobject / ero / EROIpv6PrefixSubobjectParser.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 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.EROSubobjectParser;
15 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectSerializer;
16 import org.opendaylight.protocol.rsvp.parser.spi.EROSubobjectUtil;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
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.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 EROIpv6PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33     public static final int TYPE = 2;
34
35     private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
36     private static final int RESERVED = 1;
37     private static final int CONTENT_LENGTH = PREFIX_F_OFFSET + RESERVED + 1;
38
39     @Override
40     public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException {
41         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42         final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
43         builder.setLoose(loose);
44         if (buffer.readableBytes() != CONTENT_LENGTH) {
45             throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
46         }
47         final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET);
48         final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
49             new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
50         builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
51         return builder.build();
52     }
53
54     @Override
55     public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
56         checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
57             "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
58         final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
59         final Ipv6Prefix ipv6prefix = specObj.getIpPrefix().getIpv6Prefix();
60         checkArgument(ipv6prefix != null, "Ipv6Prefix is mandatory.");
61         serializeSubobject(buffer, subobject, ipv6prefix);
62     }
63
64     static void serializeSubobject(final ByteBuf buffer, final SubobjectContainer subobject,
65             final Ipv6Prefix ipv6prefix) {
66         final ByteBuf body = Unpooled.buffer();
67         Ipv6Util.writeIpv6Prefix(ipv6prefix, body);
68         body.writeZero(RESERVED);
69         EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);
70     }
71 }