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