Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / mon / mon_common.c
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35
36 #include <talloc.h>
37 #include <glib.h>
38 #include <string.h>
39
40 #include <mon_internal.h>
41
42 // Monitoring common code
43
44 struct mon_cmd_entry {
45   MON_CMD code;
46   const char *name;
47 };
48
49 struct mon_opt_entry {
50   MON_OPT_TYPE code;
51   MON_CMD cmd_code;
52   const char *name;
53 };
54
55 /* Table of commands */
56 struct mon_cmd_entry mon_cmd_table[] = {
57     { MON_CMD_SHOW, "show" },
58     { MON_CMD_UNKNOWN } /* list terminator */
59 };
60
61 /* Table of options */
62 struct  mon_opt_entry mon_opt_table[] = {
63     { OPT_TYPE_SHOW_VERSION,            MON_CMD_SHOW,  "version"            },
64     { OPT_TYPE_SHOW_CONFIG_FILES,       MON_CMD_SHOW,  "config_files"       },
65     { OPT_TYPE_SHOW_UPTIME,             MON_CMD_SHOW,  "uptime"             },
66     { OPT_TYPE_SHOW_TID_REQS_PROCESSED, MON_CMD_SHOW,  "tid_reqs_processed" },
67     { OPT_TYPE_SHOW_TID_REQS_FAILED,    MON_CMD_SHOW,  "tid_reqs_failed"    },
68     { OPT_TYPE_SHOW_TID_REQS_PENDING,   MON_CMD_SHOW,  "tid_reqs_pending"   },
69     { OPT_TYPE_SHOW_TID_ERROR_COUNT,    MON_CMD_SHOW,  "tid_error_count"    },
70     { OPT_TYPE_SHOW_ROUTES,             MON_CMD_SHOW,  "routes"             },
71     { OPT_TYPE_SHOW_PEERS,              MON_CMD_SHOW,  "peers"              },
72     { OPT_TYPE_SHOW_COMMUNITIES,        MON_CMD_SHOW,  "communities"        },
73     { OPT_TYPE_SHOW_REALMS,             MON_CMD_SHOW,  "realms"             },
74     { OPT_TYPE_SHOW_RP_CLIENTS,         MON_CMD_SHOW,  "rp_clients"         },
75     { OPT_TYPE_UNKNOWN } /* list terminator */
76 };
77
78 /*** Commands ***/
79
80 static struct mon_cmd_entry *find_cmd_entry(MON_CMD code)
81 {
82   struct mon_cmd_entry *entry;
83
84   for (entry=mon_cmd_table; entry->code != MON_CMD_UNKNOWN; entry++) {
85     if (entry->code == code)
86       return entry;
87   }
88
89   return NULL;
90 }
91
92 const char *mon_cmd_to_string(MON_CMD cmd)
93 {
94   struct mon_cmd_entry *entry = find_cmd_entry(cmd);
95
96   if (entry)
97     return entry->name;
98
99   return NULL;
100 }
101
102 MON_CMD mon_cmd_from_string(const char *s)
103 {
104   struct mon_cmd_entry *entry;
105
106   for (entry=mon_cmd_table; entry->code != MON_CMD_UNKNOWN; entry++) {
107     if (strcmp(s, entry->name) == 0)
108       return entry->code;
109   }
110
111   return MON_CMD_UNKNOWN;
112 }
113
114 /*** Options ***/
115
116 static struct mon_opt_entry *find_opt_entry(MON_OPT_TYPE code)
117 {
118   struct mon_opt_entry *entry;
119
120   for (entry=mon_opt_table; entry->code != OPT_TYPE_UNKNOWN; entry++) {
121     if (entry->code == code)
122       return entry;
123   }
124
125   return NULL;
126 }
127
128 const char *mon_opt_type_to_string(MON_OPT_TYPE opt_type)
129 {
130   struct mon_opt_entry *entry = find_opt_entry(opt_type);
131
132   if (entry)
133     return entry->name;
134
135   return NULL;
136 }
137
138 MON_OPT_TYPE mon_opt_type_from_string(const char *s)
139 {
140   struct mon_opt_entry *entry;
141
142   for (entry=mon_opt_table; entry->code != OPT_TYPE_UNKNOWN; entry++) {
143     if (strcmp(s, entry->name) == 0)
144       return entry->code;
145   }
146
147   return OPT_TYPE_UNKNOWN;
148 }