package com.cisco.trex.stateful.api.lowlevel;

import com.google.gson.JsonObject;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/** Java implementation for TRex python sdk ASTFAssociationRule class */
public class ASTFAssociationRule {

  private JsonObject fields = new JsonObject();
  private int port;

  /**
   * constructor
   *
   * @param ipStart
   * @param ipEnd
   * @param port
   */
  public ASTFAssociationRule(String ipStart, String ipEnd, int port) {
    this.port = port;
    fields.addProperty("port", port);
    if (!StringUtils.isEmpty(ipStart)) {
      fields.addProperty("ip_start", ipStart);
    }
    if (!StringUtils.isEmpty(ipStart)) {
      fields.addProperty("ip_end", ipEnd);
    }
  }

  /** @param port */
  public ASTFAssociationRule(int port) {
    this(null, null, port);
  }

  /**
   * constructor
   *
   * @param ipStart
   * @param ipEnd
   * @param port
   * @param l7List
   */
  public ASTFAssociationRule(String ipStart, String ipEnd, int port, List<Integer> l7List) {
    this(null, null, port);
    if (!l7List.isEmpty()) {
      fields.addProperty("l7_map", "{\"offset\": " + l7List.toString() + "}");
    }
  }

  /**
   * construct
   *
   * @param ipStart
   * @param ipEnd
   * @param port
   * @param l7Map
   */
  public ASTFAssociationRule(
      String ipStart, String ipEnd, int port, Map<String, List<Integer>> l7Map) {
    this(null, null, port);
    if (!l7Map.isEmpty()) {
      String l7_map =
          String.format(
              "{\"offset\": %s, \"value\": %s}",
              l7Map.get("offset").toString(), l7Map.get("value").toString());
      fields.addProperty("l7_map", l7_map);
    }
  }

  /**
   * get port
   *
   * @return port
   */
  public int getPort() {
    return this.port;
  }

  /**
   * to json format
   *
   * @return JsonObject
   */
  public JsonObject toJson() {
    return fields;
  }
}

@@ -40,16 +40,50 @@
 level;%0A%0A
+import com.google.gson.JsonArray;%0A
 import c
@@ -1088,34 +1088,38 @@
  %7B%0A    this(
-null, null
+ipStart, ipEnd
 , port);%0A   
@@ -1146,34 +1146,76 @@
 ()) %7B%0A      
-fields.add
+JsonObject l7_map = new JsonObject();%0A      addL7Map
 Property(%22l7
@@ -1215,59 +1215,68 @@
 rty(
-%22
+l7List, 
 l7_map
-%22
 , 
-%22%7B%5C
 %22offset
-%5C%22: %22 + l7List.toString() + %22%7D%22
+%22);%0A      fields.add(%22l7_map%22, l7_map
 );%0A 
@@ -1507,34 +1507,38 @@
  %7B%0A    this(
-null, null
+ipStart, ipEnd
 , port);%0A   
@@ -1572,225 +1572,488 @@
     
-String l7_map =%0A          String.format(%0A              %22%7B%5C%22offset%5C%22: %25s
+JsonObject l7_map = new JsonObject();%0A      addL7MapProperty(l7Map.get(%22offset%22), l7_map, %22offset%22);%0A      addL7MapProperty(l7Map.get(%22value%22), l7_map
 , 
-%5C
 %22value
-%5C%22: %25s%7D%22,%0A              l7Map.get(%22offset%22).toString(), l7Map.get(%22value%22).toString());%0A      fields
+%22);%0A      fields.add(%22l7_map%22, l7_map);%0A    %7D%0A  %7D%0A%0A  private void addL7MapProperty(List%3CInteger%3E values, JsonObject l7Map, String property) %7B%0A    if (values == null %7C%7C values.isEmpty()) %7B%0A      return;%0A    %7D%0A    JsonArray jsonArray = new JsonArray();%0A    values.forEach(jsonArray::add);%0A    l7Map
 .add
-P
+(p
 roperty
-(%22l7_map%22, l7_map);%0A    %7D
+, jsonArray);
 %0A  %7D

Update constructors in ASTFAssociationRule