blob: edab1137a38dc49fe6326f074759758b9a34bc36 [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#pragma once
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/server/object.hpp>
5#include <string>
Deepak Kodihalli9fab2792018-08-28 07:47:11 -05006#include <phosphor-logging/log.hpp>
Deepak Kodihallic4966192018-08-23 02:19:58 -05007#include "xyz/openbmc_project/Network/Client/server.hpp"
8
9namespace phosphor
10{
11namespace rsyslog_config
12{
13
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050014using namespace phosphor::logging;
Deepak Kodihallic4966192018-08-23 02:19:58 -050015using NetworkClient = sdbusplus::xyz::openbmc_project::Network::server::Client;
16using Iface = sdbusplus::server::object::object<NetworkClient>;
17
18/** @class Server
19 * @brief Configuration for rsyslog server
20 * @details A concrete implementation of the
21 * xyz.openbmc_project.Network.Client API, in order to
22 * provide remote rsyslog server's address and port.
23 */
24class Server : public Iface
25{
26 public:
27 Server() = delete;
28 Server(const Server&) = delete;
29 Server& operator=(const Server&) = delete;
30 Server(Server&&) = delete;
31 Server& operator=(Server&&) = delete;
32 virtual ~Server() = default;
33
34 /** @brief Constructor to put object onto bus at a dbus path.
35 * @param[in] bus - Bus to attach to.
36 * @param[in] path - Path to attach at.
37 * @param[in] filePath - rsyslog remote logging config file
38 */
39 Server(sdbusplus::bus::bus& bus,
40 const std::string& path,
41 const char* filePath) :
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050042 Iface(bus, path.c_str(), true),
Deepak Kodihallic4966192018-08-23 02:19:58 -050043 configFilePath(filePath)
44 {
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050045 try
46 {
47 restore(configFilePath.c_str());
48 }
49 catch(const std::exception& e)
50 {
51 log<level::ERR>(e.what());
52 }
53
54 emit_object_added();
Deepak Kodihallic4966192018-08-23 02:19:58 -050055 }
56
57 using NetworkClient::address;
58 using NetworkClient::port;
59
60 /** @brief Override that updates rsyslog config file as well
61 * @param[in] value - remote server address
62 * @returns value of changed address
63 */
64 virtual std::string address(std::string value) override;
65
66 /** @brief Override that updates rsyslog config file as well
67 * @param[in] value - remote server port
68 * @returns value of changed port
69 */
70 virtual uint16_t port(uint16_t value) override;
71
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -050072 /** @brief Restart rsyslog's systemd unit
73 */
74 virtual void restart();
75
Deepak Kodihallic4966192018-08-23 02:19:58 -050076 private:
77 /** @brief Update remote server address and port in
78 * rsyslog config file.
79 * @param[in] serverAddress - remote server address
80 * @param[in] serverPort - remote server port
81 * @param[in] filePath - rsyslog config file path
82 */
83 void writeConfig(
84 const std::string& serverAddress,
85 uint16_t serverPort,
86 const char* filePath);
87
Deepak Kodihalli4db81462018-08-27 06:01:46 -050088 /** @brief Checks if input IP address is valid (uses getaddrinfo)
89 * @param[in] address - server address
90 * @returns true if valid, false otherwise
91 */
92 bool addressValid(const std::string& address);
93
Deepak Kodihalli9fab2792018-08-28 07:47:11 -050094 /** @brief Populate existing config into D-Bus properties
95 * @param[in] filePath - rsyslog config file path
96 */
97 void restore(const char* filePath);
98
Deepak Kodihallic4966192018-08-23 02:19:58 -050099 std::string configFilePath{};
100};
101
102} // namespace rsyslog_config
103} // namespace phosphor