-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin-kea.cc
102 lines (78 loc) · 2.69 KB
/
plugin-kea.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/// @file plugin-kea.cc
/// @author Tomek Mrugalski, Marcin Siodelski
/// @brief plugin for sysrepo datastore for ISC Kea
///
/// @copyright
/// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
///
/// This Source Code Form is subject to the terms of the Mozilla Public
/// License, v. 2.0. If a copy of the MPL was not distributed with this
/// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <cstdio>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include "plugin-kea.h"
#include "yang-kea.h"
extern "C" {
#include "sysrepo.h"
using namespace std;
const string KEA_CONTROL_SOCKET = "/tmp/kea-dhcp6-ctrl.sock";
const string KEA_CONTROL_CLIENT = CLIENT_DIR "/ctrl-channel-cli";
const string CFG_TEMP_FILE = "/tmp/kea-plugin-gen-cfg.json";
/* retrieves & prints current Kea configuration */
static void
retrieve_current_config(sr_session_ctx_t *session)
{
SysrepoKea interface(session);
string json = interface.getConfig();
std::ofstream fs;
fs.open(CFG_TEMP_FILE.c_str(), std::ofstream::out);
fs << json;
fs.close();
string cmd = KEA_CONTROL_CLIENT + " " + KEA_CONTROL_SOCKET + " " + CFG_TEMP_FILE;
system (cmd.c_str());
// remove(CFG_TEMP_FILE.c_str());
std::cout << json << std::endl;
}
static int
module_change_cb(sr_session_ctx_t *session, const char *module_name, sr_notif_event_t event,
void *private_ctx)
{
cerr << "plugin-kea configuration has changed" << endl;
retrieve_current_config(session);
return SR_ERR_OK;
}
int
sr_plugin_init_cb(sr_session_ctx_t *session, void **private_ctx)
{
sr_subscription_ctx_t *subscription = NULL;
int rc = SR_ERR_OK;
rc = sr_module_change_subscribe(session, "ietf-kea-dhcpv6", module_change_cb, NULL,
0, SR_SUBSCR_DEFAULT, &subscription);
//rc = sr_subtree_change_subscribe(session, "/ietf-kea-dhcpv6:server/*", module_change_cb, NULL,
// 0, SR_SUBSCR_DEFAULT, &subscription);
if (SR_ERR_OK != rc) {
goto error;
}
cerr << "plugin-kea initialized successfully" << endl;
retrieve_current_config(session);
/* set subscription as our private context */
*private_ctx = subscription;
return SR_ERR_OK;
error:
cerr << "plugin-kea initialization failed: " << sr_strerror(rc) << endl;
sr_unsubscribe(session, subscription);
return rc;
}
void
sr_plugin_cleanup_cb(sr_session_ctx_t *session, sr_subscription_ctx_t *private_ctx)
{
/* subscription was set as our private context */
sr_unsubscribe(session, private_ctx);
cout << "pluging-kea plugin cleanup finished" << endl;
}
}