odp-util: Add support for named ports to odp_flow_key_from_string().
[cascardo/ovs.git] / tests / test-odp.c
1 /*
2  * Copyright (c) 2011 Nicira Networks.
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
26 int
27 main(void)
28 {
29     struct ds in;
30
31     ds_init(&in);
32     while (!ds_get_line(&in, stdin)) {
33         struct ofpbuf odp_key;
34         struct flow flow;
35         struct ds out;
36         int error;
37         char *s;
38
39         /* Delete comments, skip blank lines. */
40         s = ds_cstr(&in);
41         if (*s == '#') {
42             puts(s);
43             continue;
44         }
45         if (strchr(s, '#')) {
46             *strchr(s, '#') = '\0';
47         }
48         if (s[strspn(s, " ")] == '\0') {
49             putchar('\n');
50             continue;
51         }
52
53         /* Convert string to OVS DP key. */
54         ofpbuf_init(&odp_key, 0);
55         error = odp_flow_key_from_string(ds_cstr(&in), NULL, &odp_key);
56         if (error) {
57             printf("odp_flow_key_from_string: error\n");
58             goto next;
59         }
60
61         /* Convert odp_key to flow. */
62         error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow);
63         if (error) {
64             printf("odp_flow_key_to_flow: error\n");
65             goto next;
66         }
67
68         /* Convert cls_rule back to odp_key. */
69         ofpbuf_uninit(&odp_key);
70         ofpbuf_init(&odp_key, 0);
71         odp_flow_key_from_flow(&odp_key, &flow);
72
73         /* Convert odp_key to string. */
74         ds_init(&out);
75         odp_flow_key_format(odp_key.data, odp_key.size, &out);
76         puts(ds_cstr(&out));
77         ds_destroy(&out);
78
79     next:
80         ofpbuf_uninit(&odp_key);
81     }
82     ds_destroy(&in);
83
84     return 0;
85 }