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