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 com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.RROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.RROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.RROSubobjectUtil;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
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 RROIpv4PrefixSubobjectParser implements RROSubobjectParser, RROSubobjectSerializer {
35 public static final int TYPE = 1;
37 private static final int PREFIX_F_LENGTH = 1;
38 private static final int FLAGS_SIZE = 8;
40 private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
42 private static final int CONTENT4_LENGTH = Ipv4Util.IP4_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 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
50 if (buffer.readableBytes() != CONTENT4_LENGTH) {
51 throw new PCEPDeserializerException(
52 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
54 final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
55 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820._record.route.subobjects
56 .subobject.type.ip.prefix._case.IpPrefix prefix = new IpPrefixBuilder().setIpPrefix(
57 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length)))
59 buffer.skipBytes(PREFIX_F_LENGTH);
60 final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE);
61 return new SubobjectBuilder()
62 .setProtectionAvailable(flags.get(LPA_F_OFFSET))
63 .setProtectionInUse(flags.get(LPIU_F_OFFSET))
64 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix).build())
69 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
70 checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
71 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
72 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
73 final IpPrefix prefix = specObj.getIpPrefix();
74 final Ipv6Prefix ipv6Prefix = prefix.getIpv6Prefix();
75 if (ipv6Prefix != null) {
76 RROIpv6PrefixSubobjectParser.serializeSubobject(buffer, subobject, ipv6Prefix);
80 final BitArray flags = new BitArray(FLAGS_SIZE);
81 flags.set(LPA_F_OFFSET, subobject.getProtectionAvailable());
82 flags.set(LPIU_F_OFFSET, subobject.getProtectionInUse());
83 final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
84 checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
85 Ipv4Util.writeIpv4Prefix(prefix.getIpv4Prefix(), body);
86 flags.toByteBuf(body);
87 RROSubobjectUtil.formatSubobject(TYPE, body, buffer);