// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
// vim: ts=8 sw=2 sts=2 expandtab

#include "cls/rgw/cls_rgw_ops.h"

#include "common/Formatter.h"
#include "common/ceph_json.h"
#include "include/utime.h"

using std::list;
using std::map;

using ceph::Formatter;

void rgw_cls_tag_timeout_op::dump(Formatter *f) const
{
  f->dump_int("tag_timeout", tag_timeout);
}

list<rgw_cls_tag_timeout_op> rgw_cls_tag_timeout_op::generate_test_instances()
{
  list<rgw_cls_tag_timeout_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().tag_timeout = 23323;
  return ls;
}

void cls_rgw_gc_set_entry_op::dump(Formatter *f) const
{
  f->dump_unsigned("expiration_secs", expiration_secs);
  f->open_object_section("obj_info");
  info.dump(f);
  f->close_section();
}

list<cls_rgw_gc_set_entry_op> cls_rgw_gc_set_entry_op::generate_test_instances()
{
  list<cls_rgw_gc_set_entry_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().expiration_secs = 123;
  return ls;
}

void cls_rgw_gc_defer_entry_op::dump(Formatter *f) const
{
  f->dump_unsigned("expiration_secs", expiration_secs);
  f->dump_string("tag", tag);
}

list<cls_rgw_gc_defer_entry_op> cls_rgw_gc_defer_entry_op::generate_test_instances()
{
  list<cls_rgw_gc_defer_entry_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().expiration_secs = 123;
  ls.back().tag = "footag";
  return ls;
}

void cls_rgw_gc_list_op::dump(Formatter *f) const
{
  f->dump_string("marker", marker);
  f->dump_unsigned("max", max);
  f->dump_bool("expired_only", expired_only);
}

list<cls_rgw_gc_list_op> cls_rgw_gc_list_op::generate_test_instances()
{
  list<cls_rgw_gc_list_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().marker = "mymarker";
  ls.back().max = 2312;
  return ls;
}

void cls_rgw_gc_list_ret::dump(Formatter *f) const
{
  encode_json("entries", entries, f);
  f->dump_string("next_marker", next_marker);
  f->dump_int("truncated", (int)truncated);
}

list<cls_rgw_gc_list_ret> cls_rgw_gc_list_ret::generate_test_instances()
{
  list<cls_rgw_gc_list_ret> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().entries.push_back(cls_rgw_gc_obj_info());
  ls.back().truncated = true;
  return ls;
}

void cls_rgw_gc_remove_op::dump(Formatter *f) const
{
  encode_json("tags", tags, f);
}

list<cls_rgw_gc_remove_op> cls_rgw_gc_remove_op::generate_test_instances()
{
  list<cls_rgw_gc_remove_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().tags.push_back("tag1");
  ls.back().tags.push_back("tag2");
  return ls;
}

void cls_rgw_lc_get_entry_ret::dump(Formatter *f) const
{
  encode_json("entry", entry, f);
}

list<cls_rgw_lc_get_entry_ret> cls_rgw_lc_get_entry_ret::generate_test_instances()
{
  list<cls_rgw_lc_get_entry_ret> ls;
  cls_rgw_lc_entry entry("bucket1", 6000, 0);
  ls.emplace_back();
  ls.back().entry = entry;
  return ls;
}

list<rgw_cls_obj_prepare_op> rgw_cls_obj_prepare_op::generate_test_instances()
{
  list<rgw_cls_obj_prepare_op> o;
  rgw_cls_obj_prepare_op op;
  op.op = CLS_RGW_OP_ADD;
  op.key.name = "name";
  op.tag = "tag";
  op.locator = "locator";
  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_obj_prepare_op::dump(Formatter *f) const
{
  f->dump_int("op", op);
  f->dump_string("name", key.name);
  f->dump_string("tag", tag);
  f->dump_string("locator", locator);
}

list<rgw_cls_obj_complete_op> rgw_cls_obj_complete_op::generate_test_instances()
{
  list<rgw_cls_obj_complete_op> o;
  rgw_cls_obj_complete_op op;
  op.op = CLS_RGW_OP_DEL;
  op.key.name = "name";
  op.locator = "locator";
  op.ver.pool = 2;
  op.ver.epoch = 100;
  op.tag = "tag";

  list<rgw_bucket_dir_entry_meta> l = rgw_bucket_dir_entry_meta::generate_test_instances();
  auto iter = l.begin();
  op.meta = *iter;

  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_obj_complete_op::dump(Formatter *f) const
{
  f->dump_int("op", (int)op);
  f->dump_string("name", key.name);
  f->dump_string("instance", key.instance);
  f->dump_string("locator", locator);
  f->open_object_section("ver");
  ver.dump(f);
  f->close_section();
  f->open_object_section("meta");
  meta.dump(f);
  f->close_section();
  f->dump_string("tag", tag);
  f->dump_bool("log_op", log_op);
  f->dump_int("bilog_flags", bilog_flags);
  encode_json("zones_trace", zones_trace, f);
}

list<rgw_cls_link_olh_op> rgw_cls_link_olh_op::generate_test_instances()
{
  list<rgw_cls_link_olh_op> o;
  rgw_cls_link_olh_op op;
  op.key.name = "name";
  op.olh_tag = "olh_tag";
  op.delete_marker = true;
  op.op_tag = "op_tag";
  op.olh_epoch = 123;
  list<rgw_bucket_dir_entry_meta> l = rgw_bucket_dir_entry_meta::generate_test_instances();
  auto iter = l.begin();
  op.meta = *iter;
  op.log_op = true;

  o.push_back(std::move(op));

  o.emplace_back();
  return o;
}

void rgw_cls_link_olh_op::dump(Formatter *f) const
{
  encode_json("key", key, f);
  encode_json("olh_tag", olh_tag, f);
  encode_json("delete_marker", delete_marker, f);
  encode_json("op_tag", op_tag, f);
  encode_json("meta", meta, f);
  encode_json("olh_epoch", olh_epoch, f);
  encode_json("log_op", log_op, f);
  encode_json("bilog_flags", (uint32_t)bilog_flags, f);
  utime_t ut(unmod_since);
  encode_json("unmod_since", ut, f);
  encode_json("high_precision_time", high_precision_time, f);
  encode_json("zones_trace", zones_trace, f);
}

list<rgw_cls_unlink_instance_op> rgw_cls_unlink_instance_op::generate_test_instances()
{
  list<rgw_cls_unlink_instance_op> o;
  rgw_cls_unlink_instance_op op;
  op.key.name = "name";
  op.op_tag = "op_tag";
  op.olh_epoch = 124;
  op.log_op = true;

  o.push_back(std::move(op));

  o.emplace_back();
  return o;
}

void rgw_cls_unlink_instance_op::dump(Formatter *f) const
{
  encode_json("key", key, f);
  encode_json("op_tag", op_tag, f);
  encode_json("olh_epoch", olh_epoch, f);
  encode_json("log_op", log_op, f);
  encode_json("bilog_flags", (uint32_t)bilog_flags, f);
  encode_json("zones_trace", zones_trace, f);
}

list<rgw_cls_read_olh_log_op> rgw_cls_read_olh_log_op::generate_test_instances()
{
  list<rgw_cls_read_olh_log_op> o;
  rgw_cls_read_olh_log_op op;
  op.olh.name = "name";
  op.ver_marker = 123;
  op.olh_tag = "olh_tag";

  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_read_olh_log_op::dump(Formatter *f) const
{
  encode_json("olh", olh, f);
  encode_json("ver_marker", ver_marker, f);
  encode_json("olh_tag", olh_tag, f);
}

list<rgw_cls_read_olh_log_ret> rgw_cls_read_olh_log_ret::generate_test_instances()
{
  list<rgw_cls_read_olh_log_ret> o;
  rgw_cls_read_olh_log_ret r;
  r.is_truncated = true;
  list<rgw_bucket_olh_log_entry> l = rgw_bucket_olh_log_entry::generate_test_instances();
  auto iter = l.begin();
  r.log[1].push_back(*iter);

  o.push_back(std::move(r));
  o.emplace_back();
  return o;
}

void rgw_cls_read_olh_log_ret::dump(Formatter *f) const
{
  encode_json("log", log, f);
  encode_json("is_truncated", is_truncated, f);
}

list<rgw_cls_trim_olh_log_op> rgw_cls_trim_olh_log_op::generate_test_instances()
{
  list<rgw_cls_trim_olh_log_op> o;
  rgw_cls_trim_olh_log_op op;
  op.olh.name = "olh.name";
  op.ver = 100;
  op.olh_tag = "olh_tag";

  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_trim_olh_log_op::dump(Formatter *f) const
{
  encode_json("olh", olh, f);
  encode_json("ver", ver, f);
  encode_json("olh_tag", olh_tag, f);
}

list<rgw_cls_bucket_clear_olh_op> rgw_cls_bucket_clear_olh_op::generate_test_instances()
{
  list<rgw_cls_bucket_clear_olh_op> o;
  rgw_cls_bucket_clear_olh_op op;
  op.key.name = "key.name";
  op.olh_tag = "olh_tag";

  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_bucket_clear_olh_op::dump(Formatter *f) const
{
  encode_json("key", key, f);
  encode_json("olh_tag", olh_tag, f);
}

list<rgw_cls_list_op> rgw_cls_list_op::generate_test_instances()
{
  list<rgw_cls_list_op> o;
  rgw_cls_list_op op;
  op.start_obj.name = "start_obj";
  op.num_entries = 100;
  op.filter_prefix = "filter_prefix";
  o.push_back(std::move(op));
  o.emplace_back();
  return o;
}

void rgw_cls_list_op::dump(Formatter *f) const
{
  f->dump_string("start_obj", start_obj.name);
  f->dump_unsigned("num_entries", num_entries);
}

list<rgw_cls_list_ret> rgw_cls_list_ret::generate_test_instances()
{
  list<rgw_cls_list_ret> o;
  for (auto& d : rgw_bucket_dir::generate_test_instances()) {
    rgw_cls_list_ret ret;
    ret.dir = d;
    ret.is_truncated = true;

    o.push_back(std::move(ret));
  }

  o.emplace_back();
  return o;
}

void rgw_cls_list_ret::dump(Formatter *f) const
{
  f->open_object_section("dir");
  dir.dump(f);
  f->close_section();
  f->dump_int("is_truncated", (int)is_truncated);
}

list<rgw_cls_check_index_ret> rgw_cls_check_index_ret::generate_test_instances()
{
  list<rgw_cls_check_index_ret> o;

  list<rgw_bucket_dir_header> h = rgw_bucket_dir_header::generate_test_instances();
  rgw_cls_check_index_ret r;
  r.existing_header = h.front();
  r.calculated_header = h.front();
  o.push_back(std::move(r));

  o.emplace_back();
  return o;
}

void rgw_cls_check_index_ret::dump(Formatter *f) const
{
  encode_json("existing_header", existing_header, f);
  encode_json("calculated_header", calculated_header, f);
}

list<rgw_cls_bucket_update_stats_op> rgw_cls_bucket_update_stats_op::generate_test_instances()
{
  list<rgw_cls_bucket_update_stats_op> o;

  rgw_cls_bucket_update_stats_op r;
  r.absolute = true;
  rgw_bucket_category_stats& s = r.stats[RGWObjCategory::None];
  s.total_size = 1;
  s.total_size_rounded = 4096;
  s.num_entries = 1;
  rgw_bucket_category_stats& dec_s = r.dec_stats[RGWObjCategory::None];
  dec_s.total_size = 1;
  dec_s.total_size_rounded = 4096;
  dec_s.num_entries = 1;
  o.push_back(std::move(r));
  o.emplace_back();
  return o;
}

void rgw_cls_bucket_update_stats_op::dump(Formatter *f) const
{
  encode_json("absolute", absolute, f);
  map<int, rgw_bucket_category_stats> s;
  for (auto& entry : stats) {
    s[(int)entry.first] = entry.second;
  }
  encode_json("stats", s, f);
  map<int, rgw_bucket_category_stats> dec_s;
  for (auto& entry : dec_stats) {
    dec_s[(int)entry.first] = entry.second;
  }
  encode_json("dec_stats", dec_s, f);
}

void cls_rgw_bi_log_list_op::dump(Formatter *f) const
{
  f->dump_string("marker", marker);
  f->dump_unsigned("max", max);
}

list<cls_rgw_bi_log_list_op> cls_rgw_bi_log_list_op::generate_test_instances()
{
  list<cls_rgw_bi_log_list_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().marker = "mark";
  ls.back().max = 123;
  return ls;
}

void cls_rgw_bi_log_trim_op::dump(Formatter *f) const
{
  f->dump_string("start_marker", start_marker);
  f->dump_string("end_marker", end_marker);
}

list<cls_rgw_bi_log_trim_op> cls_rgw_bi_log_trim_op::generate_test_instances()
{
  list<cls_rgw_bi_log_trim_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().start_marker = "foo";
  ls.back().end_marker = "bar";
  return ls;
}

void cls_rgw_bi_log_list_ret::dump(Formatter *f) const
{
  encode_json("entries", entries, f);
  f->dump_unsigned("truncated", (int)truncated);
}

list<cls_rgw_bi_log_list_ret> cls_rgw_bi_log_list_ret::generate_test_instances()
{
  list<cls_rgw_bi_log_list_ret> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().entries.push_back(rgw_bi_log_entry());
  ls.back().truncated = true;
  return ls;
}

std::list<cls_rgw_mp_upload_part_info_update_op> cls_rgw_mp_upload_part_info_update_op::generate_test_instances()
{
  std::list<cls_rgw_mp_upload_part_info_update_op> ls;
  ls.emplace_back();
  ls.back().part_key = "part1";
  ls.emplace_back();
  ls.back().part_key = "part2";
  return ls;
}

void cls_rgw_mp_upload_part_info_update_op::dump(Formatter* f) const
{
  encode_json("part_key", part_key, f);
  encode_json("part_num", info.num, f);
  encode_json("part_prefix", info.manifest.get_prefix(), f);
}

list<cls_rgw_reshard_add_op> cls_rgw_reshard_add_op::generate_test_instances()
{
  list<cls_rgw_reshard_add_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  list<cls_rgw_reshard_entry> l = cls_rgw_reshard_entry::generate_test_instances();
  auto iter = l.begin();
  ls.back().entry = *iter;
  return ls;
}

void cls_rgw_reshard_add_op::dump(Formatter *f) const
{
  encode_json("entry", entry, f);
}

list<cls_rgw_reshard_list_op> cls_rgw_reshard_list_op::generate_test_instances()
{
  list<cls_rgw_reshard_list_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().max = 1000;
  ls.back().marker = "foo";
  return ls;
}

void cls_rgw_reshard_list_op::dump(Formatter *f) const
{
  encode_json("max", max, f);
  encode_json("marker", marker, f);
}

list<cls_rgw_reshard_list_ret> cls_rgw_reshard_list_ret::generate_test_instances()
{
  list<cls_rgw_reshard_list_ret> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().entries.push_back(cls_rgw_reshard_entry());
  ls.back().is_truncated = true;
  return ls;
}

void cls_rgw_reshard_list_ret::dump(Formatter *f) const
{
  encode_json("entries", entries, f);
  encode_json("is_truncated", is_truncated, f);
}

list<cls_rgw_reshard_get_op> cls_rgw_reshard_get_op::generate_test_instances()
{
  list<cls_rgw_reshard_get_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_reshard_get_op::dump(Formatter *f) const
{
  encode_json("entry", entry, f);
}

list<cls_rgw_reshard_get_ret> cls_rgw_reshard_get_ret::generate_test_instances()
{
  list<cls_rgw_reshard_get_ret> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_reshard_get_ret::dump(Formatter *f) const
{
  encode_json("entry", entry, f);
}

list<cls_rgw_reshard_remove_op> cls_rgw_reshard_remove_op::generate_test_instances()
{
  list<cls_rgw_reshard_remove_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  ls.back().bucket_name = "foo";
  ls.back().bucket_id = "bucket_id";
  return ls;
}

void cls_rgw_reshard_remove_op::dump(Formatter *f) const
{
  encode_json("bucket_name", bucket_name, f);
  encode_json("bucket_id", bucket_name, f);
}


list<cls_rgw_set_bucket_resharding_op> cls_rgw_set_bucket_resharding_op::generate_test_instances()
{
  list<cls_rgw_set_bucket_resharding_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_set_bucket_resharding_op::dump(Formatter *f) const
{
  encode_json("entry", entry, f);
}

list<cls_rgw_clear_bucket_resharding_op> cls_rgw_clear_bucket_resharding_op::generate_test_instances()
{
  list<cls_rgw_clear_bucket_resharding_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_clear_bucket_resharding_op::dump(Formatter *f) const
{
}

list<cls_rgw_guard_bucket_resharding_op> cls_rgw_guard_bucket_resharding_op::generate_test_instances()
{
  list<cls_rgw_guard_bucket_resharding_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_guard_bucket_resharding_op::dump(Formatter *f) const
{
  encode_json("ret_err", ret_err, f);
}


list<cls_rgw_get_bucket_resharding_op> cls_rgw_get_bucket_resharding_op::generate_test_instances()
{
  list<cls_rgw_get_bucket_resharding_op> ls;
  ls.emplace_back();
  ls.emplace_back();
  return ls;
}

void cls_rgw_get_bucket_resharding_op::dump(Formatter *f) const
{
}

void rgw_cls_bi_put_entries_op::dump(Formatter *f) const
{
  encode_json("entries", entries, f);
  encode_json("check_existing", check_existing, f);
}
