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.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
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.pcep.types.rev181109.exclude.route.object.xro.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
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.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
31 * Parser for {@link IpPrefixCase}.
33 public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
34 public static final int TYPE = 2;
36 private static final int PREFIX_F_LENGTH = 1;
37 private static final int PREFIX6_F_OFFSET = Ipv6Util.IPV6_LENGTH;
38 private static final int CONTENT6_LENGTH = PREFIX6_F_OFFSET + PREFIX_F_LENGTH + 1;
41 public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
42 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
43 if (buffer.readableBytes() != CONTENT6_LENGTH) {
44 throw new PCEPDeserializerException(
45 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
47 final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET);
48 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
49 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
50 buffer.skipBytes(PREFIX_F_LENGTH);
51 final SubobjectBuilder builder = new SubobjectBuilder()
52 .setMandatory(mandatory)
53 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build())
54 .setAttribute(Attribute.forValue(buffer.readUnsignedByte()));
55 return builder.build();
59 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
60 checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
61 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
62 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
63 final Ipv6Prefix ipv6Prefix = specObj.getIpPrefix().getIpv6Prefix();
64 checkArgument(ipv6Prefix != null, "Ipv6Prefix is mandatory.");
65 serializeSubobject(buffer, subobject, ipv6Prefix);
68 static void serializeSubobject(final ByteBuf buffer, final Subobject subobject, final Ipv6Prefix ipv6Prefix) {
69 final ByteBuf body = Unpooled.buffer(CONTENT6_LENGTH);
70 Ipv6Util.writeIpv6Prefix(ipv6Prefix, body);
71 final Attribute attribute = subobject.getAttribute();
72 checkArgument(attribute != null, "Attribute is mandatory.");
73 body.writeByte(attribute.getIntValue());
74 XROSubobjectUtil.formatSubobject(TYPE, subobject.getMandatory(), body, buffer);