2 * Copyright (c) 2015 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.rsvp.parser.impl.subobject.xro;
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.rsvp.parser.spi.RSVPParsingException;
15 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectParser;
16 import org.opendaylight.protocol.rsvp.parser.spi.XROSubobjectSerializer;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.protocol.util.Ipv6Util;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.ExcludeRouteSubobjects.Attribute;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.SubobjectType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.basic.explicit.route.subobjects.subobject.type.ip.prefix._case.IpPrefixBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainer;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.exclude.route.object.exclude.route.object.SubobjectContainerBuilder;
32 * Parser for {@link IpPrefixCase}.
34 public class XROIpv6PrefixSubobjectParser implements XROSubobjectParser, XROSubobjectSerializer {
35 public static final int TYPE = 2;
37 private static final int PREFIX_F_LENGTH = 1;
38 private static final int PREFIX6_F_OFFSET = Ipv6Util.IPV6_LENGTH;
39 private static final int CONTENT6_LENGTH = PREFIX6_F_OFFSET + PREFIX_F_LENGTH + 1;
42 public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory)
43 throws RSVPParsingException {
44 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
45 final SubobjectContainerBuilder builder = new SubobjectContainerBuilder();
46 builder.setMandatory(mandatory);
47 if (buffer.readableBytes() != CONTENT6_LENGTH) {
48 throw new RSVPParsingException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
50 final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET);
51 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(
52 Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length)));
53 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
54 buffer.skipBytes(PREFIX_F_LENGTH);
55 builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte()));
56 return builder.build();
60 public void serializeSubobject(final SubobjectContainer subobject, final ByteBuf buffer) {
61 final SubobjectType type = subobject.getSubobjectType();
62 checkArgument(type instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.",
64 final IpPrefixSubobject specObj = ((IpPrefixCase) type).getIpPrefix();
65 final Ipv6Prefix ipv6Prefix = specObj.getIpPrefix().getIpv6Prefix();
66 checkArgument(ipv6Prefix != null, "Ipv6Prefix is mandatory.");
67 serializeSubobject(buffer, subobject, ipv6Prefix);
70 static void serializeSubobject(final ByteBuf buffer, final SubobjectContainer subobject,
71 final Ipv6Prefix ipv6Prefix) {
72 final ByteBuf body = Unpooled.buffer(CONTENT6_LENGTH);
73 Ipv6Util.writeIpv6Prefix(ipv6Prefix, body);
74 final Attribute attribute = subobject.getAttribute();
75 checkArgument(attribute != null, "Attribute is mandatory.");
76 body.writeByte(attribute.getIntValue());
77 XROSubobjectUtil.formatSubobject(TYPE, subobject.isMandatory(), body, buffer);