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;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.XROSubobjectParser;
18 import org.opendaylight.protocol.pcep.spi.XROSubobjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.XROSubobjectUtil;
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.exclude.route.object.xro.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.exclude.route.object.xro.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
30 import org.opendaylight.yangtools.yang.common.Uint8;
33 * Parser for {@link IpPrefixCase}.
35 public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
37 public static final int TYPE = 2;
39 private static final int PREFIX_F_LENGTH = 1;
41 private static final int PREFIX6_F_OFFSET = Ipv6Util.IPV6_LENGTH;
43 private static final int CONTENT6_LENGTH = PREFIX6_F_OFFSET + PREFIX_F_LENGTH + 1;
46 public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException {
47 Preconditions.checkArgument(buffer != null && buffer.isReadable(),
48 "Array of bytes is mandatory. Can't be null or empty.");
49 if (buffer.readableBytes() != CONTENT6_LENGTH) {
50 throw new PCEPDeserializerException(
51 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
53 final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET);
54 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
55 new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
56 buffer.skipBytes(PREFIX_F_LENGTH);
57 final SubobjectBuilder builder = new SubobjectBuilder()
58 .setMandatory(mandatory)
59 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build())
60 .setAttribute(Attribute.forValue(buffer.readUnsignedByte()));
61 return builder.build();
65 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
66 Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
67 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
68 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
69 final IpPrefix prefix = specObj.getIpPrefix();
70 final ByteBuf body = Unpooled.buffer(CONTENT6_LENGTH);
71 Preconditions.checkArgument(prefix.getIpv6Prefix() != null, "Ipv6Prefix is mandatory.");
72 writeIpv6Prefix(prefix.getIpv6Prefix(), body);
73 Preconditions.checkArgument(subobject.getAttribute() != null, "Attribute is mandatory.");
74 writeUnsignedByte(Uint8.valueOf(subobject.getAttribute().getIntValue()), body);
75 XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);