Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 602 lines (486 sloc) 18.188 kb
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-

// Copyright (c) 2001-2011 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2, June
// 1991 as published by the Free Software Foundation. Redistribution
// and/or modification of this program under the terms of any other
// version of the GNU General Public License is not permitted.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU General Public License, Version 2, a copy of which can be
// found in the XORP LICENSE.gpl file.
//
// XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net


#ifndef __RTRMGR_TASK_HH__
#define __RTRMGR_TASK_HH__




#include "libxorp/bug_catcher.hh"
#include "libxorp/run_command.hh"
#include "libxorp/status_codes.h"

#include "libxipc/xrl_router.hh"

#include "unexpanded_program.hh"
#include "unexpanded_xrl.hh"


class MasterConfigTree;
class ModuleCommand;
class ModuleManager;
class Task;
class TaskManager;
class XorpClient;

class Validation {
public:
    typedef XorpCallback1<void, bool>::RefPtr CallBack;

    Validation(const string& module_name, bool verbose)
: _module_name(module_name), _verbose(verbose) {};
    virtual ~Validation() {};

    virtual void validate(RunShellCommand::ExecId exec_id, CallBack cb) = 0;

protected:
    const string _module_name;
    RunShellCommand::ExecId _exec_id;
    bool _verbose; // Set to true if output is verbose
};

class DelayValidation : public Validation {
public:
    DelayValidation(const string& module_name, EventLoop& eventloop,
uint32_t ms, bool verbose);

    void validate(RunShellCommand::ExecId exec_id, CallBack cb);

private:
    void timer_expired();

    EventLoop& _eventloop;
    CallBack _cb;
    uint32_t _delay_in_ms;
    XorpTimer _timer;
};

class XrlStatusValidation : public Validation {
public:
    XrlStatusValidation(const string& module_name, const XrlAction& xrl_action,
TaskManager& taskmgr);
    virtual ~XrlStatusValidation() {
eventloop().remove_timer(_retry_timer);
// TODO: Should remove callbacks pointing to this as well,
// but that is a whole other problem...
    }

    void validate(RunShellCommand::ExecId exec_id, CallBack cb);

protected:
    void dummy_response();
    virtual void xrl_done(const XrlError& e, XrlArgs* xrl_args);
    virtual void handle_status_response(ProcessStatus status,
const string& reason) = 0;
    EventLoop& eventloop();

    const XrlAction& _xrl_action;
    TaskManager& _task_manager;
    CallBack _cb;
    XorpTimer _retry_timer;
    uint32_t _retries;
};

class ProgramStatusValidation : public Validation {
public:
    ProgramStatusValidation(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);
    virtual ~ProgramStatusValidation();

    void validate(RunShellCommand::ExecId exec_id, CallBack cb);

protected:
    virtual void handle_status_response(bool success,
const string& stdout_output,
const string& stderr_output) = 0;
    EventLoop& eventloop();

    const ProgramAction& _program_action;
    TaskManager& _task_manager;
    CallBack _cb;

private:
    void stdout_cb(RunShellCommand* run_command, const string& output);
    void stderr_cb(RunShellCommand* run_command, const string& output);
    void done_cb(RunShellCommand* run_command, bool success,
const string& error_msg);
    void execute_done(bool success);

    RunShellCommand* _run_command;
    string _command_stdout;
    string _command_stderr;
    XorpTimer _delay_timer;
};

class XrlStatusStartupValidation : public XrlStatusValidation {
public:
    XrlStatusStartupValidation(const string& module_name,
const XrlAction& xrl_action,
TaskManager& taskmgr);

private:
    void handle_status_response(ProcessStatus status, const string& reason);
};

class ProgramStatusStartupValidation : public ProgramStatusValidation {
public:
    ProgramStatusStartupValidation(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);

private:
    void handle_status_response(bool success,
const string& stdout_output,
const string& stderr_output);
};

class XrlStatusReadyValidation : public XrlStatusValidation {
public:
    XrlStatusReadyValidation(const string& module_name,
const XrlAction& xrl_action,
TaskManager& taskmgr);

private:
    void handle_status_response(ProcessStatus status, const string& reason);
};

class ProgramStatusReadyValidation : public ProgramStatusValidation {
public:
    ProgramStatusReadyValidation(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);

private:
    void handle_status_response(bool success,
const string& stdout_output,
const string& stderr_output);
};

class XrlStatusConfigMeValidation : public XrlStatusValidation {
public:
    XrlStatusConfigMeValidation(const string& module_name,
const XrlAction& xrl_action,
TaskManager& taskmgr);

private:
    void handle_status_response(ProcessStatus status, const string& reason);
};

class ProgramStatusConfigMeValidation : public ProgramStatusValidation {
public:
    ProgramStatusConfigMeValidation(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);

private:
    void handle_status_response(bool success,
const string& stdout_output,
const string& stderr_output);
};

class XrlStatusShutdownValidation : public XrlStatusValidation {
public:
    XrlStatusShutdownValidation(const string& module_name,
const XrlAction& xrl_action,
TaskManager& taskmgr);

private:
    void xrl_done(const XrlError& e, XrlArgs* xrl_args);
    void handle_status_response(ProcessStatus status, const string& reason);
};

class ProgramStatusShutdownValidation : public ProgramStatusValidation {
public:
    ProgramStatusShutdownValidation(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);

private:
    void handle_status_response(bool success,
const string& stdout_output,
const string& stderr_output);
};

class Startup {
public:
    typedef XorpCallback1<void, bool>::RefPtr CallBack;
    Startup(const string& module_name, bool verbose);
    virtual ~Startup() {}

    virtual void startup(const RunShellCommand::ExecId& exec_id,
CallBack cb) = 0;

protected:
    const string _module_name;
    bool _verbose; // Set to true if output is verbose
};

class XrlStartup : public Startup {
public:
    XrlStartup(const string& module_name, const XrlAction& xrl_action,
TaskManager& taskmgr);
    virtual ~XrlStartup() {}

    void startup(const RunShellCommand::ExecId& exec_id, CallBack cb);
    EventLoop& eventloop() const;

private:
    void dummy_response();
    void startup_done(const XrlError& err, XrlArgs* xrl_args);

    const XrlAction& _xrl_action;
    TaskManager& _task_manager;
    CallBack _cb;
    XorpTimer _dummy_timer;
};

class ProgramStartup : public Startup {
public:
    ProgramStartup(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);
    virtual ~ProgramStartup();

    void startup(const RunShellCommand::ExecId& exec_id, CallBack cb);
    EventLoop& eventloop() const;

private:
    void stdout_cb(RunShellCommand* run_command, const string& output);
    void stderr_cb(RunShellCommand* run_command, const string& output);
    void done_cb(RunShellCommand* run_command, bool success,
const string& error_msg);
    void execute_done(bool success);

    const ProgramAction& _program_action;
    TaskManager& _task_manager;
    CallBack _cb;

    RunShellCommand* _run_command;
    string _command_stdout;
    string _command_stderr;
    XorpTimer _delay_timer;
};

class Shutdown {
public:
    typedef XorpCallback1<void, bool>::RefPtr CallBack;
    Shutdown(const string& module_name, bool verbose);
    virtual ~Shutdown() {}

    virtual void shutdown(const RunShellCommand::ExecId& exec_id,
CallBack cb) = 0;

protected:
    const string _module_name;
    bool _verbose; // Set to true if output is verbose
};

class XrlShutdown : public Shutdown {
public:
    XrlShutdown(const string& module_name, const XrlAction& xrl_action,
TaskManager& taskmgr);
    virtual ~XrlShutdown() {}

    void shutdown(const RunShellCommand::ExecId& exec_id, CallBack cb);
    EventLoop& eventloop() const;

private:
    void dummy_response();
    void shutdown_done(const XrlError& err, XrlArgs* xrl_args);

    const XrlAction& _xrl_action;
    TaskManager& _task_manager;
    CallBack _cb;
    XorpTimer _dummy_timer;
};

class ProgramShutdown : public Shutdown {
public:
    ProgramShutdown(const string& module_name,
const ProgramAction& program_action,
TaskManager& taskmgr);
    virtual ~ProgramShutdown();

    void shutdown(const RunShellCommand::ExecId& exec_id, CallBack cb);
    EventLoop& eventloop() const;

private:
    void stdout_cb(RunShellCommand* run_command, const string& output);
    void stderr_cb(RunShellCommand* run_command, const string& output);
    void done_cb(RunShellCommand* run_command, bool success,
const string& error_msg);
    void execute_done(bool success);

    const ProgramAction& _program_action;
    TaskManager& _task_manager;
    CallBack _cb;

    RunShellCommand* _run_command;
    string _command_stdout;
    string _command_stderr;
    XorpTimer _delay_timer;
};

class TaskBaseItem {
public:
    TaskBaseItem(Task& task) : _task(task) {}
    TaskBaseItem(const TaskBaseItem& them) : _task(them._task) {}
    virtual ~TaskBaseItem() {}

    virtual bool execute(string& errmsg) = 0;
    virtual void unschedule() = 0;

    Task& task() { return (_task); }

private:
    Task& _task;
};

class TaskXrlItem : public TaskBaseItem {
public:
    TaskXrlItem(const UnexpandedXrl& uxrl, const XrlRouter::XrlCallback& cb,
Task& task,
uint32_t xrl_resend_count = TaskXrlItem::DEFAULT_RESEND_COUNT,
int xrl_resend_delay_ms = TaskXrlItem::DEFAULT_RESEND_DELAY_MS);
    TaskXrlItem(const TaskXrlItem& them);

    bool execute(string& errmsg);
    void execute_done(const XrlError& err, XrlArgs* xrl_args);
    void resend();
    void unschedule();

private:
    static const uint32_t DEFAULT_RESEND_COUNT;
    static const int DEFAULT_RESEND_DELAY_MS;

    UnexpandedXrl _unexpanded_xrl;
    XrlRouter::XrlCallback _xrl_callback;
    uint32_t _xrl_resend_count_limit;
    uint32_t _xrl_resend_count;
    int _xrl_resend_delay_ms;
    XorpTimer _xrl_resend_timer;
    bool _verbose; // Set to true if output is verbose
};

class TaskProgramItem : public TaskBaseItem {
public:
    typedef XorpCallback4<void, bool, const string&, const string&, bool>::RefPtr ProgramCallback;

    TaskProgramItem(const UnexpandedProgram& program,
TaskProgramItem::ProgramCallback program_cb,
Task& task);
    TaskProgramItem(const TaskProgramItem& them);
    ~TaskProgramItem();

    bool execute(string& errmsg);
    void execute_done(bool success);
    void unschedule();

private:
    void stdout_cb(RunShellCommand* run_command, const string& output);
    void stderr_cb(RunShellCommand* run_command, const string& output);
    void done_cb(RunShellCommand* run_command, bool success,
const string& error_msg);

    UnexpandedProgram _unexpanded_program;
    RunShellCommand* _run_command;
    string _command_stdout;
    string _command_stderr;
    TaskProgramItem::ProgramCallback _program_cb;
    XorpTimer _delay_timer;
    bool _verbose; // Set to true if output is verbose
};

class Task {
public:
    typedef XorpCallback2<void, bool, const string&>::RefPtr CallBack;

    Task(const string& name, TaskManager& taskmgr);
    ~Task();

    void start_module(const string& mod_name, Validation* startup_validation,
Validation* config_validation, Startup* startup);
    void shutdown_module(const string& mod_name, Validation* validation,
Shutdown* shutdown);
    void add_xrl(const UnexpandedXrl& xrl, XrlRouter::XrlCallback& cb);
    void add_program(const UnexpandedProgram& program,
TaskProgramItem::ProgramCallback program_cb);
    void set_ready_validation(Validation* validation);
    Validation* ready_validation() const { return _ready_validation; }
    bool will_shutdown_module() const { return _stop_module; }
    void run(CallBack cb);
    void item_done(bool success, bool fatal, const string& errmsg);
    bool do_exec() const;
    bool is_verification() const;
    XorpClient& xorp_client() const;

    /**
* Get a reference to the ExecId object.
*
* @return a reference to the ExecId object that is used
* for setting the execution ID when running the task.
*/
    const RunShellCommand::ExecId& exec_id() const { return _exec_id; }

    /**
* Set the execution ID for executing the task.
*
* @param v the execution ID.
*/
    void set_exec_id(const RunShellCommand::ExecId& v) { _exec_id = v; }

    const string& name() const { return _name; }
    EventLoop& eventloop() const;

    bool verbose() const { return _verbose; }

protected:
    void step1_start();
    void step1_done(bool success);

    void step2_wait();
    void step2_done(bool success);

    void step2_2_wait();
    void step2_2_done(bool success);

    void step2_3_wait();
    void step2_3_done(bool success);

    void step3_config();
    void step3_done(bool success);

    void step4_wait();
    void step4_done(bool success);

    void step5_stop();
    void step5_done(bool success);

    void step6_wait();
    void step6_done(bool success);

    void step7_wait();
    void step7_kill();

    void step8_report();
    void task_fail(const string& errmsg, bool fatal);

private:
    string _name; // The name of the task
    TaskManager& _taskmgr;
    string _module_name; // The name of the module to start and stop
    bool _start_module;
    bool _stop_module;
    Validation* _startup_validation; // The validation mechanism for the
                                     // module startup
    Validation* _config_validation; // The validation mechanism for the
// module configuration
    Validation* _ready_validation; // The validation mechanism for the module
                                   // reconfiguration
    Validation* _shutdown_validation; // The validation mechanism for the
                                       // module shutdown
    Startup* _startup_method;
    Shutdown* _shutdown_method;
    list<TaskBaseItem *> _task_items;
    bool _config_done; // True if we changed the module's config
    CallBack _task_complete_cb; // The task completion callback
    XorpTimer _wait_timer;
    RunShellCommand::ExecId _exec_id;
    bool _verbose; // Set to true if output is verbose
};

class TaskManager : public BugCatcher {
    typedef XorpCallback2<void, bool, string>::RefPtr CallBack;

public:
    TaskManager(MasterConfigTree& config_tree,
ModuleManager& mmgr,
XorpClient& xclient, bool global_do_exec,
bool verbose);
    ~TaskManager();

    void set_do_exec(bool do_exec, bool is_verification);
    void reset();
    int add_module(const ModuleCommand& mod_cmd, string& error_msg);
    void add_xrl(const string& module_name, const UnexpandedXrl& xrl,
XrlRouter::XrlCallback& cb);
    void add_program(const string& module_name,
const UnexpandedProgram& program,
TaskProgramItem::ProgramCallback program_cb);
    void shutdown_module(const string& module_name);
    void run(CallBack cb);
    XorpClient& xorp_client() const { return _xorp_client; }
    ModuleManager& module_manager() const { return _module_manager; }
    MasterConfigTree& config_tree() const { return _config_tree; }
    bool do_exec() const { return _current_do_exec; }
    bool is_verification() const { return _is_verification; }
    bool verbose() const { return _verbose; }
    EventLoop& eventloop() const;

    /**
* @short kill_process is used to kill a fatally wounded process
*
* kill_process is used to kill a fatally wounded process. This
* does not politely ask the process to die, because if we get
* here we can't communicate with the process using XRLs or any other
* mechanism, so we just kill it outright.
*
* @param module_name the module name of the process to be killed.
*/
    void kill_process(const string& module_name);

    /**
* Get a reference to the ExecId object.
*
* @return a reference to the ExecId object that is used
* for setting the execution ID when running the tasks.
*/
    const RunShellCommand::ExecId& exec_id() const { return _exec_id; }

    /**
* Set the execution ID for executing the tasks.
*
* @param v the execution ID.
*/
    void set_exec_id(const RunShellCommand::ExecId& v) { _exec_id = v; }


private:
    void reorder_tasks();
    void run_task();
    void task_done(bool success, const string& errmsg);
    void fail_tasklist_initialization(const string& errmsg);
    Task& find_task(const string& module_name);
    void null_callback();

    MasterConfigTree& _config_tree;
    ModuleManager& _module_manager;
    XorpClient& _xorp_client;
    bool _global_do_exec; // Set to false if we're never going
// to execute anything because we're
// in a debug mode
    bool _current_do_exec;
    bool _is_verification; // Set to true if current execution
// is for verification purpose
    bool _verbose; // Set to true if output is verbose

    // _tasks provides fast access to a Task by name
    map<string, Task*> _tasks;

    // _tasklist maintains the execution order
    list<Task*> _tasklist;

    // _shutdown_order maintains the shutdown ordering
    list<Task*> _shutdown_order;

    map<string, const ModuleCommand*> _module_commands;

    RunShellCommand::ExecId _exec_id;

    CallBack _completion_cb;
};

#endif // __RTRMGR_TASK_HH__
Something went wrong with that request. Please try again.