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.EROSubobjectParser;
16 import org.opendaylight.protocol.pcep.spi.EROSubobjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.EROSubobjectUtil;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.protocol.util.Ipv4Util;
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.explicit.route.object.ero.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.IpPrefixSubobject;
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;
30 * Parser for {@link IpPrefixCase}.
32 public class EROIpv4PrefixSubobjectParser implements EROSubobjectParser, EROSubobjectSerializer {
33 public static final int TYPE = 1;
35 private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
36 private static final int RESERVED = 1;
37 private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + RESERVED + 1;
40 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
41 checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
42 if (buffer.readableBytes() != CONTENT4_LENGTH) {
43 throw new PCEPDeserializerException(
44 "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
46 final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
47 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(
48 new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length)));
49 final SubobjectBuilder builder = new SubobjectBuilder()
51 .setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
52 return builder.build();
56 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
57 checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase,
58 "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
59 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
60 final IpPrefix prefix = specObj.getIpPrefix();
61 final Ipv6Prefix ipv6prefix = prefix.getIpv6Prefix();
62 if (ipv6prefix != null) {
63 EROIpv6PrefixSubobjectParser.serializeSubobject(buffer, subobject, ipv6prefix);
67 final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
68 checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
69 Ipv4Util.writeIpv4Prefix(prefix.getIpv4Prefix(), body);
70 body.writeZero(RESERVED);
71 EROSubobjectUtil.formatSubobject(TYPE, subobject.getLoose(), body, buffer);