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;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.flowspec.handlers.AbstractOperandParser;
19 import org.opendaylight.protocol.bgp.flowspec.handlers.BitmaskOperandParser;
20 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
21 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
22 import org.opendaylight.protocol.bgp.flowspec.handlers.Util;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.BitmaskOperand;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.TcpFlagsCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.TcpFlagsCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlags;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.flowspec.type.tcp.flags._case.TcpFlagsBuilder;
31 public final class FSTcpFlagsHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
32 static final int TCP_FLAGS_VALUE = 9;
34 private static void serializeTcpFlags(final List<TcpFlags> flags, final ByteBuf nlriByteBuf) {
35 for (final Iterator<TcpFlags> it = flags.iterator(); it.hasNext(); ) {
36 final TcpFlags flag = it.next();
37 final ByteBuf flagsBuf = Unpooled.buffer();
38 Util.writeShortest(flag.getValue(), flagsBuf);
39 BitmaskOperandParser.INSTANCE.serialize(flag.getOp(), flagsBuf.readableBytes(),
40 !it.hasNext(), nlriByteBuf);
41 nlriByteBuf.writeBytes(flagsBuf);
46 public void serializeType(final FlowspecType fsType, final ByteBuf output) {
47 Preconditions.checkArgument(fsType instanceof TcpFlagsCase, "TcpFlagsCase class is mandatory!");
48 output.writeByte(TCP_FLAGS_VALUE);
49 serializeTcpFlags(((TcpFlagsCase) fsType).getTcpFlags(), output);
53 public FlowspecType parseType(final ByteBuf buffer) {
54 requireNonNull(buffer, "input buffer is null, missing data to parse.");
55 return new TcpFlagsCaseBuilder().setTcpFlags(parseTcpFlags(buffer)).build();
58 private static List<TcpFlags> parseTcpFlags(final ByteBuf nlri) {
59 final List<TcpFlags> flags = new ArrayList<>();
61 // we can do this as all fields will be rewritten in the cycle
62 final TcpFlagsBuilder builder = new TcpFlagsBuilder();
64 final byte b = nlri.readByte();
65 final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
67 final short length = AbstractOperandParser.parseLength(b);
68 builder.setValue(ByteArray.bytesToInt(ByteArray.readBytes(nlri, length)));
69 end = op.isEndOfList();
70 flags.add(builder.build());