aboutsummaryrefslogtreecommitdiffstats
path: root/test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp
diff options
context:
space:
mode:
authorWojciech Sliwka <wojciech.sliwka@nokia.com>2019-07-10 13:48:52 +0200
committerGary Wu <gary.wu@futurewei.com>2019-07-19 17:18:49 +0000
commit032ff22ef20b59950a8b5fae8d2ba6d03e93ac93 (patch)
tree98058dcf2726b3f432cecffc42bd7cbccca3555e /test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp
parent79c9b78adb7fbc943fd2aee7d333fd3cadf5b8f3 (diff)
Opensourcing new version of Simulator
Additional info in README.md Issue-ID: INT-1134 Signed-off-by: Wojciech Sliwka <wojciech.sliwka@nokia.com> Change-Id: I06d41fd3f361b7a451b30b702882810e4136a129
Diffstat (limited to 'test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp')
-rw-r--r--test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp b/test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp
new file mode 100644
index 000000000..225fe03a4
--- /dev/null
+++ b/test/mocks/pnfsimulator/netconfsimulator/netopeer-change-saver-native/SysrepoCallback.cpp
@@ -0,0 +1,108 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Simulator
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+#include "SysrepoCallback.h"
+#define CREATED "CREATED"
+#define DELETED "DELETED"
+#define MODIFIED "MODIFIED"
+#define MOVED "MOVED"
+#define XPATH_MAX_LEN 100
+
+
+int SysrepoCallback::module_change(sysrepo::S_Session sess, const char *module_name, sr_notif_event_t event, void *private_ctx) {
+ {
+ if (event == SR_EV_APPLY) {
+ char change_path[XPATH_MAX_LEN];
+
+ try {
+#ifdef DEBUG
+ std::cout << "\n ========== CHANGES: =============================================\n" << std::endl;
+#endif
+ snprintf(change_path, XPATH_MAX_LEN, "/%s:*", module_name);
+ auto it = sess->get_changes_iter(&change_path[0]);
+ while (auto change = sess->get_change_next(it)) {
+ std::string message = create_message(change);
+ std::cout<<message<<std::endl;
+ kafkaWrapper->kafka_send_message(message);
+ }
+#ifdef DEBUG
+ std::cout << "\n ========== END OF CHANGES =======================================\n" << std::endl;
+#endif
+ } catch( const std::exception& e ) {
+ std::cerr << e.what() << std::endl;
+ }
+ }
+ return SR_ERR_OK;
+ }
+}
+
+SysrepoCallback::SysrepoCallback(std::shared_ptr<KafkaWrapper> wrapper) {
+ this->kafkaWrapper = wrapper;
+}
+
+std::string SysrepoCallback::create_message(sysrepo::S_Change change) {
+ std::string change_details;
+ sysrepo::S_Val new_val = change->new_val();
+ sysrepo::S_Val old_val = change->old_val();
+
+ switch (change->oper()) {
+ case SR_OP_CREATED:
+ if (nullptr != new_val) {
+ change_details.append(CREATED).append(": ").append(new_val->to_string());
+ }
+ break;
+ case SR_OP_DELETED:
+ if (nullptr != old_val) {
+ change_details.append(DELETED).append(": ").append(old_val->to_string());
+ }
+ break;
+ case SR_OP_MODIFIED:
+ if (nullptr != old_val && nullptr != new_val) {
+ change_details.append(MODIFIED).append(": ").append(": old value: ").append(old_val->to_string())
+ .append(", new value: ").append(new_val->to_string());
+ }
+ break;
+ case SR_OP_MOVED:
+ if (nullptr != old_val && nullptr != new_val) {
+ change_details.append(MOVED).append(": ").append(new_val->to_string())
+ .append(" after ").append(old_val->to_string());
+ } else if (nullptr != new_val) {
+ change_details.append(MOVED).append(": ").append(new_val->xpath()).append(" last");
+ }
+ break;
+ }
+ return change_details;
+}
+
+void SysrepoCallback::print_current_config(sysrepo::S_Session session, const char *module_name) {
+ char select_xpath[XPATH_MAX_LEN];
+ try {
+ snprintf(select_xpath, XPATH_MAX_LEN, "/%s:*//*", module_name);
+
+ auto values = session->get_items(&select_xpath[0]);
+ if (values == nullptr)
+ return;
+
+ for(unsigned int i = 0; i < values->val_cnt(); i++)
+ std::cout << values->val(i)->to_string();
+ } catch( const std::exception& e ) {
+ std::cout << e.what() << std::endl;
+ }
+}