b20246f87422dcadc862ab59e60667829a7a79ff
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / FSIpv6SourcePrefixHandler.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.flowspec.handlers;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 public final class FSIpv6SourcePrefixHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
23     public static final int SOURCE_PREFIX_VALUE = 2;
24
25     @Override
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);
30     }
31
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);
36         output.writeByte(0);
37         output.writeBytes(bytes, 0, Ipv4Util.prefixBitsToBytes(Byte.toUnsignedInt(prefixBits)));
38     }
39
40     @Override
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();
44     }
45
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);
51     }
52 }