|
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,116 @@ 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 | + if ((rc = ietf_interfaces_change(session, config, diff, event, confd))) |
| 123 | + goto free_diff; |
89 | 124 |
|
90 | | - AUDIT("The new configuration has been applied."); |
| 125 | + /* infix-dhcp-client*/ |
| 126 | + if ((rc = infix_dhcp_client_change(session, config, diff, event, confd))) |
| 127 | + goto free_diff; |
91 | 128 |
|
92 | | - return SR_ERR_OK; |
| 129 | + /* ietf-keystore */ |
| 130 | + if ((rc = ietf_keystore_change(session, config, diff, event, confd))) |
| 131 | + goto free_diff; |
| 132 | + |
| 133 | + /* infix-services */ |
| 134 | + if ((rc = infix_services_change(session, config, diff, event, confd))) |
| 135 | + goto free_diff; |
| 136 | + |
| 137 | + /* ietf-syslog*/ |
| 138 | + if ((rc = ietf_syslog_change(session, config, diff, event, confd))) |
| 139 | + goto free_diff; |
| 140 | + |
| 141 | + /* ietf-system */ |
| 142 | + if ((rc = ietf_system_change(session, config, diff, event, confd))) |
| 143 | + goto free_diff; |
| 144 | + |
| 145 | + /* infix-containers */ |
| 146 | + if ((rc = infix_containers_change(session, config, diff, event, confd))) |
| 147 | + goto free_diff; |
| 148 | + |
| 149 | + /* ietf-hardware */ |
| 150 | + if ((rc = ietf_hardware_change(session, config, diff, event, confd))) |
| 151 | + goto free_diff; |
| 152 | + |
| 153 | + /* ietf-routing */ |
| 154 | + if ((rc = ietf_routing_change(session, config, diff, event, confd))) |
| 155 | + goto free_diff; |
| 156 | + |
| 157 | + /* infix-dhcp-server */ |
| 158 | + if ((rc = infix_dhcp_server_change(session, config, diff, event, confd))) |
| 159 | + goto free_diff; |
| 160 | + |
| 161 | + /* infix-firewall */ |
| 162 | + if ((rc = infix_firewall_change(session, config, diff, event, confd))) |
| 163 | + goto free_diff; |
| 164 | + |
| 165 | + if (cfg) |
| 166 | + sr_release_data(cfg); |
| 167 | + |
| 168 | + if (event == SR_EV_DONE) { |
| 169 | + /* skip reload in bootstrap, implicit reload in runlevel change */ |
| 170 | + if (systemf("runlevel >/dev/null 2>&1")) { |
| 171 | + /* trigger any tasks waiting for confd to have applied *-config */ |
| 172 | + system("initctl -nbq cond set bootstrap"); |
| 173 | + return SR_ERR_OK; |
| 174 | + } |
| 175 | + |
| 176 | + if (systemf("initctl -b reload")) { |
| 177 | + EMERG("initctl reload: failed applying new configuration!"); |
| 178 | + return SR_ERR_SYS; |
| 179 | + } |
| 180 | + |
| 181 | + AUDIT("The new configuration has been applied."); |
| 182 | + } |
| 183 | +free_diff: |
| 184 | + lyd_free_tree(diff); |
| 185 | + return rc; |
| 186 | +} |
| 187 | + |
| 188 | +static int subscribe_module(char *model, struct confd *confd, int flags) { |
| 189 | + 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 | 190 | } |
94 | 191 |
|
95 | 192 | int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
@@ -136,56 +233,119 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
136 | 233 | */ |
137 | 234 | confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL); |
138 | 235 |
|
139 | | - rc = ietf_interfaces_init(&confd); |
140 | | - if (rc) |
| 236 | + rc = subscribe_module("ietf-interfaces", &confd, 0); |
| 237 | + if (rc) { |
| 238 | + ERROR("Failed to subscribe to ietf-interfaces"); |
141 | 239 | goto err; |
142 | | - rc = ietf_keystore_init(&confd); |
143 | | - if (rc) |
| 240 | + } |
| 241 | + rc = subscribe_module("ietf-netconf-acm", &confd, 0); |
| 242 | + if (rc) { |
| 243 | + ERROR("Failed to subscribe to ietf-netconf-acm"); |
144 | 244 | goto err; |
145 | | - rc = ietf_syslog_init(&confd); |
146 | | - if (rc) |
| 245 | + } |
| 246 | + rc = subscribe_module("infix-dhcp-client", &confd, 0); |
| 247 | + if (rc) { |
| 248 | + ERROR("Failed to subscribe to infix-dhcp-client"); |
147 | 249 | goto err; |
148 | | - rc = ietf_system_init(&confd); |
149 | | - if (rc) |
| 250 | + } |
| 251 | + rc = subscribe_module("ietf-keystore", &confd, SR_SUBSCR_UPDATE); |
| 252 | + if (rc) { |
| 253 | + ERROR("Failed to subscribe to ietf-keystore"); |
150 | 254 | goto err; |
151 | | - rc = infix_containers_init(&confd); |
152 | | - if (rc) |
| 255 | + } |
| 256 | + rc = subscribe_module("infix-services", &confd, 0); |
| 257 | + if (rc) { |
| 258 | + ERROR("Failed to subscribe to infix-services"); |
| 259 | + goto err; |
| 260 | + } |
| 261 | + rc = subscribe_module("ietf-system", &confd, 0); |
| 262 | + if (rc) { |
| 263 | + ERROR("Failed to subscribe to ietf-system"); |
| 264 | + goto err; |
| 265 | + } |
| 266 | + rc = subscribe_module("ieee802-dot1ab-lldp", &confd, 0); |
| 267 | + if (rc) { |
| 268 | + ERROR("Failed to subscribe to ieee802-dot1ab-lldp"); |
| 269 | + goto err; |
| 270 | + } |
| 271 | +#ifdef CONTAINERS |
| 272 | + rc = subscribe_module("infix-containers", &confd, 0); |
| 273 | + if (rc) { |
| 274 | + ERROR("Failed to subscribe to infix-containers"); |
| 275 | + goto err; |
| 276 | + } |
| 277 | +#endif |
| 278 | + rc = subscribe_module("infix-dhcp-server", &confd, 0); |
| 279 | + if (rc) { |
| 280 | + ERROR("Failed to subscribe to infix-dhcp-server"); |
| 281 | + goto err; |
| 282 | + } |
| 283 | + rc = subscribe_module("ietf-routing", &confd, 0); |
| 284 | + if (rc) { |
| 285 | + ERROR("Failed to subscribe to ietf-routing"); |
| 286 | + goto err; |
| 287 | + } |
| 288 | + rc = subscribe_module("ietf-hardware", &confd, 0); |
| 289 | + if (rc) { |
| 290 | + ERROR("Failed to subscribe to ietf-hardware"); |
| 291 | + goto err; |
| 292 | + } |
| 293 | + rc = subscribe_module("infix-firewall", &confd, 0); |
| 294 | + if (rc) { |
| 295 | + ERROR("Failed to subscribe to infix-firewall"); |
| 296 | + goto err; |
| 297 | + } |
| 298 | + rc = subscribe_module("infix-meta", &confd, SR_SUBSCR_UPDATE); |
| 299 | + if (rc) { |
| 300 | + ERROR("Failed to subscribe to infix-meta"); |
153 | 301 | goto err; |
154 | | - rc = infix_dhcp_client_init(&confd); |
| 302 | + } |
| 303 | + |
| 304 | + rc = ietf_system_rpc_init(&confd); |
155 | 305 | if (rc) |
156 | 306 | goto err; |
157 | | - rc = infix_dhcp_server_init(&confd); |
| 307 | + rc = infix_containers_rpc_init(&confd); |
158 | 308 | if (rc) |
159 | 309 | goto err; |
160 | | - rc = infix_factory_init(&confd); |
| 310 | + rc = infix_dhcp_server_rpc_init(&confd); |
161 | 311 | if (rc) |
162 | 312 | goto err; |
163 | | - rc = ietf_factory_default_init(&confd); |
| 313 | + |
| 314 | + rc = infix_factory_rpc_init(&confd); |
164 | 315 | if (rc) |
165 | 316 | goto err; |
166 | | - rc = ietf_routing_init(&confd); |
| 317 | + |
| 318 | + rc = ietf_factory_default_rpc_init(&confd); |
167 | 319 | if (rc) |
168 | 320 | goto err; |
169 | | - rc = infix_meta_init(&confd); |
| 321 | + |
| 322 | + rc = infix_firewall_rpc_init(&confd); |
170 | 323 | if (rc) |
171 | 324 | goto err; |
172 | | - rc = infix_system_sw_init(&confd); |
| 325 | + |
| 326 | + rc = infix_system_sw_rpc_init(&confd); |
173 | 327 | if (rc) |
174 | 328 | goto err; |
175 | | - rc = infix_services_init(&confd); |
| 329 | + |
| 330 | + /* Candidate infer configurations */ |
| 331 | + rc = ietf_hardware_candidate_init(&confd); |
176 | 332 | if (rc) |
177 | 333 | goto err; |
178 | | - rc = ietf_hardware_init(&confd); |
| 334 | + |
| 335 | + rc = infix_firewall_candidate_init(&confd); |
179 | 336 | if (rc) |
180 | 337 | goto err; |
181 | | - rc = infix_firewall_init(&confd); |
| 338 | + |
| 339 | + rc = infix_dhcp_server_candidate_init(&confd); |
182 | 340 | if (rc) |
183 | 341 | goto err; |
184 | 342 |
|
| 343 | + rc = infix_dhcp_client_candidate_init(&confd); |
| 344 | + if (rc) |
| 345 | + goto err; |
185 | 346 | /* YOUR_INIT GOES HERE */ |
186 | 347 |
|
187 | 348 | return SR_ERR_OK; |
188 | | - |
189 | 349 | err: |
190 | 350 | ERROR("init failed: %s", sr_strerror(rc)); |
191 | 351 | if (confd.root) |
|
0 commit comments