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