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.bgp.flowspec.handlers;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.protocol.util.Ipv4Util;
16 import org.opendaylight.protocol.util.Ipv6Util;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.group.ipv6.flowspec.flowspec.type.SourceIpv6PrefixCaseBuilder;
22 public final class FSIpv6SourcePrefixHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
23 public static final int SOURCE_PREFIX_VALUE = 2;
26 public void serializeType(final FlowspecType value, final ByteBuf output) {
27 Preconditions.checkArgument(value instanceof SourceIpv6PrefixCase, "SourceIpv6PrefixCase class is mandatory!");
28 output.writeByte(SOURCE_PREFIX_VALUE);
29 writePrefix(((SourceIpv6PrefixCase) value).getSourcePrefix(), output);
32 static void writePrefix(final Ipv6Prefix prefix, final ByteBuf output) {
33 final byte[] bytes = Ipv6Util.bytesForPrefix(prefix);
34 final byte prefixBits = bytes[Ipv6Util.IPV6_LENGTH];
35 output.writeByte(prefixBits);
37 output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
41 public FlowspecType parseType(final ByteBuf buffer) {
42 requireNonNull(buffer, "input buffer is null, missing data to parse.");
43 return new SourceIpv6PrefixCaseBuilder().setSourcePrefix(parseIpv6Prefix(buffer)).build();
46 static Ipv6Prefix parseIpv6Prefix(final ByteBuf nlri) {
47 final int bitLength = nlri.readUnsignedByte();
48 nlri.readUnsignedByte();
49 // FIXME: this does not look right if bitLenght % Byte.SIZE != 0
50 return Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, bitLength / Byte.SIZE), bitLength);