示例#1
0
  protected LBPool jsonToPool(String json) throws IOException {
    if (json == null) return null;

    MappingJsonFactory f = new MappingJsonFactory();
    JsonParser jp;
    LBPool pool = new LBPool();

    try {
      jp = f.createJsonParser(json);
    } catch (JsonParseException e) {
      throw new IOException(e);
    }

    jp.nextToken();
    if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
      throw new IOException("Expected START_OBJECT");
    }

    while (jp.nextToken() != JsonToken.END_OBJECT) {
      if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
        throw new IOException("Expected FIELD_NAME");
      }

      String n = jp.getCurrentName();
      jp.nextToken();
      if (jp.getText().equals("")) continue;
      if (n.equals("id")) {
        pool.id = jp.getText();
        continue;
      }
      if (n.equals("tenant_id")) {
        pool.tenantId = jp.getText();
        continue;
      }
      if (n.equals("name")) {
        pool.name = jp.getText();
        continue;
      }
      if (n.equals("network_id")) {
        pool.netId = jp.getText();
        continue;
      }
      if (n.equals("lb_method")) {
        pool.lbMethod = Short.parseShort(jp.getText());
        continue;
      }
      if (n.equals("protocol")) {
        String tmp = jp.getText();
        if (tmp.equalsIgnoreCase("TCP")) {
          pool.protocol = IPv4.PROTOCOL_TCP;
        } else if (tmp.equalsIgnoreCase("UDP")) {
          pool.protocol = IPv4.PROTOCOL_UDP;
        } else if (tmp.equalsIgnoreCase("ICMP")) {
          pool.protocol = IPv4.PROTOCOL_ICMP;
        }
        continue;
      }
      if (n.equals("vip_id")) {
        pool.vipId = jp.getText();
        continue;
      }

      log.warn("Unrecognized field {} in " + "parsing Pools", jp.getText());
    }
    jp.close();

    return pool;
  }
示例#2
0
  /**
   * Auxiliary method obtain the JSON fields and their values
   *
   * @param fmJson
   * @param sv
   */
  public static void jsonConverter(String fmJson, IStatsService sv) {

    MappingJsonFactory f = new MappingJsonFactory();
    JsonParser jp;
    try {
      try {
        jp = f.createJsonParser(fmJson);
      } catch (JsonParseException e) {
        throw new IOException(e);
      }

      jp.nextToken();
      if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
        throw new IOException("Expected START_OBJECT");
      }

      while (jp.nextToken() != JsonToken.END_OBJECT) {
        if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
          throw new IOException("Expected FIELD_NAME");
        }

        String n = jp.getCurrentName();
        jp.nextToken();
        if (jp.getText().equals("")) {
          continue;
        }

        // This assumes user having dpid info for involved switches
        else if (n.equalsIgnoreCase("srcDpid")) {
          try {
            srcId = DatapathId.of(jp.getText());
          } catch (NumberFormatException e) {
            log.error("Unable to parse switch DPID: {}", jp.getText());
            // TODO should return some error message via HTTP message
          }
        } else if (n.equalsIgnoreCase("dstDpid")) {
          try {
            dstId = DatapathId.of(jp.getText());
          } catch (NumberFormatException e) {
            log.error("Unable to parse switch DPID: {}", jp.getText());
            // TODO should return some error message via HTTP message
          }
        } else if (n.equalsIgnoreCase("srcIp")) {
          try {
            srcIp = IPv4Address.of(jp.getText());
          } catch (NumberFormatException e) {
            log.error("Unable to parse IP srcIp: {}", jp.getText());
            // TODO should return some error message via HTTP message
          }
        } else if (n.equalsIgnoreCase("dstIp")) {
          try {
            dstIp = IPv4Address.of(jp.getText());
          } catch (NumberFormatException e) {
            log.error("Unable to parse IP dstIp: {}", jp.getText());
            // TODO should return some error message via HTTP message
          }
        } else if (n.equalsIgnoreCase("loss")) {
          try {
            loss = Integer.parseInt(jp.getText());
          } catch (NumberFormatException e) {
            log.error("Unable to parse loss: {}", jp.getText());
            // TODO should return some error message via HTTP message
          }
        }
      }

    } catch (IOException e) {
      log.error("Unable to parse JSON string: {}", e);
    }
  }