RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
deploymentDaemonLifeCycle.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_REUSABLECOMPONENT_DEPLOYMENT_DAEMON_LIFE_CYCLE_HPP
13#define RTCTK_REUSABLECOMPONENT_DEPLOYMENT_DAEMON_LIFE_CYCLE_HPP
14
16
22
24
25#include <mal/Cii.hpp>
26
27#include <memory>
28#include <string>
29
31
32using namespace rtctk::componentFramework;
33
35
36// this we reply over MAL for commands that succeeds
37inline const std::string STD_OK_REPLY = "OK";
38
40
41// High level exception types returned via MAL
42
47class DeploymentDaemonRequestAborted : public rtctkif::DeploymentException {
48public:
49 static constexpr int32_t ERROR_CODE = 11;
51 : rtctkif::DeploymentException("Request aborted.", ERROR_CODE) {
52 }
53};
54
60class NoDeployment : public rtctkif::DeploymentException {
61public:
62 static constexpr int32_t ERROR_CODE = 11;
63 NoDeployment(const std::string& request_id, const std::string& state_id)
64 : rtctkif::DeploymentException("No active deployment. (Request " + request_id +
65 " rejected in state " + state_id + ")",
66 ERROR_CODE) {
67 }
68};
69
75class DeploymentAlreadyExists : public rtctkif::DeploymentException {
76public:
77 static constexpr int32_t ERROR_CODE = 12;
78 DeploymentAlreadyExists(const std::string& request_id, const std::string& state_id)
79 : rtctkif::DeploymentException("Active deployment already exists. (Request " + request_id +
80 " rejected in state " + state_id + ")",
81 ERROR_CODE) {
82 }
83};
84
89class DeploymentDaemonRequestRejected : public rtctkif::DeploymentException {
90public:
94 static constexpr int32_t ERROR_CODE = 10;
95 DeploymentDaemonRequestRejected(const std::string& request_id, const std::string& state_id)
96 : rtctkif::DeploymentException(
97 "Request " + request_id + " rejected in state " + state_id + ")", ERROR_CODE) {
98 }
99};
100
105class DeploymentDaemonRequestFailed : public rtctkif::DeploymentException {
106public:
110 static constexpr int32_t ERROR_CODE = 501;
111 explicit DeploymentDaemonRequestFailed(const std::string& message)
112 : rtctkif::DeploymentException(message, ERROR_CODE) {
113 }
114};
115
123 public:
124 virtual void ActivityDeploying(cfw::StopToken st, std::string args) = 0;
125 virtual void ActivityUndeploying(cfw::StopToken st) = 0;
126
127 virtual std::string ActionGetActiveDeployment() = 0;
128 virtual std::string ActionGetDescription() = 0;
129
130 virtual std::vector<std::string> ActionGetDeploymentSets() = 0;
131
132 virtual std::vector<std::string> ActionGetComponents() = 0;
133 virtual std::vector<std::string> ActionGetActiveComponents() = 0;
134
135 virtual void ActionStartComponent(std::string args) = 0;
136 virtual void ActionStopComponent(std::string args) = 0;
137
138 virtual void ActivityStarting(cfw::StopToken st) = 0;
139
140 virtual std::vector<std::string> ActionGetServices() = 0;
141 virtual std::vector<std::string> ActionGetActiveServices() = 0;
142 };
143
145 public:
147 : m_replier(replier), m_engine(engine) {
148 }
149
150 virtual ~InputStage() = default;
151
152 virtual void Start() {
153 // exit event is used by SIGTERM, SIGINT and SIGHUP
154 m_engine.RegisterExitEvent(std::make_unique<deployment_daemon_events::Exit>());
155 // deployment daemon MAL commands
157 }
158
159 protected:
162 };
163
165 public:
167 // Handlers ###################################################################
168
169 // clang-format off
170 engine.RegisterRejectHandler<deployment_daemon_events::Reset, DeploymentDaemonRequestRejected>();
171 engine.RegisterRejectHandler<deployment_daemon_events::GetState, DeploymentDaemonRequestRejected>();
172 engine.RegisterRejectHandler<deployment_daemon_events::Exit, DeploymentDaemonRequestRejected>();
173
174 engine.RegisterRejectHandler<deployment_daemon_events::Deploy, DeploymentAlreadyExists>();
175 engine.RegisterRejectHandler<deployment_daemon_events::Undeploy, NoDeployment>();
176 engine.RegisterRejectHandler<deployment_daemon_events::GetActiveDeployment, NoDeployment>();
177 engine.RegisterRejectHandler<deployment_daemon_events::GetDescription, NoDeployment>();
178 engine.RegisterRejectHandler<deployment_daemon_events::GetDeploymentSets, NoDeployment>();
179 engine.RegisterRejectHandler<deployment_daemon_events::GetComponents, NoDeployment>();
180 engine.RegisterRejectHandler<deployment_daemon_events::GetActiveComponents, NoDeployment>();
181
182 engine.RegisterRejectHandler<deployment_daemon_events::StartComponent, NoDeployment>();
183 engine.RegisterRejectHandler<deployment_daemon_events::StopComponent, NoDeployment>();
184
185 engine.RegisterRejectHandler<deployment_daemon_events::GetServices, NoDeployment>();
186 engine.RegisterRejectHandler<deployment_daemon_events::GetActiveServices, NoDeployment>();
187 // clang-format on
188
189 m_custom_success_handler = [this] {
190 m_engine.PostEvent(std::make_unique<deployment_daemon_events::Done>());
191 };
192
193 m_custom_error_handler = [this](const std::exception_ptr& eptr) {
194 m_engine.PostEvent(std::make_unique<deployment_daemon_events::Error>(eptr));
195 };
196
197 engine.RegisterActionStatic<deployment_daemon_events::Deploy>(
198 "ActionDeployEntry", [this](const auto& ev) {
199 assert(not m_tmp_deploy_request);
200 m_tmp_deploy_request = ev.GetPayload();
201 });
202
203 engine.RegisterActionStatic<deployment_daemon_events::Done>(
204 "ActionDeployDone", [this](const auto&) {
205 assert(m_tmp_deploy_request);
206 m_tmp_deploy_request->SetReplyValue(STD_OK_REPLY);
207 m_tmp_deploy_request.reset();
208 });
209 engine.RegisterActionStatic<deployment_daemon_events::Error>(
210 "ActionDeployFailed", [this](const auto& ev) {
211 const auto& eptr = ev.GetPayload();
212 assert(m_tmp_deploy_request);
213 m_tmp_deploy_request->SetException(
215 m_tmp_deploy_request.reset();
216 });
217
218 engine.RegisterActionStatic<deployment_daemon_events::Undeploy>(
219 "ActionUndeployEntry", [this](const auto& ev) {
220 assert(not m_tmp_undeploy_request);
221 m_tmp_undeploy_request = ev.GetPayload();
222 });
223
224 engine.RegisterActionStatic<deployment_daemon_events::Done>(
225 "ActionUndeployDone", [this](const auto&) {
227 m_tmp_undeploy_request->SetReplyValue(STD_OK_REPLY);
229 });
230
231 engine.RegisterActionStatic<deployment_daemon_events::Error>(
232 "ActionUndeployFailed", [this](const auto& ev) {
233 const auto& eptr = ev.GetPayload();
235 m_tmp_undeploy_request->SetException(
238 });
239
240 // Activities #####################################################################
241
242 engine.RegisterActivity(
243 "ActivityDeploying",
244 [this](StopToken stop_token) {
245 std::string arg = m_tmp_deploy_request->GetRequestPayload();
246 // JsonPayload arg = JsonPayload::parse(args);
247 static_cast<BizLogicIf&>(this->m_logic).ActivityDeploying(stop_token, arg);
248 },
251
252 engine.RegisterActivity(
253 "ActivityUndeploying",
254 [this](StopToken stop_token) {
255 static_cast<BizLogicIf&>(this->m_logic).ActivityUndeploying(stop_token);
256 },
259
260 // Reset ######################################################################
261
262 engine.RegisterAction("ActionStartingEntry", [this](auto c) {
264 });
265
266 engine.RegisterAction("ActionStartingDone", [this](auto c) {
267 if (m_tmp_request) {
268 m_tmp_request->SetReplyValue(STD_OK_REPLY);
269 m_tmp_request = nullptr;
270 }
271 });
272
273 // Exit #####################################################################
274
275 engine.RegisterAction("ActionExit", [this](auto c) {
277 if (req == nullptr) {
278 // no event or request
279 return;
280 }
281 req->SetReplyValue(STD_OK_REPLY);
282 m_engine.Stop();
283 });
284
285 // GetState #####################################################################
286
287 engine.RegisterAction("ActionGetState", [this](auto c) {
289 if (req == nullptr) {
290 // no event or request
291 return;
292 }
293 req->SetReplyValue(m_engine.GetState());
294 });
295
296 // SetLogLevel #####################################################################
297
298 engine.RegisterAction("ActionSetLogLevel", [this](auto c) {
300 if (req == nullptr) {
301 // no event or request
302 return;
303 }
304 auto loginfo = req->GetRequestPayload();
305 auto selected_logger = loginfo->getLogger();
306 auto& logger = GetLogger(selected_logger);
307 log4cplus::LogLevelManager llm;
308 auto selected_ll = llm.fromString(loginfo->getLevel());
309 if (selected_ll == log4cplus::NOT_SET_LOG_LEVEL) {
310 req->SetException(DeploymentDaemonRequestFailed("Invalid LogLevel specified"));
311 } else {
312 logger.setLogLevel(selected_ll);
313 LOG4CPLUS_INFO(GetLogger("rtctk"),
314 "Log Level of logger '" << loginfo->getLogger() << "' set to '"
315 << llm.toString(logger.getLogLevel())
316 << "'");
317 req->SetReplyValue(STD_OK_REPLY);
318 }
319 });
320
321 engine.RegisterAction("ActionGetActiveDeployment", [this](auto c) {
323 if (req == nullptr) {
324 // no event or request
325 return;
326 }
327 req->SetReplyValue(
328 static_cast<BizLogicIf&>(this->m_logic).ActionGetActiveDeployment());
329 });
330
331 engine.RegisterAction("ActionGetDescription", [this](auto c) {
333 if (req == nullptr) {
334 // no event or request
335 return;
336 }
337 req->SetReplyValue(static_cast<BizLogicIf&>(this->m_logic).ActionGetDescription());
338 });
339
340 engine.RegisterAction("ActionStartComponent", [this](auto c) {
342 if (req == nullptr) {
343 // no event or request
344 return;
345 }
346 std::string arg = req->GetRequestPayload();
347 try {
348 static_cast<BizLogicIf&>(this->m_logic).ActionStartComponent(arg);
349 req->SetReplyValue(STD_OK_REPLY);
350 } catch (const std::exception& eptr) {
351 req->SetException(rtctkif::DeploymentException(
352 "ActionStartComponent failed: " + std::string(eptr.what()), 601));
353 }
354 });
355
356 engine.RegisterAction("ActionStopComponent", [this](auto c) {
358 if (req == nullptr) {
359 // no event or request
360 return;
361 }
362 std::string arg = req->GetRequestPayload();
363 try {
364 static_cast<BizLogicIf&>(this->m_logic).ActionStopComponent(arg);
365 req->SetReplyValue(STD_OK_REPLY);
366 } catch (const std::exception& eptr) {
367 req->SetException(rtctkif::DeploymentException(
368 "ActionStopComponent failed: " + std::string(eptr.what()), 602));
369 }
370 });
371
372 engine.RegisterAction("ActionGetDeploymentSets", [this](auto c) {
374 if (req == nullptr) {
375 // no event or request
376 return;
377 }
378 req->SetReplyValue(
379 static_cast<BizLogicIf&>(this->m_logic).ActionGetDeploymentSets());
380 });
381
382 engine.RegisterAction("ActionGetComponents", [this](auto c) {
384 if (req == nullptr) {
385 // no event or request
386 return;
387 }
388 req->SetReplyValue(static_cast<BizLogicIf&>(this->m_logic).ActionGetComponents());
389 });
390
391 engine.RegisterAction("ActionGetActiveComponents", [this](auto c) {
393 if (req == nullptr) {
394 // no event or request
395 return;
396 }
397 req->SetReplyValue(
398 static_cast<BizLogicIf&>(this->m_logic).ActionGetActiveComponents());
399 });
400
401 engine.RegisterAction("ActionGetServices", [this](auto c) {
403 if (req == nullptr) {
404 // no event or request
405 return;
406 }
407 req->SetReplyValue(static_cast<BizLogicIf&>(this->m_logic).ActionGetServices());
408 });
409
410 engine.RegisterAction("ActionGetActiveServices", [this](auto c) {
412 if (req == nullptr) {
413 // no event or request
414 return;
415 }
416 req->SetReplyValue(
417 static_cast<BizLogicIf&>(this->m_logic).ActionGetActiveServices());
418 });
419 // Activities #####################################################################
420
421 m_engine.RegisterActivity(
422 "ActivityStarting",
423 [this](StopToken stop_token) { m_logic.ActivityStarting(stop_token); },
426 }
427
428 virtual ~OutputStage() = default;
429
430 protected:
433 std::optional<rad::cii::Request<std::string>> m_tmp_undeploy_request;
434 std::optional<rad::cii::Request<std::string, std::string>> m_tmp_deploy_request;
435
436 std::shared_ptr<rad::cii::Request<std::string, void>> m_tmp_request;
437 std::function<void()> m_custom_success_handler;
438 std::function<void(std::exception_ptr)> m_custom_error_handler;
439 };
440
442 public:
444 // clang-format off
445 mm.AddState(Initial, "Initial");
446 mm.AddState(Composite, "On");
447 mm.AddState(Final, "Off");
448 mm.AddState(Initial, "On::Initial", "On");
449 mm.AddState(Simple, "On::Starting", "On", "ActivityStarting", "ActionStartingEntry");
450 mm.AddState(Simple, "On::NotDeployed", "On");
451 mm.AddState(Simple, "On::Deployed", "On");
452 mm.AddState(Simple, "On::Deploying", "On", "ActivityDeploying", "ActionDeployEntry");
453 mm.AddState(Simple, "On::Undeploying", "On", "ActivityUndeploying", "ActionUndeployEntry");
454
455 mm.AddTrans("Initial", "On");
456 mm.AddTrans("On", "Off", "deployment_daemon_events.Exit", "", "ActionExit");
457 mm.AddTrans("On", "On", "deployment_daemon_events.Reset");
458 mm.AddTrans("On", "", "deployment_daemon_events.GetState", "", "ActionGetState");
459 mm.AddTrans("On", "", "deployment_daemon_events.SetLogLevel", "", "ActionSetLogLevel");
460
461 mm.AddTrans("On::Initial", "On::Starting");
462 mm.AddTrans("On::Starting", "On::NotDeployed", "deployment_daemon_events.Done", "", "ActionStartingDone");
463 mm.AddTrans("On::NotDeployed", "On::Deploying", "deployment_daemon_events.Deploy", "", "");
464 mm.AddTrans("On::Deploying", "On::Deployed", "deployment_daemon_events.Done", "", "ActionDeployDone");
465 mm.AddTrans("On::Deploying", "On::NotDeployed", "deployment_daemon_events.Error", "", "ActionDeployFailed");
466 mm.AddTrans("On::Deployed", "On::Undeploying", "deployment_daemon_events.Undeploy", "", "");
467 mm.AddTrans("On::Undeploying", "On::NotDeployed", "deployment_daemon_events.Done", "", "ActionUndeployDone");
468 mm.AddTrans("On::Undeploying", "On::NotDeployed", "deployment_daemon_events.Error", "", "ActionUndeployFailed");
469
470 mm.AddTrans("On", "", "deployment_daemon_events.GetDeploymentSets", "", "ActionGetDeploymentSets");
471 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetActiveDeployment", "", "ActionGetActiveDeployment");
472 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetDescription", "", "ActionGetDescription");
473 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetComponents", "", "ActionGetComponents");
474 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetActiveComponents", "", "ActionGetActiveComponents");
475 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.StartComponent", "", "ActionStartComponent");
476 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.StopComponent", "", "ActionStopComponent");
477 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetServices", "", "ActionGetServices");
478 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetActiveServices", "", "ActionGetActiveServices");
479 // clang-format on
480 }
481 };
482};
483
484} // namespace rtctk::deploymentDaemon
485
486#endif // RTCTK_REUSABLECOMPONENT_DEPLOYMENT_DAEMON_LIFE_CYCLE_HPP
Class that handles reception of commands using MAL.
Definition commandReplier.hpp:29
ModelBuilderBase(StateMachineEngine &engine)
Definition modelBuilderBase.hpp:36
ModelManipulator mm
Definition modelBuilderBase.hpp:78
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:168
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:186
Definition stateMachineEngine.hpp:34
void RegisterAction(const std::string &id, ActionMethod action)
Register action.
Definition stateMachineEngine.cpp:85
void RegisterRejectHandler(const std::string &id, RejectMethod reject)
Register reject handler.
Definition stateMachineEngine.cpp:128
void RegisterActivity(const std::string &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition stateMachineEngine.cpp:120
void RegisterActionStatic(const std::string &id, std::function< void(const Event &)> action)
Register action for statically known event type.
Definition stateMachineEngine.hpp:92
Thrown if the command is not allowed because there is already deployment (= in Deployment State)
Definition deploymentDaemonLifeCycle.hpp:75
static constexpr int32_t ERROR_CODE
Definition deploymentDaemonLifeCycle.hpp:77
DeploymentAlreadyExists(const std::string &request_id, const std::string &state_id)
Definition deploymentDaemonLifeCycle.hpp:78
static void Register(cfw::CommandReplier &replier, cfw::StateMachineEngine &engine)
Definition deploymentDaemonCmdsImpl.hpp:37
Definition deploymentDaemonLifeCycle.hpp:122
virtual void ActivityDeploying(cfw::StopToken st, std::string args)=0
virtual std::vector< std::string > ActionGetDeploymentSets()=0
virtual std::vector< std::string > ActionGetActiveServices()=0
virtual std::vector< std::string > ActionGetServices()=0
virtual std::vector< std::string > ActionGetActiveComponents()=0
virtual std::vector< std::string > ActionGetComponents()=0
CommandReplier & m_replier
Definition deploymentDaemonLifeCycle.hpp:160
InputStage(CommandReplier &replier, StateMachineEngine &engine)
Definition deploymentDaemonLifeCycle.hpp:146
virtual void Start()
Definition deploymentDaemonLifeCycle.hpp:152
StateMachineEngine & m_engine
Definition deploymentDaemonLifeCycle.hpp:161
ModelBuilder(StateMachineEngine &engine)
Definition deploymentDaemonLifeCycle.hpp:443
std::function< void()> m_custom_success_handler
Definition deploymentDaemonLifeCycle.hpp:437
std::optional< rad::cii::Request< std::string > > m_tmp_undeploy_request
Definition deploymentDaemonLifeCycle.hpp:433
std::optional< rad::cii::Request< std::string, std::string > > m_tmp_deploy_request
Definition deploymentDaemonLifeCycle.hpp:434
std::shared_ptr< rad::cii::Request< std::string, void > > m_tmp_request
Definition deploymentDaemonLifeCycle.hpp:436
std::function< void(std::exception_ptr)> m_custom_error_handler
Definition deploymentDaemonLifeCycle.hpp:438
StateMachineEngine & m_engine
Definition deploymentDaemonLifeCycle.hpp:431
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition deploymentDaemonLifeCycle.hpp:166
BizLogicIf & m_logic
Definition deploymentDaemonLifeCycle.hpp:432
static constexpr int32_t ERROR_CODE
Definition deploymentDaemonLifeCycle.hpp:49
DeploymentDaemonRequestAborted()
Definition deploymentDaemonLifeCycle.hpp:50
Thrown if the command was accepted but the task to run failed.
Definition deploymentDaemonLifeCycle.hpp:105
static constexpr int32_t ERROR_CODE
Definition deploymentDaemonLifeCycle.hpp:110
DeploymentDaemonRequestFailed(const std::string &message)
Definition deploymentDaemonLifeCycle.hpp:111
Thrown if the command is not allowed in current state.
Definition deploymentDaemonLifeCycle.hpp:89
static constexpr int32_t ERROR_CODE
Definition deploymentDaemonLifeCycle.hpp:94
DeploymentDaemonRequestRejected(const std::string &request_id, const std::string &state_id)
Definition deploymentDaemonLifeCycle.hpp:95
Thrown if the command is not allowed because there is no deployment (=not in Deployment State)
Definition deploymentDaemonLifeCycle.hpp:60
NoDeployment(const std::string &request_id, const std::string &state_id)
Definition deploymentDaemonLifeCycle.hpp:63
static constexpr int32_t ERROR_CODE
Definition deploymentDaemonLifeCycle.hpp:62
Receive commands via MAL.
Implementation of MAL commands for layer 'DeploymentDaemonComponent'.
Provides macros and utilities for exception handling.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition logger.cpp:191
Logging Support Library based on log4cplus.
Base class of the ModelBuilder.
@ Simple
Definition model.hpp:22
@ Composite
Definition model.hpp:22
@ Final
Definition model.hpp:22
@ Initial
Definition model.hpp:22
rad::StopToken StopToken
Definition stopToken.hpp:19
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition stateMachineEngine.hpp:255
const std::string STD_OK_REPLY
Definition stdComponent.hpp:85
Definition deploymentDaemonBusinessLogic.cpp:34
A container that can hold any type of service.
Wrapper around the SCXML State Machine Engine.
Life cycle for DeploymentDaemonComponent.
Definition deploymentDaemonLifeCycle.hpp:121