ioam: remove api boilerplate
[vpp.git] / src / plugins / ioam / export / ioam_export_test.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *------------------------------------------------------------------
17  * ioam_export_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25
26 #define __plugin_msg_base export_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
28
29
30 /* Declare message IDs */
31 #include <ioam/export/ioam_export.api_enum.h>
32 #include <ioam/export/ioam_export.api_types.h>
33
34 #define vl_endianfun            /* define message structures */
35 #define vl_printfun
36 #define vl_api_version(n,v) static u32 api_version=(v);
37 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
38 #include <ioam/export/ioam_export.api.h>
39 #undef vl_endianfun
40 #undef vl_printfun
41 #undef vl_api_version
42
43 typedef struct
44 {
45   /* API message ID base */
46   u16 msg_id_base;
47   vat_main_t *vat_main;
48 } export_test_main_t;
49
50 static export_test_main_t export_test_main;
51
52 #define foreach_standard_reply_retval_handler   \
53 _(ioam_export_ip6_enable_disable_reply)
54
55 #define _(n)                                            \
56     static void vl_api_##n##_t_handler                  \
57     (vl_api_##n##_t * mp)                               \
58     {                                                   \
59         vat_main_t * vam = export_test_main.vat_main;   \
60         i32 retval = ntohl(mp->retval);                 \
61         if (vam->async_mode) {                          \
62             vam->async_errors += (retval < 0);          \
63         } else {                                        \
64             vam->retval = retval;                       \
65             vam->result_ready = 1;                      \
66         }                                               \
67     }
68 foreach_standard_reply_retval_handler;
69 #undef _
70
71 /*
72  * Table of message reply handlers, must include boilerplate handlers
73  * we just generated
74  */
75 #define foreach_vpe_api_reply_msg                                       \
76 _(IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY, ioam_export_ip6_enable_disable_reply)
77
78
79 static int
80 api_ioam_export_ip6_enable_disable (vat_main_t * vam)
81 {
82   unformat_input_t *i = vam->input;
83   int is_disable = 0;
84   vl_api_ioam_export_ip6_enable_disable_t *mp;
85   int ret;
86
87   /* Parse args required to build the message */
88   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
89     {
90       if (unformat (i, "disable"))
91         is_disable = 1;
92       else
93         break;
94     }
95
96   /* Construct the API message */
97   M(IOAM_EXPORT_IP6_ENABLE_DISABLE, mp);
98   mp->is_disable = is_disable;
99
100   /* send it... */
101   S(mp);
102
103   /* Wait for a reply... */
104   W (ret);
105   return ret;
106 }
107
108 /*
109  * List of messages that the api test plugin sends,
110  * and that the data plane plugin processes
111  */
112 #define foreach_vpe_api_msg \
113 _(ioam_export_ip6_enable_disable, "<intfc> [disable]")
114
115 static void
116 ioam_export_vat_api_hookup (vat_main_t * vam)
117 {
118   export_test_main_t *sm = &export_test_main;
119   /* Hook up handlers for replies from the data plane plug-in */
120 #define _(N,n)                                                  \
121     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
122                            #n,                                  \
123                            vl_api_##n##_t_handler,              \
124                            vl_noop_handler,                     \
125                            vl_api_##n##_t_endian,               \
126                            vl_api_##n##_t_print,                \
127                            sizeof(vl_api_##n##_t), 1);
128   foreach_vpe_api_reply_msg;
129 #undef _
130
131   /* API messages we can send */
132 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
133   foreach_vpe_api_msg;
134 #undef _
135
136   /* Help strings */
137 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
138   foreach_vpe_api_msg;
139 #undef _
140 }
141
142 clib_error_t *
143 ioam_export_vat_plugin_register (vat_main_t * vam)
144 {
145   export_test_main_t *sm = &export_test_main;
146   u8 *name;
147
148   sm->vat_main = vam;
149
150   name = format (0, "ioam_export_%08x%c", api_version, 0);
151   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
152
153   if (sm->msg_id_base != (u16) ~ 0)
154     ioam_export_vat_api_hookup (vam);
155
156   vec_free (name);
157
158   return 0;
159 }