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.impl.subobject;
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.BitSet;
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.ByteArray;
20 import org.opendaylight.protocol.util.Ipv6Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
30 * Parser for {@link IpPrefixCase}
32 public class RROIpv6PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
34 public static final int TYPE = 2;
36 private static final int PREFIX_F_LENGTH = 1;
37 private static final int FLAGS_F_LENGTH = 1;
39 private static final int PREFIX_F_OFFSET = Ipv6Util.IPV6_LENGTH;
41 private static final int CONTENT_LENGTH = Ipv6Util.IPV6_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
43 private static final int LPA_F_OFFSET = 7;
44 private static final int LPIU_F_OFFSET = 6;
47 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
48 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
49 final SubobjectBuilder builder = new SubobjectBuilder();
50 if (buffer.readableBytes() != CONTENT_LENGTH) {
51 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
53 final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX_F_OFFSET));
54 IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer,
55 Ipv6Util.IPV6_LENGTH), length)));
56 buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
57 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
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();
65 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
66 Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
67 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
68 final IpPrefix prefix = specObj.getIpPrefix();
69 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
70 if (subobject.isProtectionAvailable() != null) {
71 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
73 if (subobject.isProtectionInUse() != null) {
74 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
76 final ByteBuf body = Unpooled.buffer(CONTENT_LENGTH);
77 body.writeBytes(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()));
78 body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
79 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);