ovn-controller: Pass 'chassis_id' explicitly to functions that need it.
[cascardo/ovs.git] / ovn / controller / chassis.c
1 /* Copyright (c) 2015 Nicira, Inc.
2  *
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 #include <config.h>
17 #include "chassis.h"
18
19 #include "lib/vswitch-idl.h"
20 #include "openvswitch/vlog.h"
21 #include "ovn/lib/ovn-sb-idl.h"
22 #include "ovn-controller.h"
23
24 VLOG_DEFINE_THIS_MODULE(chassis);
25
26 void
27 chassis_init(struct controller_ctx *ctx)
28 {
29     ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_open_vswitch);
30     ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_open_vswitch_col_external_ids);
31 }
32
33 void
34 chassis_run(struct controller_ctx *ctx, const char *chassis_id)
35 {
36     if (!ctx->ovnsb_idl_txn) {
37         return;
38     }
39
40     const struct sbrec_chassis *chassis_rec;
41     const struct ovsrec_open_vswitch *cfg;
42     const char *encap_type, *encap_ip;
43     struct sbrec_encap *encap_rec;
44     static bool inited = false;
45
46     chassis_rec = get_chassis_by_name(ctx->ovnsb_idl, chassis_id);
47
48     /* xxx Need to support more than one encap.  Also need to support
49      * xxx encap options. */
50     cfg = ovsrec_open_vswitch_first(ctx->ovs_idl);
51     if (!cfg) {
52         VLOG_INFO("No Open_vSwitch row defined.");
53         return;
54     }
55
56     encap_type = smap_get(&cfg->external_ids, "ovn-encap-type");
57     encap_ip = smap_get(&cfg->external_ids, "ovn-encap-ip");
58     if (!encap_type || !encap_ip) {
59         VLOG_INFO("Need to specify an encap type and ip");
60         return;
61     }
62
63     if (chassis_rec) {
64         int i;
65
66         for (i = 0; i < chassis_rec->n_encaps; i++) {
67             if (!strcmp(chassis_rec->encaps[i]->type, encap_type)
68                 && !strcmp(chassis_rec->encaps[i]->ip, encap_ip)) {
69                 /* Nothing changed. */
70                 inited = true;
71                 return;
72             } else if (!inited) {
73                 VLOG_WARN("Chassis config changing on startup, make sure "
74                           "multiple chassis are not configured : %s/%s->%s/%s",
75                           chassis_rec->encaps[i]->type,
76                           chassis_rec->encaps[i]->ip,
77                           encap_type, encap_ip);
78             }
79
80         }
81     }
82
83     ovsdb_idl_txn_add_comment(ctx->ovnsb_idl_txn,
84                               "ovn-controller: registering chassis '%s'",
85                               chassis_id);
86
87     if (!chassis_rec) {
88         chassis_rec = sbrec_chassis_insert(ctx->ovnsb_idl_txn);
89         sbrec_chassis_set_name(chassis_rec, chassis_id);
90     }
91
92     encap_rec = sbrec_encap_insert(ctx->ovnsb_idl_txn);
93
94     sbrec_encap_set_type(encap_rec, encap_type);
95     sbrec_encap_set_ip(encap_rec, encap_ip);
96
97     sbrec_chassis_set_encaps(chassis_rec, &encap_rec, 1);
98
99     inited = true;
100 }
101
102 /* Returns true if the database is all cleaned up, false if more work is
103  * required. */
104 bool
105 chassis_cleanup(struct controller_ctx *ctx, const char *chassis_id)
106 {
107     /* Delete Chassis row. */
108     const struct sbrec_chassis *chassis_rec
109         = get_chassis_by_name(ctx->ovnsb_idl, chassis_id);
110     if (!chassis_rec) {
111         return true;
112     }
113     if (ctx->ovnsb_idl_txn) {
114         ovsdb_idl_txn_add_comment(ctx->ovnsb_idl_txn,
115                                   "ovn-controller: unregistering chassis '%s'",
116                                   chassis_id);
117         sbrec_chassis_delete(chassis_rec);
118     }
119     return false;
120 }