f54340d2f07eed8986ea65f8179095841182cd0b
[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 static java.util.Objects.requireNonNull;
11
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;
30
31 public final class FSTcpFlagsHandler implements FlowspecTypeParser, FlowspecTypeSerializer {
32     static final int TCP_FLAGS_VALUE = 9;
33
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);
42         }
43     }
44
45     @Override
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);
50     }
51
52     @Override
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();
56     }
57
58     private static List<TcpFlags> parseTcpFlags(final ByteBuf nlri) {
59         final List<TcpFlags> flags = new ArrayList<>();
60         boolean end = false;
61         // we can do this as all fields will be rewritten in the cycle
62         final TcpFlagsBuilder builder = new TcpFlagsBuilder();
63         while (!end) {
64             final byte b = nlri.readByte();
65             final BitmaskOperand op = BitmaskOperandParser.INSTANCE.parse(b);
66             builder.setOp(op);
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());
71         }
72         return flags;
73     }
74 }