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;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
16 import java.util.BitSet;
18 import org.opendaylight.protocol.concepts.Ipv4Util;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
21 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
22 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.Subobject;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.reported.route.object.rro.SubobjectBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.IpPrefixSubobject;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.IpPrefixCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
33 * Parser for {@link IpPrefixCase}
35 public class RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
37 public static final int TYPE = 1;
39 private static final int PREFIX_F_LENGTH = 1;
40 private static final int FLAGS_F_LENGTH = 1;
42 private static final int PREFIX4_F_OFFSET = 0 + Ipv4Util.IP4_LENGTH;
44 private static final int CONTENT4_LENGTH = Ipv4Util.IP4_LENGTH + PREFIX_F_LENGTH + FLAGS_F_LENGTH;
46 private static final int LPA_F_OFFSET = 7;
47 private static final int LPIU_F_OFFSET = 6;
50 public Subobject parseSubobject(final ByteBuf buffer) throws PCEPDeserializerException {
51 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
52 if (buffer.readableBytes() != CONTENT4_LENGTH) {
53 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
55 final SubobjectBuilder builder = new SubobjectBuilder();
56 final int length = UnsignedBytes.toInt(buffer.getByte(PREFIX4_F_OFFSET));
57 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefix prefix = new IpPrefixBuilder().setIpPrefix(
58 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))).build();
59 buffer.readerIndex(buffer.readerIndex() + PREFIX_F_LENGTH);
60 final BitSet flags = ByteArray.bytesToBitSet(ByteArray.readBytes(buffer, FLAGS_F_LENGTH));
61 builder.setProtectionAvailable(flags.get(LPA_F_OFFSET));
62 builder.setProtectionInUse(flags.get(LPIU_F_OFFSET));
63 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix).build());
64 return builder.build();
68 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
69 Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
70 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
71 final IpPrefix prefix = specObj.getIpPrefix();
72 Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
73 if (prefix.getIpv6Prefix() != null) {
74 new RROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
76 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
77 if (subobject.isProtectionAvailable() != null) {
78 flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
80 if (subobject.isProtectionInUse() != null) {
81 flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
83 final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
84 body.writeBytes(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()));
85 body.writeBytes(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH));
86 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);