Global replace of Nicira Networks.
[sliver-openvswitch.git] / tests / test-odp.c
1 /*
2  * Copyright (c) 2011 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 "vlog.h"
26
27 int
28 main(void)
29 {
30     struct ds in;
31
32     ds_init(&in);
33     vlog_set_levels_from_string("odp_util:console:dbg");
34     while (!ds_get_line(&in, stdin)) {
35         enum odp_key_fitness fitness;
36         struct ofpbuf odp_key;
37         struct flow flow;
38         struct ds out;
39         int error;
40         char *s;
41
42         /* Delete comments, skip blank lines. */
43         s = ds_cstr(&in);
44         if (*s == '#') {
45             puts(s);
46             continue;
47         }
48         if (strchr(s, '#')) {
49             *strchr(s, '#') = '\0';
50         }
51         if (s[strspn(s, " ")] == '\0') {
52             putchar('\n');
53             continue;
54         }
55
56         /* Convert string to OVS DP key. */
57         ofpbuf_init(&odp_key, 0);
58         error = odp_flow_key_from_string(ds_cstr(&in), NULL, &odp_key);
59         if (error) {
60             printf("odp_flow_key_from_string: error\n");
61             goto next;
62         }
63
64         /* Convert odp_key to flow. */
65         fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
66         switch (fitness) {
67         case ODP_FIT_PERFECT:
68             break;
69
70         case ODP_FIT_TOO_LITTLE:
71             printf("ODP_FIT_TOO_LITTLE: ");
72             break;
73
74         case ODP_FIT_TOO_MUCH:
75             printf("ODP_FIT_TOO_MUCH: ");
76             break;
77
78         case ODP_FIT_ERROR:
79             printf("odp_flow_key_to_flow: error\n");
80             goto next;
81         }
82
83         /* Convert cls_rule back to odp_key. */
84         ofpbuf_uninit(&odp_key);
85         ofpbuf_init(&odp_key, 0);
86         odp_flow_key_from_flow(&odp_key, &flow);
87
88         /* Convert odp_key to string. */
89         ds_init(&out);
90         odp_flow_key_format(odp_key.data, odp_key.size, &out);
91         puts(ds_cstr(&out));
92         ds_destroy(&out);
93
94     next:
95         ofpbuf_uninit(&odp_key);
96     }
97     ds_destroy(&in);
98
99     return 0;
100 }