Bug 5503 - remove package cyclic dependency in BGP-FS
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / FSTcpFlagsHandler.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;
9
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;
27
28 public final class FSTcpFlagsHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
29     public static final int TCP_FLAGS_VALUE = 9;
30
31     @Override
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);
36     }
37
38     @Override
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();
42     }
43
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);
50         }
51     }
52
53     private static List<TcpFlags> parseTcpFlags(final ByteBuf nlri) {
54         final List<TcpFlags> flags = new ArrayList<>();
55         boolean end = false;
56         // we can do this as all fields will be rewritten in the cycle
57         final TcpFlagsBuilder builder = new TcpFlagsBuilder();
58         while (!end) {
59             final byte b = nlri.readByte();
60             final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
61             builder.setOp(op);
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());
66         }
67         return flags;
68     }
69 }