xorp

template_commands.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/rtrmgr/template_commands.hh,v 1.39 2008/10/02 21:58:25 bms Exp $
00021 
00022 #ifndef __RTRMGR_TEMPLATE_COMMANDS_HH__
00023 #define __RTRMGR_TEMPLATE_COMMANDS_HH__
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include "libxorp/callback.hh"
00031 #include "libxorp/run_command.hh"
00032 
00033 #include "template_base_command.hh"
00034 #include "master_conf_tree_node.hh"
00035 #include "rtrmgr_error.hh"
00036 
00037 
00038 class XorpClient;
00039 class XRLdb;
00040 
00041 class Action {
00042 public:
00043     Action(TemplateTreeNode& template_tree_node, const list<string>& action);
00044     virtual ~Action() {};
00045 
00046     enum ActionCharType { VAR, NON_VAR, QUOTE, ASSIGN };
00047 
00048     string str() const;
00049     TemplateTreeNode& template_tree_node() { return _template_tree_node; }
00050     const TemplateTreeNode& template_tree_node() const { return _template_tree_node; }
00051     virtual bool expand_action(string& error_msg);
00052     virtual bool check_referred_variables(string& error_msg) const;
00053 
00054 protected:
00055     list<string> _action;
00056     list<string> _split_cmd;
00057     list<string> _referred_variables;
00058     TemplateTreeNode& _template_tree_node;
00059 };
00060 
00061 class XrlAction : public Action {
00062 public:
00063     XrlAction(TemplateTreeNode& template_tree_node, const list<string>& action,
00064           const XRLdb* xrldb) throw (ParseError);
00065 
00066     virtual bool expand_action(string& error_msg);
00067     int execute(const MasterConfigTreeNode& ctn, TaskManager& task_manager,
00068         XrlRouter::XrlCallback cb) const;
00069 #if 0    
00070     template<class TreeNode> int expand_xrl_variables(const TreeNode& tn,
00071                               string& result,
00072                               string& error_msg) const;
00073 #endif
00074     template<class TreeNode> Xrl* expand_xrl_variables(const TreeNode& tn,
00075                                string& error_msg) const;
00076     const string& request() const { return _request; }
00077     const string& xrl_return_spec() const { return _response; }
00078     string related_module() const;
00079     string affected_module() const;
00080 
00081 private:
00082 #ifdef DEBUG_XRLDB
00083     bool check_xrl_is_valid(const list<string>& action,
00084                 const XRLdb* xrldb, string& error_msg);
00085 #endif
00086     template<class TreeNode> bool expand_vars(const TreeNode& tn,
00087                           const string& s, 
00088                           string& result) const;
00089 
00090     string      _module_name;
00091     list<string>    _split_request;
00092     string      _request;
00093     list<string>    _split_response;
00094     string      _response;
00095     const XRLdb*    _xrldb;
00096 };
00097 
00098 class ProgramAction : public Action {
00099 public:
00100     ProgramAction(TemplateTreeNode& template_tree_node,
00101           const list<string>& action) throw (ParseError);
00102 
00103     virtual bool expand_action(string& error_msg);
00104     int execute(const MasterConfigTreeNode& ctn,
00105         TaskManager&            task_manager,
00106         TaskProgramItem::ProgramCallback program_cb) const;
00107     template<class TreeNode> int expand_program_variables(const TreeNode& tn,
00108                               string& result,
00109                               string& error_msg) const;
00110     string related_module() const;
00111     string affected_module() const;
00112     const string& request() const { return _request; }
00113     const string& stdout_variable_name() const { return _stdout_variable_name; }
00114     const string& stderr_variable_name() const { return _stderr_variable_name; }
00115 
00116 private:
00117     bool check_program_is_valid(const list<string>& action, string& error_msg);
00118     void parse_program_response(const string& part) throw (ParseError);
00119 
00120     string      _module_name;
00121     list<string>    _split_request;
00122     string      _request;
00123     list<string>    _split_response;
00124     string      _response;
00125     string      _stdout_variable_name;
00126     string      _stderr_variable_name;
00127 };
00128 
00129 class Command : public BaseCommand {
00130 public:
00131     Command(TemplateTreeNode& template_tree_node, const string& cmd_name);
00132     virtual ~Command();
00133 
00134     void add_action(const list<string>& action, const XRLdb* xrldb)
00135     throw (ParseError);
00136     int execute(MasterConfigTreeNode& ctn, TaskManager& task_manager) const;
00137     void xrl_action_complete(const XrlError& err,
00138                  XrlArgs* xrl_args,
00139                  MasterConfigTreeNode* ctn,
00140                  Action* action) const;
00141     bool process_xrl_action_return_arguments(XrlArgs* xrl_args,
00142                          MasterConfigTreeNode* ctn,
00143                          Action* action) const;
00144     void program_action_complete(bool success,
00145                  const string& command_stdout,
00146                  const string& command_stderr,
00147                  bool do_exec,
00148                  MasterConfigTreeNode* ctn,
00149                  string stdout_variable_name,
00150                  string stderr_variable_name) const;
00151     set<string> affected_modules() const;
00152     bool affects_module(const string& module) const;
00153     virtual string str() const;
00154     virtual bool expand_actions(string& error_msg);
00155     virtual bool check_referred_variables(string& error_msg) const;
00156 
00157 protected:
00158     list<Action*>   _actions;
00159 };
00160 
00161 
00162 #endif // __RTRMGR_TEMPLATE_COMMANDS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations