|
1 | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
2 | 2 |
|
| 3 | +#include <srx/srx_val.h> |
3 | 4 | #include <srx/common.h> |
| 5 | +#include <srx/lyx.h> |
4 | 6 | #include "core.h" |
5 | 7 |
|
6 | 8 | struct confd confd; |
@@ -75,21 +77,105 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul |
75 | 77 | return SR_ERR_SYS; |
76 | 78 | } |
77 | 79 |
|
78 | | - /* skip reload in bootstrap, implicit reload in runlevel change */ |
79 | | - if (systemf("runlevel >/dev/null 2>&1")) { |
80 | | - /* trigger any tasks waiting for confd to have applied *-config */ |
81 | | - system("initctl -nbq cond set bootstrap"); |
| 80 | + |
| 81 | + return SR_ERR_OK; |
| 82 | +} |
| 83 | + |
| 84 | +static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, |
| 85 | + const char *xpath, sr_event_t event, uint32_t request_id, void *_confd) |
| 86 | +{ |
| 87 | + struct lyd_node *diff = NULL, *config = NULL; |
| 88 | + static uint32_t last_event = -1; |
| 89 | + struct confd *confd = _confd; |
| 90 | + static uint32_t last_id = 0; |
| 91 | + confd_dependency_t result; |
| 92 | + sr_data_t *cfg = NULL; |
| 93 | + int rc = SR_ERR_OK; |
| 94 | + |
| 95 | + if (request_id == last_id && last_event == event) |
82 | 96 | return SR_ERR_OK; |
| 97 | + last_id = request_id; |
| 98 | + last_event = event; |
| 99 | + |
| 100 | + if (event == SR_EV_CHANGE || event == SR_EV_DONE) { |
| 101 | + rc = srx_get_diff(session, &diff); |
| 102 | + if (rc != SR_ERR_OK) { |
| 103 | + ERROR("Failed to get diff: %d", rc); |
| 104 | + return rc; |
| 105 | + } |
| 106 | + rc = sr_get_data(session, "//.", 0, 0, 0, &cfg); |
| 107 | + if (rc || !cfg) |
| 108 | + goto free_diff; |
| 109 | + |
| 110 | + config = cfg->tree; |
| 111 | +#if 0 |
| 112 | + /* Debug: print diff to file */ |
| 113 | + FILE *f = fopen("/tmp/confd-diff.json", "w"); |
| 114 | + if (f) { |
| 115 | + lyd_print_file(f, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 116 | + fclose(f); |
| 117 | + } |
| 118 | +#endif |
83 | 119 | } |
84 | 120 |
|
85 | | - if (systemf("initctl -b reload")) { |
86 | | - EMERG("initctl reload: failed applying new configuration!"); |
87 | | - return SR_ERR_SYS; |
88 | | - } |
| 121 | + /* ietf-interfaces */ |
| 122 | + ietf_interfaces_change(session, config, diff, event, confd); |
89 | 123 |
|
90 | | - AUDIT("The new configuration has been applied."); |
| 124 | + /* infix-dhcp-client*/ |
| 125 | + infix_dhcp_client_change(session, config, diff, event, confd); |
91 | 126 |
|
92 | | - return SR_ERR_OK; |
| 127 | + /* ietf-keystore */ |
| 128 | + ietf_keystore_change(session, config, diff, event, confd); |
| 129 | + |
| 130 | + /* infix-services */ |
| 131 | + infix_services_change(session, config, diff, event, confd); |
| 132 | + |
| 133 | + /* ietf-syslog*/ |
| 134 | + ietf_syslog_change(session, config, diff, event, confd); |
| 135 | + |
| 136 | + /* ietf-system */ |
| 137 | + ietf_system_change(session, config, diff, event, confd); |
| 138 | + |
| 139 | + /* infix-containers */ |
| 140 | + infix_containers_change(session, config, diff, event, confd); |
| 141 | + |
| 142 | + /* ietf-hardware */ |
| 143 | + ietf_hardware_change(session, config, diff, event, confd); |
| 144 | + |
| 145 | + /* ietf-routing */ |
| 146 | + ietf_routing_change(session, config, diff, event, confd); |
| 147 | + |
| 148 | + /* infix-dhcp-server */ |
| 149 | + infix_dhcp_server_change(session, config, diff, event, confd); |
| 150 | + |
| 151 | + /* infix-firewall */ |
| 152 | + infix_firewall_change(session, config, diff, event, confd); |
| 153 | + |
| 154 | + if (cfg) |
| 155 | + sr_release_data(cfg); |
| 156 | + |
| 157 | + if (event == SR_EV_DONE) { |
| 158 | + /* skip reload in bootstrap, implicit reload in runlevel change */ |
| 159 | + if (systemf("runlevel >/dev/null 2>&1")) { |
| 160 | + /* trigger any tasks waiting for confd to have applied *-config */ |
| 161 | + system("initctl -nbq cond set bootstrap"); |
| 162 | + return SR_ERR_OK; |
| 163 | + } |
| 164 | + |
| 165 | + if (systemf("initctl -b reload")) { |
| 166 | + EMERG("initctl reload: failed applying new configuration!"); |
| 167 | + return SR_ERR_SYS; |
| 168 | + } |
| 169 | + |
| 170 | + AUDIT("The new configuration has been applied."); |
| 171 | + } |
| 172 | +free_diff: |
| 173 | + lyd_free_tree(diff); |
| 174 | + return rc; |
| 175 | +} |
| 176 | + |
| 177 | +static int subscribe_module(char *model, struct confd *confd, int flags) { |
| 178 | + return sr_module_change_subscribe(confd->session, model, "//.", change_cb, confd, CB_PRIO_PRIMARY, SR_SUBSCR_CHANGE_ALL_MODULES | SR_SUBSCR_DEFAULT | flags, &confd->sub); |
93 | 179 | } |
94 | 180 |
|
95 | 181 | int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
@@ -136,56 +222,119 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
136 | 222 | */ |
137 | 223 | confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL); |
138 | 224 |
|
139 | | - rc = ietf_interfaces_init(&confd); |
140 | | - if (rc) |
| 225 | + rc = subscribe_module("ietf-interfaces", &confd, 0); |
| 226 | + if (rc) { |
| 227 | + ERROR("Failed to subscribe to ietf-interfaces"); |
141 | 228 | goto err; |
142 | | - rc = ietf_keystore_init(&confd); |
143 | | - if (rc) |
| 229 | + } |
| 230 | + rc = subscribe_module("ietf-netconf-acm", &confd, 0); |
| 231 | + if (rc) { |
| 232 | + ERROR("Failed to subscribe to ietf-netconf-acm"); |
144 | 233 | goto err; |
145 | | - rc = ietf_syslog_init(&confd); |
146 | | - if (rc) |
| 234 | + } |
| 235 | + rc = subscribe_module("infix-dhcp-client", &confd, 0); |
| 236 | + if (rc) { |
| 237 | + ERROR("Failed to subscribe to infix-dhcp-client"); |
147 | 238 | goto err; |
148 | | - rc = ietf_system_init(&confd); |
149 | | - if (rc) |
| 239 | + } |
| 240 | + rc = subscribe_module("ietf-keystore", &confd, SR_SUBSCR_UPDATE); |
| 241 | + if (rc) { |
| 242 | + ERROR("Failed to subscribe to ietf-keystore"); |
150 | 243 | goto err; |
151 | | - rc = infix_containers_init(&confd); |
152 | | - if (rc) |
| 244 | + } |
| 245 | + rc = subscribe_module("infix-services", &confd, 0); |
| 246 | + if (rc) { |
| 247 | + ERROR("Failed to subscribe to infix-services"); |
| 248 | + goto err; |
| 249 | + } |
| 250 | + rc = subscribe_module("ietf-system", &confd, 0); |
| 251 | + if (rc) { |
| 252 | + ERROR("Failed to subscribe to ietf-system"); |
| 253 | + goto err; |
| 254 | + } |
| 255 | + rc = subscribe_module("ieee802-dot1ab-lldp", &confd, 0); |
| 256 | + if (rc) { |
| 257 | + ERROR("Failed to subscribe to ieee802-dot1ab-lldp"); |
| 258 | + goto err; |
| 259 | + } |
| 260 | +#ifdef CONTAINERS |
| 261 | + rc = subscribe_module("infix-containers", &confd, 0); |
| 262 | + if (rc) { |
| 263 | + ERROR("Failed to subscribe to infix-containers"); |
| 264 | + goto err; |
| 265 | + } |
| 266 | +#endif |
| 267 | + rc = subscribe_module("infix-dhcp-server", &confd, 0); |
| 268 | + if (rc) { |
| 269 | + ERROR("Failed to subscribe to infix-dhcp-server"); |
| 270 | + goto err; |
| 271 | + } |
| 272 | + rc = subscribe_module("ietf-routing", &confd, 0); |
| 273 | + if (rc) { |
| 274 | + ERROR("Failed to subscribe to ietf-routing"); |
| 275 | + goto err; |
| 276 | + } |
| 277 | + rc = subscribe_module("ietf-hardware", &confd, 0); |
| 278 | + if (rc) { |
| 279 | + ERROR("Failed to subscribe to ietf-hardware"); |
| 280 | + goto err; |
| 281 | + } |
| 282 | + rc = subscribe_module("infix-firewall", &confd, 0); |
| 283 | + if (rc) { |
| 284 | + ERROR("Failed to subscribe to infix-firewall"); |
| 285 | + goto err; |
| 286 | + } |
| 287 | + rc = subscribe_module("infix-meta", &confd, SR_SUBSCR_UPDATE); |
| 288 | + if (rc) { |
| 289 | + ERROR("Failed to subscribe to infix-meta"); |
153 | 290 | goto err; |
154 | | - rc = infix_dhcp_client_init(&confd); |
| 291 | + } |
| 292 | + |
| 293 | + rc = ietf_system_rpc_init(&confd); |
155 | 294 | if (rc) |
156 | 295 | goto err; |
157 | | - rc = infix_dhcp_server_init(&confd); |
| 296 | + rc = infix_containers_rpc_init(&confd); |
158 | 297 | if (rc) |
159 | 298 | goto err; |
160 | | - rc = infix_factory_init(&confd); |
| 299 | + rc = infix_dhcp_server_rpc_init(&confd); |
161 | 300 | if (rc) |
162 | 301 | goto err; |
163 | | - rc = ietf_factory_default_init(&confd); |
| 302 | + |
| 303 | + rc = infix_factory_rpc_init(&confd); |
164 | 304 | if (rc) |
165 | 305 | goto err; |
166 | | - rc = ietf_routing_init(&confd); |
| 306 | + |
| 307 | + rc = ietf_factory_default_rpc_init(&confd); |
167 | 308 | if (rc) |
168 | 309 | goto err; |
169 | | - rc = infix_meta_init(&confd); |
| 310 | + |
| 311 | + rc = infix_firewall_rpc_init(&confd); |
170 | 312 | if (rc) |
171 | 313 | goto err; |
172 | | - rc = infix_system_sw_init(&confd); |
| 314 | + |
| 315 | + rc = infix_system_sw_rpc_init(&confd); |
173 | 316 | if (rc) |
174 | 317 | goto err; |
175 | | - rc = infix_services_init(&confd); |
| 318 | + |
| 319 | + /* Candidate infer configurations */ |
| 320 | + rc = ietf_hardware_candidate_init(&confd); |
176 | 321 | if (rc) |
177 | 322 | goto err; |
178 | | - rc = ietf_hardware_init(&confd); |
| 323 | + |
| 324 | + rc = infix_firewall_candidate_init(&confd); |
179 | 325 | if (rc) |
180 | 326 | goto err; |
181 | | - rc = infix_firewall_init(&confd); |
| 327 | + |
| 328 | + rc = infix_dhcp_server_candidate_init(&confd); |
182 | 329 | if (rc) |
183 | 330 | goto err; |
184 | 331 |
|
| 332 | + rc = infix_dhcp_client_candidate_init(&confd); |
| 333 | + if (rc) |
| 334 | + goto err; |
185 | 335 | /* YOUR_INIT GOES HERE */ |
186 | 336 |
|
187 | 337 | return SR_ERR_OK; |
188 | | - |
189 | 338 | err: |
190 | 339 | ERROR("init failed: %s", sr_strerror(rc)); |
191 | 340 | if (confd.root) |
|
0 commit comments