|
1 | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
2 | 2 |
|
| 3 | +#include <srx/srx_val.h> |
3 | 4 | #include <srx/common.h> |
4 | 5 | #include "core.h" |
5 | 6 |
|
@@ -88,6 +89,118 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul |
88 | 89 | return SR_ERR_OK; |
89 | 90 | } |
90 | 91 |
|
| 92 | +static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, |
| 93 | + const char *xpath, sr_event_t event, uint32_t request_id, void *_confd) |
| 94 | +{ |
| 95 | + struct confd *confd = _confd; |
| 96 | + char *output = NULL; |
| 97 | + struct lyd_node *diff = NULL, *config = NULL; |
| 98 | + sr_data_t *cfg = NULL; |
| 99 | + |
| 100 | + int rc = SR_ERR_OK; |
| 101 | + static uint32_t last_id = 0; |
| 102 | + static uint32_t last_event = -1; |
| 103 | + if (request_id == last_id && last_event == event) |
| 104 | + return SR_ERR_OK; |
| 105 | + last_id = request_id; |
| 106 | + last_event = event; |
| 107 | + // Free previous diff if it exists |
| 108 | + // Get the new diff |
| 109 | + |
| 110 | + if (event == SR_EV_CHANGE || event == SR_EV_DONE) { |
| 111 | + rc = srx_get_diff(session, &diff); |
| 112 | + if (rc != SR_ERR_OK) { |
| 113 | + ERROR("Failed to get diff: %d", rc); |
| 114 | + return rc; |
| 115 | + } |
| 116 | + rc = sr_get_data(session, "//.", 0, 0, 0, &cfg); |
| 117 | + if (rc || !cfg) |
| 118 | + goto free_diff; |
| 119 | + |
| 120 | + config = cfg->tree; |
| 121 | + } |
| 122 | + |
| 123 | + // For logging: Write to file instead of using ERROR() to avoid truncation |
| 124 | + if (diff) { |
| 125 | + FILE *f = fopen("/tmp/sysrepo_diff.json", "w"); |
| 126 | + if (f) { |
| 127 | + lyd_print_file(f, diff, LYD_JSON, |
| 128 | + LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL); |
| 129 | + fclose(f); |
| 130 | + ERROR("req_id=%u event=%d module=%s - Full diff written to /tmp/sysrepo_diff.json", |
| 131 | + request_id, event, module_name); |
| 132 | + } else { |
| 133 | + // Fallback: print to memory but limit what we log |
| 134 | + lyd_print_mem(&output, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 135 | + if (output) { |
| 136 | + // Only log first 500 chars to avoid truncation |
| 137 | + ERROR("req_id=%u event=%d module=%s - Diff (truncated): %.500s...", |
| 138 | + request_id, event, module_name, output); |
| 139 | + free(output); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + /* Prepare diff */ |
| 144 | + // Iterera igenom alla tills ingen lägger till något mer |
| 145 | + |
| 146 | + /* ietf-interfaces */ |
| 147 | + ietf_interfaces_change(session, config, diff, event, confd); /* TODO: WiFi depends on keystore */ |
| 148 | + |
| 149 | + /* infix-dhcp-client*/ |
| 150 | + infix_dhcp_client_change(session, config, diff, event, confd); |
| 151 | + |
| 152 | + /* ietf-keystore */ |
| 153 | + ietf_keystore_change(session, config, diff, event, confd); |
| 154 | + |
| 155 | + /* infix-services */ |
| 156 | + infix_services_lldp_change(session, config, diff, event, confd); |
| 157 | + infix_services_mdns_change(session, config, diff, event, confd); /* TODO: Depends on hostname changes */ |
| 158 | + infix_services_ssh_change(session, config, diff, event, confd); /* TODO: Depends on keystore changes*/ |
| 159 | + infix_services_web_change(session, config, diff, event, confd); |
| 160 | + infix_services_ttyd_change(session, config, diff, event, confd); |
| 161 | + infix_services_restconf_change(session, config, diff, event, confd); |
| 162 | + infix_services_netbrowse_change(session, config, diff, event, confd); |
| 163 | + |
| 164 | + /* ietf-syslog*/ |
| 165 | + ietf_syslog_file_change(session, config, diff, event, confd); |
| 166 | + ietf_syslog_remote_change(session, config, diff, event, confd); |
| 167 | + ietf_syslog_rotate_change(session, config, diff, event, confd); |
| 168 | + ietf_syslog_server_change(session, config, diff, event, confd); /* TODO: Depends on hostname changes? */ |
| 169 | + |
| 170 | + /* ietf-system */ |
| 171 | + ietf_system_change_auth(session, config, diff, event, confd); |
| 172 | + ietf_system_change_ntp(session, config, diff, event, confd); |
| 173 | + ietf_system_change_dns(session, config, diff, event, confd); |
| 174 | + ietf_system_change_editor(session,config, diff, event, confd); |
| 175 | + ietf_system_change_clock(session, config, diff, event, confd); |
| 176 | + ietf_system_change_hostname(session, config, diff, event, confd); |
| 177 | + ietf_system_change_motd(session, config, diff, event, confd); |
| 178 | + ietf_system_change_motd_banner(session, config, diff, event, confd); |
| 179 | + ietf_system_change_nacm(session, config, diff, event, confd); /* Must be called after ietf_system_change_auth, which create the users */ |
| 180 | + |
| 181 | + /* infix-containers */ |
| 182 | + infix_containers_change(session, config, diff, event, confd); |
| 183 | + |
| 184 | + /* ietf-hardware */ |
| 185 | + ietf_hardware_change(session, config, diff, event, confd); |
| 186 | + |
| 187 | + /* ietf-routing */ |
| 188 | + ietf_routing_change(session, config, diff, event, confd); |
| 189 | + |
| 190 | + /* infix-dhcp-server */ |
| 191 | + infix_dhcp_server_change(session, config, diff, event, confd); /* TODO: Depends on hostname? Line: 350*/ |
| 192 | + |
| 193 | + /* infix-firewall */ |
| 194 | + infix_firewall_change(session, config, diff, event, confd); |
| 195 | + |
| 196 | + if (cfg) |
| 197 | + sr_release_data(cfg); |
| 198 | +free_diff: |
| 199 | + lyd_free_tree(diff); |
| 200 | + return rc; |
| 201 | + } |
| 202 | + |
| 203 | + |
91 | 204 | int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
92 | 205 | { |
93 | 206 | int log_opts = LOG_PID | LOG_NDELAY; |
@@ -132,57 +245,78 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
132 | 245 | */ |
133 | 246 | confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL); |
134 | 247 |
|
135 | | - rc = ietf_interfaces_init(&confd); |
136 | | - if (rc) |
137 | | - goto err; |
138 | | - rc = ietf_keystore_init(&confd); |
139 | | - if (rc) |
140 | | - goto err; |
141 | | - rc = ietf_syslog_init(&confd); |
142 | | - if (rc) |
| 248 | + REGISTER_CHANGE(confd.session, "ietf-interfaces", "//.", |
| 249 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 250 | + REGISTER_CHANGE(confd.session, "infix-dhcp-client", "//.", |
| 251 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 252 | + REGISTER_CHANGE(confd.session, "ietf-keystore", "//.", |
| 253 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 254 | + REGISTER_CHANGE(confd.session, "ietf-syslog", "//.", |
| 255 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 256 | + REGISTER_CHANGE(confd.session, "infix-services", "//.", |
| 257 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 258 | + REGISTER_CHANGE(confd.session, "ietf-system", "//.", |
| 259 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 260 | + REGISTER_CHANGE(confd.session, "ieee802-dot1ab-lldp", "//.", |
| 261 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 262 | +#ifdef CONTAINERS |
| 263 | + REGISTER_CHANGE(confd.session, "infix-containers", "//.", |
| 264 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 265 | +#endif |
| 266 | + REGISTER_CHANGE(confd.session, "infix-dhcp-server", "//.", |
| 267 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 268 | + |
| 269 | + REGISTER_CHANGE(confd.session, "ietf-routing", "//.", |
| 270 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 271 | + |
| 272 | + REGISTER_CHANGE(confd.session, "ietf-hardware", "//", |
| 273 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 274 | + REGISTER_CHANGE(confd.session, "infix-firewall", "//.", |
| 275 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 276 | + |
| 277 | + rc = ietf_keystore_update_init(&confd); |
| 278 | + if(rc) |
143 | 279 | goto err; |
144 | 280 | rc = ietf_system_init(&confd); |
145 | 281 | if (rc) |
146 | 282 | goto err; |
147 | 283 | rc = infix_containers_init(&confd); |
148 | 284 | if (rc) |
149 | 285 | goto err; |
150 | | - rc = infix_dhcp_client_init(&confd); |
151 | | - if (rc) |
152 | | - goto err; |
| 286 | + |
153 | 287 | rc = infix_dhcp_server_init(&confd); |
154 | 288 | if (rc) |
155 | 289 | goto err; |
| 290 | + |
156 | 291 | rc = infix_factory_init(&confd); |
157 | 292 | if (rc) |
158 | 293 | goto err; |
| 294 | + |
159 | 295 | rc = ietf_factory_default_init(&confd); |
160 | 296 | if (rc) |
161 | 297 | goto err; |
162 | | - rc = ietf_routing_init(&confd); |
163 | | - if (rc) |
164 | | - goto err; |
| 298 | + |
165 | 299 | rc = infix_meta_init(&confd); |
166 | 300 | if (rc) |
167 | 301 | goto err; |
| 302 | + |
168 | 303 | rc = infix_system_sw_init(&confd); |
169 | 304 | if (rc) |
170 | 305 | goto err; |
171 | | - rc = infix_services_init(&confd); |
172 | | - if (rc) |
173 | | - goto err; |
| 306 | + |
174 | 307 | rc = ietf_hardware_init(&confd); |
175 | 308 | if (rc) |
176 | 309 | goto err; |
| 310 | + |
177 | 311 | rc = infix_firewall_init(&confd); |
178 | 312 | if (rc) |
179 | 313 | goto err; |
180 | 314 |
|
181 | 315 | /* YOUR_INIT GOES HERE */ |
182 | 316 |
|
183 | 317 | return SR_ERR_OK; |
184 | | - |
185 | 318 | err: |
| 319 | +fail: |
186 | 320 | ERROR("init failed: %s", sr_strerror(rc)); |
187 | 321 | if (confd.root) |
188 | 322 | json_decref(confd.root); |
|
0 commit comments