Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event notifications: receiving notifications works, but it shouldn't #577

Open
khromenokroman opened this issue Nov 21, 2024 · 2 comments
Open
Labels

Comments

@khromenokroman
Copy link
Contributor

khromenokroman commented Nov 21, 2024

so it should be that I didn't add any models to yang

notification event_tarsaction {
    description "Transaction notification event";
    leaf severiy {
        type string;
        description "Message severity";
    }
    leaf message {
        type string;
        description "Message";
    }
}

but at the same time notifications are coming
according to the documentation https://clixon-docs.readthedocs.io/en/latest/events.html it is necessary to create a description in yang, but it works without it, or is this not an optional condition, or did I do something wrong?

plugin backend

static constexpr char const NAME_PLUGIN[]{"clixon-plug-0-init"};
static constexpr char const NAME_STREAM_TRANSACTION[]{"n_notification_stream"};

int commit_validate(clicon_handle handle, [[maybe_unused]] transaction_data td) {
    auto my_logger = (ngj::Logger *)(((transaction_data_t *)td)->td_arg);
    my_logger->write(ngj::Event::Type::NOTICE, "commit validate", "Transaction stage validate start");

    my_logger->write(ngj::Event::Type::WARNING, "commit validate", "SEND NOTIFY!");

    std::string data = "Hello i'm message :) from plugin backend(validate)";
    // clang-format off
    const char *event_template =
        "<event_transaction xmlns=\"%s\">"
            "<severity>HIGH</severity>"
            "<message>%s</message>"
        "</event_transaction>";
    // clang-format on

    char name_stream[sizeof NAME_STREAM_TRANSACTION];
    memset(name_stream,'\0',sizeof name_stream);
    std::strcpy(name_stream, NAME_STREAM_TRANSACTION);

    if (stream_notify(handle, name_stream, event_template, NETCONF_BASE_NAMESPACE, data.c_str()) < 0) {
        my_logger->write(ngj::Event::Type::WARNING, "commit commit", "Error can't send stream");
        return -1;
    }

    /* только для отладки */
    std::this_thread::sleep_for(std::chrono::seconds(5));
    my_logger->write(ngj::Event::Type::WARNING, "commit validate", "SEND NOTIFY END!");

    my_logger->write(ngj::Event::Type::NOTICE, "commit validate", "Transaction stage validate is done");

    return 0;
}
clixon_plugin_api *clixon_plugin_init(clicon_handle handle) {
    clixon_log(handle, LOG_NOTICE, "[%s] Load plugin ver: %d.%d.%d rev. %d", NAME_PLUGIN, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TWEAK);

    /* создание стрима сообщений с нотификациями о транзакции */
    if (stream_add(handle, NAME_STREAM_TRANSACTION, "Notification for message transaction", 0, nullptr) < 0) {
        clixon_log(handle, LOG_ERR, "[%s] Can not stream add for notifications", NAME_PLUGIN);
    }

    strncpy(api.ca_name, NAME_PLUGIN, strlen(NAME_PLUGIN));
    api.ca_name[strlen(NAME_PLUGIN)] = '\0';
    api.ca_start = plugin_start;
    api.ca_exit = plugin_exit;
    api.u.cau_backend.cb_trans_begin = commit_begin;
    api.u.cau_backend.cb_trans_validate = commit_validate;
    api.u.cau_backend.cb_trans_commit = commit_commit;
    api.u.cau_backend.cb_trans_commit_done = commit_done;
    api.u.cau_backend.cb_trans_end = commit_end;

    return &api;
}

cli:

sysadmin@pc/> notify 
sysadmin@pc/> commit

clispec

notify("Получить нотификации из backend"), cli_notify("transactions_stream", "1", "xml");
no("Команда отмены") notify("Отключить нотификации из backend"), cli_notify("transactions_stream", "0", "xml");

output:

<notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
   <eventTime>2024-11-21T20:03:33.470347Z</eventTime>
   <event_transaction xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <severity>HIGH</severity>
      <message>Hello i'm message :) from plugin backend(validate)</message>
   </event_transaction>
</notification>
<notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
   <eventTime>2024-11-21T20:03:38.470887Z</eventTime>
   <event_transaction xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <severity>HIGH</severity>
      <message>Hello i'm message :) from plugin backend(commit)</message>
   </event_transaction>
</notification>
<notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
   <eventTime>2024-11-21T20:03:43.471381Z</eventTime>
   <event_transaction xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <severity>HIGH</severity>
      <message>Hello i'm message :) from plugin backend(done)</message>
   </event_transaction>
</notification>
<notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
   <eventTime>2024-11-21T20:03:48.475482Z</eventTime>
   <event_transaction xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <severity>HIGH</severity>
      <message>Hello i'm message :) from plugin backend(end)</message>
   </event_transaction>
</notification>
@olofhagsand
Copy link
Member

Correct, there is no YANG check of event notifications.
There should be a YANG bind or check in or around send_msg_notify_xml()

@olofhagsand
Copy link
Member

On rcv side:

  • CLI: cli_notification_cb should do YB_MODULE/YB_RPC when calling clixon_xml_parse_string()
  • NETCONF: Already (partly?) done in netconf_notification_cb
  • RESTCONF: stream_native_backend_cb and stream_fcgi_backend_cb

@olofhagsand olofhagsand changed the title receiving notifications works, but it shouldn't Event notifications: receiving notifications works, but it shouldn't Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants