|
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 |
|
@@ -92,6 +93,97 @@ int core_post_hook(sr_session_ctx_t *session, uint32_t sub_id, const char *modul |
92 | 93 | return SR_ERR_OK; |
93 | 94 | } |
94 | 95 |
|
| 96 | +static int change_cb(sr_session_ctx_t *session, uint32_t sub_id, const char *module_name, |
| 97 | + const char *xpath, sr_event_t event, uint32_t request_id, void *_confd) |
| 98 | +{ |
| 99 | + struct confd *confd = _confd; |
| 100 | + char *output = NULL; |
| 101 | + struct lyd_node *diff = NULL, *config = NULL; |
| 102 | + sr_data_t *cfg = NULL; |
| 103 | + |
| 104 | + int rc = SR_ERR_OK; |
| 105 | + static uint32_t last_id = 0; |
| 106 | + static uint32_t last_event = -1; |
| 107 | + if (request_id == last_id && last_event == event) |
| 108 | + return SR_ERR_OK; |
| 109 | + last_id = request_id; |
| 110 | + last_event = event; |
| 111 | + |
| 112 | + if (event == SR_EV_CHANGE || event == SR_EV_DONE) { |
| 113 | + rc = srx_get_diff(session, &diff); |
| 114 | + if (rc != SR_ERR_OK) { |
| 115 | + ERROR("Failed to get diff: %d", rc); |
| 116 | + return rc; |
| 117 | + } |
| 118 | + rc = sr_get_data(session, "//.", 0, 0, 0, &cfg); |
| 119 | + if (rc || !cfg) |
| 120 | + goto free_diff; |
| 121 | + |
| 122 | + config = cfg->tree; |
| 123 | + } |
| 124 | + |
| 125 | + // For logging: Write to file instead of using ERROR() to avoid truncation |
| 126 | + if (diff) { |
| 127 | + FILE *f = fopen("/tmp/sysrepo_diff.json", "w"); |
| 128 | + if (f) { |
| 129 | + lyd_print_file(f, diff, LYD_JSON, |
| 130 | + LYD_PRINT_WITHSIBLINGS | LYD_PRINT_WD_ALL); |
| 131 | + fclose(f); |
| 132 | + ERROR("req_id=%u event=%d module=%s - Full diff written to /tmp/sysrepo_diff.json", |
| 133 | + request_id, event, module_name); |
| 134 | + } else { |
| 135 | + // Fallback: print to memory but limit what we log |
| 136 | + lyd_print_mem(&output, diff, LYD_JSON, LYD_PRINT_WITHSIBLINGS); |
| 137 | + if (output) { |
| 138 | + // Only log first 500 chars to avoid truncation |
| 139 | + ERROR("req_id=%u event=%d module=%s - Diff (truncated): %.500s...", |
| 140 | + request_id, event, module_name, output); |
| 141 | + free(output); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /* ietf-interfaces */ |
| 147 | + ietf_interfaces_change(session, config, diff, event, confd); |
| 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_change(session, config, diff, event, confd); |
| 157 | + |
| 158 | + /* ietf-syslog*/ |
| 159 | + ietf_syslog_change(session, config, diff, event, confd); |
| 160 | + |
| 161 | + /* ietf-system */ |
| 162 | + ietf_system_change(session, config, diff, event, confd); |
| 163 | + |
| 164 | + /* infix-containers */ |
| 165 | + infix_containers_change(session, config, diff, event, confd); |
| 166 | + |
| 167 | + /* ietf-hardware */ |
| 168 | + ietf_hardware_change(session, config, diff, event, confd); |
| 169 | + |
| 170 | + /* ietf-routing */ |
| 171 | + ietf_routing_change(session, config, diff, event, confd); |
| 172 | + |
| 173 | + /* infix-dhcp-server */ |
| 174 | + infix_dhcp_server_change(session, config, diff, event, confd); |
| 175 | + |
| 176 | + /* infix-firewall */ |
| 177 | + infix_firewall_change(session, config, diff, event, confd); |
| 178 | + |
| 179 | + if (cfg) |
| 180 | + sr_release_data(cfg); |
| 181 | +free_diff: |
| 182 | + lyd_free_tree(diff); |
| 183 | + return rc; |
| 184 | + } |
| 185 | + |
| 186 | + |
95 | 187 | int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
96 | 188 | { |
97 | 189 | int log_opts = LOG_PID | LOG_NDELAY; |
@@ -136,57 +228,78 @@ int sr_plugin_init_cb(sr_session_ctx_t *session, void **priv) |
136 | 228 | */ |
137 | 229 | confd.ifquirks = json_load_file("/etc/product/interface-quirks.json", 0, NULL); |
138 | 230 |
|
139 | | - rc = ietf_interfaces_init(&confd); |
140 | | - if (rc) |
141 | | - goto err; |
142 | | - rc = ietf_keystore_init(&confd); |
143 | | - if (rc) |
144 | | - goto err; |
145 | | - rc = ietf_syslog_init(&confd); |
146 | | - if (rc) |
| 231 | + REGISTER_CHANGE(confd.session, "ietf-interfaces", "//.", |
| 232 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 233 | + REGISTER_CHANGE(confd.session, "infix-dhcp-client", "//.", |
| 234 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 235 | + REGISTER_CHANGE(confd.session, "ietf-keystore", "//.", |
| 236 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 237 | + REGISTER_CHANGE(confd.session, "ietf-syslog", "//.", |
| 238 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 239 | + REGISTER_CHANGE(confd.session, "infix-services", "//.", |
| 240 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 241 | + REGISTER_CHANGE(confd.session, "ietf-system", "//.", |
| 242 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 243 | + REGISTER_CHANGE(confd.session, "ieee802-dot1ab-lldp", "//.", |
| 244 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 245 | +#ifdef CONTAINERS |
| 246 | + REGISTER_CHANGE(confd.session, "infix-containers", "//.", |
| 247 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 248 | +#endif |
| 249 | + REGISTER_CHANGE(confd.session, "infix-dhcp-server", "//.", |
| 250 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 251 | + |
| 252 | + REGISTER_CHANGE(confd.session, "ietf-routing", "//.", |
| 253 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 254 | + |
| 255 | + REGISTER_CHANGE(confd.session, "ietf-hardware", "//.", |
| 256 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 257 | + REGISTER_CHANGE(confd.session, "infix-firewall", "//.", |
| 258 | + SR_SUBSCR_CHANGE_ALL_MODULES, change_cb, &confd, &confd.sub); |
| 259 | + |
| 260 | + rc = ietf_keystore_update_init(&confd); |
| 261 | + if(rc) |
147 | 262 | goto err; |
148 | 263 | rc = ietf_system_init(&confd); |
149 | 264 | if (rc) |
150 | 265 | goto err; |
151 | 266 | rc = infix_containers_init(&confd); |
152 | 267 | if (rc) |
153 | 268 | goto err; |
154 | | - rc = infix_dhcp_client_init(&confd); |
155 | | - if (rc) |
156 | | - goto err; |
| 269 | + |
157 | 270 | rc = infix_dhcp_server_init(&confd); |
158 | 271 | if (rc) |
159 | 272 | goto err; |
| 273 | + |
160 | 274 | rc = infix_factory_init(&confd); |
161 | 275 | if (rc) |
162 | 276 | goto err; |
| 277 | + |
163 | 278 | rc = ietf_factory_default_init(&confd); |
164 | 279 | if (rc) |
165 | 280 | goto err; |
166 | | - rc = ietf_routing_init(&confd); |
167 | | - if (rc) |
168 | | - goto err; |
| 281 | + |
169 | 282 | rc = infix_meta_init(&confd); |
170 | 283 | if (rc) |
171 | 284 | goto err; |
| 285 | + |
172 | 286 | rc = infix_system_sw_init(&confd); |
173 | 287 | if (rc) |
174 | 288 | goto err; |
175 | | - rc = infix_services_init(&confd); |
176 | | - if (rc) |
177 | | - goto err; |
| 289 | + |
178 | 290 | rc = ietf_hardware_init(&confd); |
179 | 291 | if (rc) |
180 | 292 | goto err; |
| 293 | + |
181 | 294 | rc = infix_firewall_init(&confd); |
182 | 295 | if (rc) |
183 | 296 | goto err; |
184 | 297 |
|
185 | 298 | /* YOUR_INIT GOES HERE */ |
186 | 299 |
|
187 | 300 | return SR_ERR_OK; |
188 | | - |
189 | 301 | err: |
| 302 | +fail: |
190 | 303 | ERROR("init failed: %s", sr_strerror(rc)); |
191 | 304 | if (confd.root) |
192 | 305 | json_decref(confd.root); |
|
0 commit comments