RTC Toolkit 4.0.2
Loading...
Searching...
No Matches
deploymentDaemonLifeCycle.hpp
Go to the documentation of this file.
1
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(std::string const& request_id, std::string const& state_id)
65 : rtctkif::DeploymentException(
66 "No active deployment. (Request " + request_id + " 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(std::string const& request_id, std::string const& 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(std::string const& request_id, std::string const& state_id)
97 : rtctkif::DeploymentException("Request " + request_id + " rejected in state " + state_id,
98 ERROR_CODE) {
99 }
100};
101
106class DeploymentDaemonRequestFailed : public rtctkif::DeploymentException {
107public:
111 static constexpr int32_t ERROR_CODE = 501;
112 explicit DeploymentDaemonRequestFailed(std::string const& message)
113 : rtctkif::DeploymentException(message, ERROR_CODE) {
114 }
115};
116
124 public:
125 virtual void ActivityDeploying(cfw::StopToken st, std::string args) = 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
143 public:
145 : m_replier(replier), m_engine(engine) {
146 }
147
148 virtual ~InputStage() = default;
149
150 virtual void Start() {
151 // exit event is used by SIGTERM, SIGINT and SIGHUP
152 m_engine.RegisterExitEvent(std::make_unique<deployment_daemon_events::Exit>());
153 // deployment daemon MAL commands
155 }
156
157 protected:
160 };
161
163 public:
165 // Handlers ###################################################################
166
167 // clang-format off
168 engine.RegisterRejectHandler<deployment_daemon_events::Reset, DeploymentDaemonRequestRejected>();
169 engine.RegisterRejectHandler<deployment_daemon_events::GetState, DeploymentDaemonRequestRejected>();
170 engine.RegisterRejectHandler<deployment_daemon_events::Exit, DeploymentDaemonRequestRejected>();
171
172 engine.RegisterRejectHandler<deployment_daemon_events::Deploy, DeploymentAlreadyExists>();
173 engine.RegisterRejectHandler<deployment_daemon_events::Undeploy, NoDeployment>();
174 engine.RegisterRejectHandler<deployment_daemon_events::GetActiveDeployment, NoDeployment>();
175 engine.RegisterRejectHandler<deployment_daemon_events::GetDescription, NoDeployment>();
176 engine.RegisterRejectHandler<deployment_daemon_events::GetDeploymentSets, NoDeployment>();
177 engine.RegisterRejectHandler<deployment_daemon_events::GetComponents, NoDeployment>();
178 engine.RegisterRejectHandler<deployment_daemon_events::GetActiveComponents, NoDeployment>();
179
180 engine.RegisterRejectHandler<deployment_daemon_events::StartComponent, NoDeployment>();
181 engine.RegisterRejectHandler<deployment_daemon_events::StopComponent, NoDeployment>();
182 // clang-format on
183
184 m_custom_success_handler = [this] {
185 m_engine.PostEvent(std::make_unique<deployment_daemon_events::Done>());
186 };
187
188 m_custom_error_handler = [this](const std::exception_ptr& eptr) {
189 m_engine.PostEvent(std::make_unique<deployment_daemon_events::Error>(eptr));
190 };
191
192 engine.RegisterActionStatic<deployment_daemon_events::Deploy>(
193 "ActionDeployEntry", [this](auto const& ev) {
194 assert(not m_tmp_deploy_request);
195 m_tmp_deploy_request = ev.GetPayload();
196 });
197
198 engine.RegisterActionStatic<deployment_daemon_events::Done>(
199 "ActionDeployDone", [this](auto const&) {
200 assert(m_tmp_deploy_request);
201 m_tmp_deploy_request->SetReplyValue(STD_OK_REPLY);
202 m_tmp_deploy_request.reset();
203 });
204 engine.RegisterActionStatic<deployment_daemon_events::Error>(
205 "ActionDeployFailed", [this](auto const& ev) {
206 auto const& eptr = ev.GetPayload();
207 assert(m_tmp_deploy_request);
208 m_tmp_deploy_request->SetException(
210 m_tmp_deploy_request.reset();
211 });
212
213 engine.RegisterActionStatic<deployment_daemon_events::Undeploy>(
214 "ActionUndeployEntry", [this](auto const& ev) {
215 assert(not m_tmp_undeploy_request);
216 m_tmp_undeploy_request = ev.GetPayload();
217 });
218
219 engine.RegisterActionStatic<deployment_daemon_events::Done>(
220 "ActionUndeployDone", [this](auto const&) {
222 m_tmp_undeploy_request->SetReplyValue(STD_OK_REPLY);
224 });
225
226 engine.RegisterActionStatic<deployment_daemon_events::Error>(
227 "ActionUndeployFailed", [this](auto const& ev) {
228 auto const& eptr = ev.GetPayload();
230 m_tmp_undeploy_request->SetException(
233 });
234
235 // Activities #####################################################################
236
237 engine.RegisterActivity(
238 "ActivityDeploying",
239 [this](StopToken stop_token) {
240 std::string arg = m_tmp_deploy_request->GetRequestPayload();
241 // JsonPayload arg = JsonPayload::parse(args);
242 static_cast<BizLogicIf&>(this->m_logic).ActivityDeploying(stop_token, arg);
243 },
246
247 engine.RegisterActivity(
248 "ActivityUndeploying",
249 [this](StopToken stop_token) {
250 static_cast<BizLogicIf&>(this->m_logic).ActivityUndeploying(stop_token);
251 },
254
255 // Reset ######################################################################
256
257 engine.RegisterAction("ActionStartingEntry", [this](auto c) {
258 m_tmp_request = GetPayloadNothrow<deployment_daemon_events::Reset>(c);
259 });
260
261 engine.RegisterAction("ActionStartingDone", [this](auto c) {
262 if (m_tmp_request) {
263 m_tmp_request->SetReplyValue(STD_OK_REPLY);
264 m_tmp_request = nullptr;
265 }
266 });
267
268 // Exit #####################################################################
269
270 engine.RegisterAction("ActionExit", [this](auto c) {
271 auto req = GetPayloadNothrow<deployment_daemon_events::Exit>(c);
272 if (req == nullptr) {
273 // no event or request
274 return;
275 }
276 req->SetReplyValue(STD_OK_REPLY);
277 m_engine.Stop();
278 });
279
280 // GetState #####################################################################
281
282 engine.RegisterAction("ActionGetState", [this](auto c) {
283 auto req = GetPayloadNothrow<deployment_daemon_events::GetState>(c);
284 if (req == nullptr) {
285 // no event or request
286 return;
287 }
288 req->SetReplyValue(m_engine.GetState());
289 });
290
291 // SetLogLevel #####################################################################
292
293 engine.RegisterAction("ActionSetLogLevel", [this](auto c) {
294 auto req = GetPayloadNothrow<deployment_daemon_events::SetLogLevel>(c);
295 if (req == nullptr) {
296 // no event or request
297 return;
298 }
299 auto loginfo = req->GetRequestPayload();
300 auto selected_logger = loginfo->getLogger();
301 auto& logger = GetLogger(selected_logger);
302 log4cplus::LogLevelManager llm;
303 auto selected_ll = llm.fromString(loginfo->getLevel());
304 if (selected_ll == log4cplus::NOT_SET_LOG_LEVEL) {
305 req->SetException(DeploymentDaemonRequestFailed("Invalid LogLevel specified"));
306 } else {
307 logger.setLogLevel(selected_ll);
308 LOG4CPLUS_INFO(GetLogger("rtctk"),
309 "Log Level of logger '" << loginfo->getLogger() << "' set to '"
310 << llm.toString(logger.getLogLevel())
311 << "'");
312 req->SetReplyValue(STD_OK_REPLY);
313 }
314 });
315
316 engine.RegisterAction("ActionGetActiveDeployment", [this](auto c) {
317 auto req = GetPayloadNothrow<deployment_daemon_events::GetActiveDeployment>(c);
318 if (req == nullptr) {
319 // no event or request
320 return;
321 }
322 req->SetReplyValue(
323 static_cast<BizLogicIf&>(this->m_logic).ActionGetActiveDeployment());
324 });
325
326 engine.RegisterAction("ActionGetDescription", [this](auto c) {
327 auto req = GetPayloadNothrow<deployment_daemon_events::GetDescription>(c);
328 if (req == nullptr) {
329 // no event or request
330 return;
331 }
332 req->SetReplyValue(static_cast<BizLogicIf&>(this->m_logic).ActionGetDescription());
333 });
334
335 engine.RegisterAction("ActionStartComponent", [this](auto c) {
336 auto req = GetPayloadNothrow<deployment_daemon_events::StartComponent>(c);
337 if (req == nullptr) {
338 // no event or request
339 return;
340 }
341 std::string arg = req->GetRequestPayload();
342 try {
343 static_cast<BizLogicIf&>(this->m_logic).ActionStartComponent(arg);
344 req->SetReplyValue(STD_OK_REPLY);
345 } catch (const std::exception& eptr) {
346 req->SetException(rtctkif::DeploymentException(
347 "ActionStartComponent failed: " + std::string(eptr.what()), 601));
348 }
349 });
350
351 engine.RegisterAction("ActionStopComponent", [this](auto c) {
352 auto req = GetPayloadNothrow<deployment_daemon_events::StopComponent>(c);
353 if (req == nullptr) {
354 // no event or request
355 return;
356 }
357 std::string arg = req->GetRequestPayload();
358 try {
359 static_cast<BizLogicIf&>(this->m_logic).ActionStopComponent(arg);
360 req->SetReplyValue(STD_OK_REPLY);
361 } catch (const std::exception& eptr) {
362 req->SetException(rtctkif::DeploymentException(
363 "ActionStopComponent failed: " + std::string(eptr.what()), 602));
364 }
365 });
366
367 engine.RegisterAction("ActionGetDeploymentSets", [this](auto c) {
368 auto req = GetPayloadNothrow<deployment_daemon_events::GetDeploymentSets>(c);
369 if (req == nullptr) {
370 // no event or request
371 return;
372 }
373 req->SetReplyValue(
374 static_cast<BizLogicIf&>(this->m_logic).ActionGetDeploymentSets());
375 });
376
377 engine.RegisterAction("ActionGetComponents", [this](auto c) {
378 auto req = GetPayloadNothrow<deployment_daemon_events::GetComponents>(c);
379 if (req == nullptr) {
380 // no event or request
381 return;
382 }
383 req->SetReplyValue(static_cast<BizLogicIf&>(this->m_logic).ActionGetComponents());
384 });
385
386 engine.RegisterAction("ActionGetActiveComponents", [this](auto c) {
387 auto req = GetPayloadNothrow<deployment_daemon_events::GetActiveComponents>(c);
388 if (req == nullptr) {
389 // no event or request
390 return;
391 }
392 req->SetReplyValue(
393 static_cast<BizLogicIf&>(this->m_logic).ActionGetActiveComponents());
394 });
395
396 // Activities #####################################################################
397
399 "ActivityStarting",
400 [this](StopToken stop_token) { m_logic.ActivityStarting(stop_token); },
403 }
404
405 virtual ~OutputStage() = default;
406
407 protected:
410 std::optional<rad::cii::Request<std::string>> m_tmp_undeploy_request;
411 std::optional<rad::cii::Request<std::string, std::string>> m_tmp_deploy_request;
412
413 std::shared_ptr<rad::cii::Request<std::string, void>> m_tmp_request;
414 std::function<void()> m_custom_success_handler;
415 std::function<void(std::exception_ptr)> m_custom_error_handler;
416 };
417
419 public:
421 // clang-format off
422 mm.AddState(Initial, "Initial");
423 mm.AddState(Composite, "On");
424 mm.AddState(Final, "Off");
425 mm.AddState(Initial, "On::Initial", "On");
426 mm.AddState(Simple, "On::Starting", "On", "ActivityStarting", "ActionStartingEntry");
427 mm.AddState(Simple, "On::NotDeployed", "On");
428 mm.AddState(Simple, "On::Deployed", "On");
429 mm.AddState(Simple, "On::Deploying", "On", "ActivityDeploying", "ActionDeployEntry");
430 mm.AddState(Simple, "On::Undeploying", "On", "ActivityUndeploying", "ActionUndeployEntry");
431
432 mm.AddTrans("Initial", "On");
433 mm.AddTrans("On", "Off", "deployment_daemon_events.Exit", "", "ActionExit");
434 mm.AddTrans("On", "On", "deployment_daemon_events.Reset");
435 mm.AddTrans("On", "", "deployment_daemon_events.GetState", "", "ActionGetState");
436 mm.AddTrans("On", "", "deployment_daemon_events.SetLogLevel", "", "ActionSetLogLevel");
437
438 mm.AddTrans("On::Initial", "On::Starting");
439 mm.AddTrans("On::Starting", "On::NotDeployed", "deployment_daemon_events.Done", "", "ActionStartingDone");
440 mm.AddTrans("On::NotDeployed", "On::Deploying", "deployment_daemon_events.Deploy", "", "");
441 mm.AddTrans("On::Deploying", "On::Deployed", "deployment_daemon_events.Done", "", "ActionDeployDone");
442 mm.AddTrans("On::Deploying", "On::NotDeployed", "deployment_daemon_events.Error", "", "ActionDeployFailed");
443 mm.AddTrans("On::Deployed", "On::Undeploying", "deployment_daemon_events.Undeploy", "", "");
444 mm.AddTrans("On::Undeploying", "On::NotDeployed", "deployment_daemon_events.Done", "", "ActionUndeployDone");
445 mm.AddTrans("On::Undeploying", "On::NotDeployed", "deployment_daemon_events.Error", "", "ActionUndeployFailed");
446
447 mm.AddTrans("On", "", "deployment_daemon_events.GetDeploymentSets", "", "ActionGetDeploymentSets");
448 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetActiveDeployment", "", "ActionGetActiveDeployment");
449 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetDescription", "", "ActionGetDescription");
450 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetComponents", "", "ActionGetComponents");
451 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.GetActiveComponents", "", "ActionGetActiveComponents");
452 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.StartComponent", "", "ActionStartComponent");
453 mm.AddTrans("On::Deployed", "", "deployment_daemon_events.StopComponent", "", "ActionStopComponent");
454 // clang-format on
455 }
456 };
457};
458
459} // namespace rtctk::deploymentDaemon
460
461#endif // RTCTK_REUSABLECOMPONENT_DEPLOYMENT_DAEMON_LIFE_CYCLE_HPP
Class that handles reception of commands using MAL.
Definition: commandReplier.hpp:29
Base class of the ModelBuilder.
Definition: modelBuilderBase.hpp:35
ModelManipulator mm
Definition: modelBuilderBase.hpp:79
void AddTrans(std::string const &source_id, std::string const &target_id, std::string const &event_id="", std::string const &guard_id="", std::string const &action_id="")
Adds a new transition.
Definition: modelManipulator.cpp:229
void AddState(StateType type, std::string const &id, std::string const &parent_id="", std::string const &activity_id="", std::string const &entry_action_id="", std::string const &exit_action_id="")
Adds a new state.
Definition: modelManipulator.cpp:30
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: exceptions.hpp:185
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: exceptions.hpp:203
Definition: stateMachineEngine.hpp:35
void RegisterRejectHandler(std::string const &id, RejectMethod reject)
Register reject handler.
Definition: stateMachineEngine.cpp:131
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:123
void RegisterExitEvent(rad::UniqueEvent exit_event)
Register an exit event.
Definition: stateMachineEngine.cpp:69
std::string GetState()
Queries the current state.
Definition: stateMachineEngine.cpp:153
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:86
void RegisterActionStatic(std::string const &id, std::function< void(Event const &)> action)
Register action for statically known event type.
Definition: stateMachineEngine.hpp:93
void Stop()
Stops execution of the state machine event loop.
Definition: stateMachineEngine.cpp:191
void PostEvent(rad::SharedEvent s)
Injects a new event into the state machine engine.
Definition: stateMachineEngine.cpp:148
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(std::string const &request_id, std::string const &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 > ActionGetActiveComponents()=0
virtual std::vector< std::string > ActionGetComponents()=0
Definition: deploymentDaemonLifeCycle.hpp:142
CommandReplier & m_replier
Definition: deploymentDaemonLifeCycle.hpp:158
InputStage(CommandReplier &replier, StateMachineEngine &engine)
Definition: deploymentDaemonLifeCycle.hpp:144
virtual void Start()
Definition: deploymentDaemonLifeCycle.hpp:150
StateMachineEngine & m_engine
Definition: deploymentDaemonLifeCycle.hpp:159
Definition: deploymentDaemonLifeCycle.hpp:418
ModelBuilder(StateMachineEngine &engine)
Definition: deploymentDaemonLifeCycle.hpp:420
Definition: deploymentDaemonLifeCycle.hpp:162
std::function< void()> m_custom_success_handler
Definition: deploymentDaemonLifeCycle.hpp:414
std::optional< rad::cii::Request< std::string > > m_tmp_undeploy_request
Definition: deploymentDaemonLifeCycle.hpp:410
std::optional< rad::cii::Request< std::string, std::string > > m_tmp_deploy_request
Definition: deploymentDaemonLifeCycle.hpp:411
std::shared_ptr< rad::cii::Request< std::string, void > > m_tmp_request
Definition: deploymentDaemonLifeCycle.hpp:413
std::function< void(std::exception_ptr)> m_custom_error_handler
Definition: deploymentDaemonLifeCycle.hpp:415
StateMachineEngine & m_engine
Definition: deploymentDaemonLifeCycle.hpp:408
OutputStage(StateMachineEngine &engine, BizLogicIf &bl)
Definition: deploymentDaemonLifeCycle.hpp:164
BizLogicIf & m_logic
Definition: deploymentDaemonLifeCycle.hpp:409
Thrown if somebody sent a stop or abort command.
Definition: deploymentDaemonLifeCycle.hpp:48
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
DeploymentDaemonRequestFailed(std::string const &message)
Definition: deploymentDaemonLifeCycle.hpp:112
static constexpr int32_t ERROR_CODE
Definition: deploymentDaemonLifeCycle.hpp:111
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(std::string const &request_id, std::string const &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
static constexpr int32_t ERROR_CODE
Definition: deploymentDaemonLifeCycle.hpp:63
NoDeployment(std::string const &request_id, std::string const &state_id)
Definition: deploymentDaemonLifeCycle.hpp:64
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.
Definition: commandReplier.cpp:22
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:180
rad::StopToken StopToken
Definition: stopToken.hpp:20
@ Simple
Definition: model.hpp:22
@ Composite
Definition: model.hpp:22
@ Final
Definition: model.hpp:22
@ Initial
Definition: model.hpp:22
Definition: deploymentDaemonBusinessLogic.cpp:30
A container that can hold any type of service.
Wrapper around the SCXML State Machine Engine.
Life cycle for DeploymentDaemonComponent.
Definition: deploymentDaemonLifeCycle.hpp:122