xorp

conf_tree_node.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 
00021 #ifndef __RTRMGR_CONF_TREE_NODE_HH__
00022 #define __RTRMGR_CONF_TREE_NODE_HH__
00023 
00024 #include "libxorp/xorp.h"
00025 
00026 
00027 
00028 
00029 
00030 
00031 #ifdef HAVE_SYS_TIME_H
00032 #include <sys/time.h>
00033 #endif
00034 
00035 #include "libxorp/timeval.hh"
00036 #include "libproto/config_node_id.hh"
00037 
00038 #include "config_operators.hh"
00039 
00040 
00041 class Command;
00042 class CommandTree;
00043 class RouterCLI;
00044 class TaskManager;
00045 class TemplateTreeNode;
00046 class ConfigTreeNode;
00047 
00048 class CTN_Compare {
00049 public:
00050     bool operator() (ConfigTreeNode* a, ConfigTreeNode *b);
00051 };
00052 
00053 class ConfigTreeNode {
00054 public:
00055     ConfigTreeNode(bool verbose);
00056     ConfigTreeNode(const ConfigTreeNode& ctn);
00057     ConfigTreeNode(const string& node_name, const string& path, 
00058            const TemplateTreeNode* ttn, ConfigTreeNode* parent,
00059            const ConfigNodeId& node_id,
00060            uid_t user_id, uint32_t clientid, bool verbose);
00061     virtual ~ConfigTreeNode();
00062 
00063     bool operator==(const ConfigTreeNode& them) const;
00064     bool is_same(const ConfigTreeNode& them, bool ignore_node_id) const;
00065     
00066     virtual ConfigTreeNode* create_node(const string& segment, 
00067                     const string& path,
00068                     const TemplateTreeNode* ttn, 
00069                     ConfigTreeNode* parent_node, 
00070                     const ConfigNodeId& node_id,
00071                     uid_t user_id, 
00072                     uint32_t clientid, 
00073                     bool verbose) = 0;
00074     virtual ConfigTreeNode* create_node(const ConfigTreeNode& ctn) = 0;
00075 
00076     void add_child(ConfigTreeNode* child);
00077     void remove_child(ConfigTreeNode* child);
00078     void add_default_children();
00079     void recursive_add_default_children();
00080     bool check_allowed_value(string& error_msg) const;
00081     bool check_allowed_value(const string& value, string& error_msg) const;
00082     bool check_allowed_operator(const string& value, string& error_msg) const;
00083     bool set_value(const string& value, uid_t user_id, string& error_msg);
00084     void set_value_without_verification(const string& value, uid_t user_id);
00085     bool set_operator(ConfigOperator op, uid_t user_id, string& error_msg);
00086     void set_operator_without_verification(ConfigOperator op, uid_t user_id);
00087     void mark_subtree_as_committed();
00088     void mark_subtree_as_uncommitted();
00089 
00090     bool merge_deltas(uid_t user_id,
00091               const ConfigTreeNode& delta_node,
00092               bool provisional_change,
00093               bool preserve_node_id,
00094               string& error_msg);
00095 
00096     bool merge_deletions(uid_t user_id, const ConfigTreeNode& deletion_node, 
00097              bool provisional_change, string& error_msg);
00098     ConfigTreeNode* find_config_module(const string& module_name);
00099 
00100     bool check_config_tree(string& error_msg) const;
00101 
00102     string discard_changes(int depth, int last_depth);
00103     int type() const;
00104     bool is_root_node() const { return (_parent == NULL); }
00105     bool is_tag() const;
00106     bool is_multi_value_node() const;
00107     bool is_leaf_value() const;
00108     bool is_read_only() const;
00109     const string& read_only_reason() const;
00110     bool is_permanent() const;
00111     const string& permanent_reason() const;
00112     bool is_user_hidden() const;
00113     const string& user_hidden_reason() const;
00114     unsigned int depth() const;
00115     const TemplateTreeNode* template_tree_node() const { return _template_tree_node; }
00116     int child_number() const;
00117     string str() const;
00118     string node_str() const;
00119     string typestr() const;
00120     const string& segname() const { return _segname; }
00121     const string& value() const;
00122     const ConfigNodeId& node_id() const { return _node_id; }
00123     void set_node_id_position(const ConfigNodeId::Position& p) {
00124     _node_id.set_position(p);
00125     }
00126     ConfigNodeId& node_id_generator() { return _node_id_generator; }
00127     uint32_t clientid() const { return _clientid; }
00128     bool has_value() const { return _has_value; }
00129     ConfigOperator get_operator() const;
00130     uid_t user_id() const { return _user_id; }
00131     void set_existence_committed(bool v) { _existence_committed = v; }
00132     bool existence_committed() const { return _existence_committed; }
00133     bool deleted() const { return _deleted; }
00134     void undelete() { _deleted = false; }
00135     void undelete_subtree();
00136     void undelete_node_and_ancestors();
00137     const TimeVal& modification_time() const { return _modification_time; }
00138     const string& path() const { return _path; }
00139     void set_parent(ConfigTreeNode* parent) { _parent = parent; }
00140     ConfigTreeNode* parent() { return _parent; }
00141     const ConfigTreeNode* const_parent() const { return _parent; }
00142     list<ConfigTreeNode*>& children() { return _children; }
00143     const list<ConfigTreeNode*>& const_children() const { return _children; }
00144     string show_subtree(bool show_top, int depth, int indent, bool do_indent,
00145             bool numbered, bool annotate,
00146             bool suppress_default_values) const;
00147     void mark_subtree_for_deletion(uid_t user_id);
00148     void delete_subtree_silently();
00149     void clone_subtree(const ConfigTreeNode& orig_node);
00150     bool retain_different_nodes(const ConfigTreeNode& them, 
00151                 bool retain_changed_values);
00152     bool retain_deletion_nodes(const ConfigTreeNode& them,
00153                    bool retain_value_changed);
00154     void retain_common_nodes(const ConfigTreeNode& them);
00155     ConfigTreeNode* find_node(const list<string>& path);
00156     const ConfigTreeNode* find_const_node(const list<string>& path) const;
00157     string subtree_str() const;
00158 
00159     bool expand_variable(const string& varname, string& value,
00160              bool ignore_deleted_nodes) const;
00161     bool expand_variable_to_full_varname(const string& varname,
00162                      string& full_varname) const;
00163     bool expand_expression(const string& expression, string& value) const;
00164     void expand_varname_to_matchlist(const vector<string>& v, size_t depth,
00165                      list<string>& matches) const;
00166     bool set_variable(const string& varname, const string& value);
00167     const string& named_value(const string& varname) const;
00168     void set_named_value(const string& varname, const string& value);
00169     string show_operator() const;
00170     bool is_committed() const;
00171     bool is_uncommitted() const;
00172     bool is_default_value() const;
00173     bool is_default_value(const string& test_value) const;
00174     bool has_undeleted_children() const;
00175     virtual void update_node_id_position();
00176 
00177 protected:
00178     bool split_up_varname(const string& varname,
00179               list<string>& var_parts) const;
00180     string join_up_varname(const list<string>& var_parts) const;
00181     enum VarType { NONE, NODE_VALUE, NODE_OPERATOR, NODE_ID, 
00182            NAMED, TEMPLATE_DEFAULT };
00183     ConfigTreeNode* find_varname_node(const string& varname, VarType& type);
00184     const ConfigTreeNode* find_const_varname_node(const string& varname, 
00185                           VarType& type) const;
00186     ConfigTreeNode* find_parent_varname_node(const list<string>& var_parts,
00187                          VarType& type);
00188     ConfigTreeNode* find_child_varname_node(const list<string>& var_parts,
00189                         VarType& type);
00190     void sort_by_template(list<ConfigTreeNode*>& children) const;
00191     string show_node_id(bool numbered, const ConfigNodeId& node_id) const;
00192     virtual void allocate_unique_node_id();
00193     string quoted_value(const string& value) const;
00194 
00195 
00196     const TemplateTreeNode* _template_tree_node;
00197     bool _has_value;
00198     string _value;
00199     string _committed_value;
00200     ConfigOperator _operator;
00201     ConfigOperator _committed_operator;
00202     string _segname;
00203     string _path;
00204     ConfigTreeNode* _parent;
00205     list<ConfigTreeNode *> _children;
00206     ConfigNodeId _node_id;
00207     ConfigNodeId _node_id_generator;
00208     uid_t _user_id; // the user ID of the user who last changed this node
00209     uid_t _committed_user_id;   // The user ID of the user who last changed
00210                 // this node before the last commit
00211     uint32_t _clientid;
00212     TimeVal _modification_time; // When the node was last changed
00213     TimeVal _committed_modification_time; // When the node was last changed
00214                       // before the last commit
00215 
00216     // Flags to keep track of what changes we've made since the last commit
00217     bool _existence_committed;  // Do we need to run %create commands
00218     bool _value_committed;  // Do we need to run %set commands
00219     bool _deleted;  // The node is deleted, but commit has not yet happened
00220 
00221     // Variables contains the explicit variables set on this node
00222     map<string, string> _variables;
00223 
00224     // on_parent_path is used during variable expansion to keep track of where
00225     // we came from
00226     bool _on_parent_path;
00227 
00228     bool _verbose;
00229 
00230 
00231 private:
00232 };
00233 
00234 #endif // __RTRMGR_CONF_TREE_NODE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations