2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.subobject;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Prefix;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
18 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
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.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.reported.route.object.rro.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
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;
31 * Parser for {@link IpPrefixCase}.
33 public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
35 public static final int TYPE = 2;
37 private static final int PREFIX_F_LENGTH = 1;
38 private static final int FLAGS_SIZE = 8;
40 private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
42 private static final int CONTENT_LENGTH = Ipv6Util.IPV6_LENGTH + PREFIX_F_LENGTH + FLAGS_SIZE / Byte.SIZE;
44 private static final int LPA_F_OFFSET = 7;
45 private static final int LPIU_F_OFFSET = 6;
48 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
49 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
50 "Array of bytes is mandatory. Can't be null or empty.");
51 if (buffer.readableBytes() != CONTENT_LENGTH) {
52 throw new PCEPDeserializerException(
53 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
55 final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET);
56 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
57 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
58 buffer.skipBytes(PREFIX_F_LENGTH);
59 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
60 final SubobjectBuilder builder = new SubobjectBuilder()
61 .setProtectionAvailable(flags.get(LPA_F_OFFSET))
62 .setProtectionInUse(flags.get(LPIU_F_OFFSET))
63 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
64 return builder.build();
68 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
69 Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
70 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
71 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
72 final IpPrefix prefix = specObj.getIpPrefix();
73 final BitArray flags = new BitArray(FLAGS_SIZE);
74 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
75 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
76 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
77 Preconditions.checkArgument(prefix.getIpv6Prefix() != null, "Ipv6Prefix is mandatory.");
78 writeIpv6Prefix(prefix.getIpv6Prefix(), body);
79 flags.toByteBuf(body);
80 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);