python: Implement "vlog/reopen" unixctl command in Python vlog.
[openvswitch] / tests / test-odp.c
1 /*
2  * Copyright (c) 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <stdio.h>
20
21 #include "dynamic-string.h"
22 #include "flow.h"
23 #include "odp-util.h"
24 #include "ofpbuf.h"
25 #include "util.h"
26 #include "vlog.h"
27
28 static int
29 parse_keys(void)
30 {
31     struct ds in;
32
33     ds_init(&in);
34     vlog_set_levels_from_string("odp_util:console:dbg");
35     while (!ds_get_test_line(&in, stdin)) {
36         enum odp_key_fitness fitness;
37         struct ofpbuf odp_key;
38         struct flow flow;
39         struct ds out;
40         int error;
41
42         /* Convert string to OVS DP key. */
43         ofpbuf_init(&odp_key, 0);
44         error = odp_flow_key_from_string(ds_cstr(&in), NULL, &odp_key);
45         if (error) {
46             printf("odp_flow_key_from_string: error\n");
47             goto next;
48         }
49
50         /* Convert odp_key to flow. */
51         fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
52         switch (fitness) {
53         case ODP_FIT_PERFECT:
54             break;
55
56         case ODP_FIT_TOO_LITTLE:
57             printf("ODP_FIT_TOO_LITTLE: ");
58             break;
59
60         case ODP_FIT_TOO_MUCH:
61             printf("ODP_FIT_TOO_MUCH: ");
62             break;
63
64         case ODP_FIT_ERROR:
65             printf("odp_flow_key_to_flow: error\n");
66             goto next;
67         }
68
69         /* Convert cls_rule back to odp_key. */
70         ofpbuf_uninit(&odp_key);
71         ofpbuf_init(&odp_key, 0);
72         odp_flow_key_from_flow(&odp_key, &flow);
73
74         /* Convert odp_key to string. */
75         ds_init(&out);
76         odp_flow_key_format(odp_key.data, odp_key.size, &out);
77         puts(ds_cstr(&out));
78         ds_destroy(&out);
79
80     next:
81         ofpbuf_uninit(&odp_key);
82     }
83     ds_destroy(&in);
84
85     return 0;
86 }
87
88 static int
89 parse_actions(void)
90 {
91     struct ds in;
92
93     ds_init(&in);
94     vlog_set_levels_from_string("odp_util:console:dbg");
95     while (!ds_get_test_line(&in, stdin)) {
96         struct ofpbuf odp_actions;
97         struct ds out;
98         int error;
99
100         /* Convert string to OVS DP actions. */
101         ofpbuf_init(&odp_actions, 0);
102         error = odp_actions_from_string(ds_cstr(&in), NULL, &odp_actions);
103         if (error) {
104             printf("odp_actions_from_string: error\n");
105             goto next;
106         }
107
108         /* Convert odp_actions back to string. */
109         ds_init(&out);
110         format_odp_actions(&out, odp_actions.data, odp_actions.size);
111         puts(ds_cstr(&out));
112         ds_destroy(&out);
113
114     next:
115         ofpbuf_uninit(&odp_actions);
116     }
117     ds_destroy(&in);
118
119     return 0;
120 }
121
122 int
123 main(int argc, char *argv[])
124 {
125     if (argc == 2 &&!strcmp(argv[1], "parse-keys")) {
126         return parse_keys();
127     } else if (argc == 2 && !strcmp(argv[1], "parse-actions")) {
128         return parse_actions();
129     } else {
130         ovs_fatal(0, "usage: %s parse-keys | parse-actions", argv[0]);
131     }
132 }