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.impl.subobject;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Prefix;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
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.pcep.spi.PCEPDeserializerException;
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.rev100924.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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 {
34 public static final int TYPE = 1;
36 private static final int PREFIX4_F_OFFSET = Ipv4Util.IP4_LENGTH;
38 private static final int RESERVED = 1;
40 private static final int CONTENT4_LENGTH = PREFIX4_F_OFFSET + RESERVED + 1;
43 public Subobject parseSubobject(final ByteBuf buffer, final boolean loose) throws PCEPDeserializerException {
44 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
45 final SubobjectBuilder builder = new SubobjectBuilder();
46 builder.setLoose(loose);
47 if (buffer.readableBytes() != CONTENT4_LENGTH) {
48 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";");
50 final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET);
51 final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix(new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer,
52 Ipv4Util.IP4_LENGTH), length)));
53 builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build());
54 return builder.build();
58 public void serializeSubobject(final Subobject subobject, final ByteBuf buffer) {
59 Preconditions.checkArgument(subobject.getSubobjectType() instanceof IpPrefixCase, "Unknown subobject instance. Passed %s. Needed IpPrefixCase.", subobject.getSubobjectType().getClass());
60 final IpPrefixSubobject specObj = ((IpPrefixCase) subobject.getSubobjectType()).getIpPrefix();
61 final IpPrefix prefix = specObj.getIpPrefix();
62 Preconditions.checkArgument(prefix.getIpv4Prefix() != null || prefix.getIpv6Prefix() != null, "Unknown AbstractPrefix instance. Passed %s.", prefix.getClass());
63 if (prefix.getIpv6Prefix() != null) {
64 new EROIpv6PrefixSubobjectParser().serializeSubobject(subobject, buffer);
66 final ByteBuf body = Unpooled.buffer(CONTENT4_LENGTH);
67 Preconditions.checkArgument(prefix.getIpv4Prefix() != null, "Ipv4Prefix is mandatory.");
68 writeIpv4Prefix(prefix.getIpv4Prefix(), body);
69 body.writeZero(RESERVED);
70 EROSubobjectUtil.formatSubobject(TYPE, subobject.isLoose(), body, buffer);