summaryrefslogtreecommitdiff
path: root/tools/policy_table_validator/main.cpp
blob: 99a4b7d7f9959df40e021767e60cc20125b12f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include "policy/policy_table/types.h"

#include "json/reader.h"
#include "utils/file_system.h"

namespace policy_table = rpc::policy_table_interface_base;

enum ResultCode {
  SUCCES = 0,
  MISSED_FILE_NAME,
  READ_ERROR,
  PARSE_ERROR,
  PT_TYPE_ERROR
};

rpc::policy_table_interface_base::PolicyTableType StringToPolicyTableType(
    const std::string& str_pt_type) {
  if (str_pt_type == "PT_PRELOADED") {
    return rpc::policy_table_interface_base::PT_PRELOADED;
  }
  if (str_pt_type == "PT_SNAPSHOT") {
    return rpc::policy_table_interface_base::PT_SNAPSHOT;
  }
  if (str_pt_type == "PT_UPDATE") {
    return rpc::policy_table_interface_base::PT_UPDATE;
  }
  return rpc::policy_table_interface_base::INVALID_PT_TYPE;
}

void help() {
  std::cout << "Usage:" << std::endl
            << "./policy_validator {Policy table type} {file_name}"
            << std::endl;
  std::cout << "Policy table types:"
               "\t PT_PRELOADED , PT_UPDATE , PT_SNAPSHOT" << std::endl;
}

int main(int argc, char** argv) {
  if (argc != 3) {
    // TODO(AKutsan): No filename
    help();
    exit(MISSED_FILE_NAME);
  }
  std::string pt_type_str = argv[1];
  std::string file_name = argv[2];
  std::string json_string;
  rpc::policy_table_interface_base::PolicyTableType pt_type;
  pt_type = StringToPolicyTableType(pt_type_str);
  if (rpc::policy_table_interface_base::PolicyTableType::INVALID_PT_TYPE ==
      pt_type) {
    std::cout << "Invalid policy table type: " << pt_type_str << std::endl;
    exit(PT_TYPE_ERROR);
  }
  bool read_result = file_system::ReadFile(file_name, json_string);
  if (false == read_result) {
    std::cout << "Read file error: " << file_name << std::endl;
    exit(READ_ERROR);
  }

  Json::Reader reader;
  Json::Value value;

  bool parce_result = reader.parse(json_string, value);
  if (false == parce_result) {
    std::cout << "Json parce fails" << std::endl;
    exit(PARSE_ERROR);
  }
  std::cout << "DEFAULT_POLICY" << std::endl;
  policy_table::Table table(&value);
  table.SetPolicyTableType(pt_type);
  bool is_valid = table.is_valid();
  if (true == is_valid) {
    std::cout << "Table is valid" << std::endl;
    exit(SUCCES);
  }

  std::cout << "Table is not valid" << std::endl;
  rpc::ValidationReport report("policy_table");
  table.ReportErrors(&report);
  std::cout << "Errors: " << std::endl
            << rpc::PrettyFormat(report) << std::endl;

  return SUCCES;
}