xorp

template_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 // $XORP: xorp/rtrmgr/template_tree_node.hh,v 1.54 2008/10/02 21:58:25 bms Exp $
00021 
00022 #ifndef __RTRMGR_TEMPLATE_TREE_NODE_HH__
00023 #define __RTRMGR_TEMPLATE_TREE_NODE_HH__
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #include "libxorp/mac.hh"
00032 
00033 #include "config_operators.hh"
00034 #include "rtrmgr_error.hh"
00035 #include "xorp_client.hh"
00036 
00037 #ifdef DEBUG_XRLDB
00038 #include "xrldb.hh"
00039 #endif
00040 
00041 enum TTNodeType {
00042     NODE_VOID       = 0,
00043     NODE_TEXT       = 1,
00044     NODE_UINT       = 2,
00045     NODE_INT        = 3,
00046     NODE_BOOL       = 4,
00047     NODE_TOGGLE     = 4,
00048     NODE_IPV4       = 5,
00049     NODE_IPV4NET    = 6,
00050     NODE_IPV6       = 7,
00051     NODE_IPV6NET    = 8,
00052     NODE_MACADDR    = 9,
00053     NODE_URL_FILE   = 10,
00054     NODE_URL_FTP    = 11,
00055     NODE_URL_HTTP   = 12,
00056     NODE_URL_TFTP   = 13,
00057     NODE_ARITH      = 14,
00058     NODE_UINTRANGE  = 15,
00059     NODE_IPV4RANGE  = 16,
00060     NODE_IPV6RANGE  = 17
00061 };
00062 
00063 enum TTSortOrder {
00064     ORDER_UNSORTED,
00065     ORDER_SORTED_NUMERIC,
00066     ORDER_SORTED_ALPHABETIC
00067 };
00068 
00069 class BaseCommand;
00070 class CommandTree;
00071 class ConfigTreeNode;
00072 class TemplateTree;
00073 
00074 class TemplateTreeNode {
00075 public:
00076     TemplateTreeNode(TemplateTree& template_tree, TemplateTreeNode* parent, 
00077              const string& path, const string& varname);
00078     virtual ~TemplateTreeNode();
00079 
00080     bool expand_template_tree(string& error_msg);
00081     bool check_template_tree(string& error_msg) const;
00082 
00083     virtual TTNodeType type() const { return NODE_VOID; }
00084     void add_cmd(const string& cmd) throw (ParseError);
00085     void add_action(const string& cmd, const list<string>& action_list)
00086     throw (ParseError);
00087 
00088     map<string, string> create_variable_map(const list<string>& segments) const;
00089 
00090     virtual string str() const;
00091     virtual string typestr() const { return string("void"); }
00092     virtual string default_str() const { return string(""); }
00093     virtual string encoded_typestr() const;
00094     virtual bool type_match(const string& s, string& error_msg) const;
00095     BaseCommand* command(const string& cmd_name);
00096     const BaseCommand* const_command(const string& cmd_name) const;
00097     set<string> commands() const;
00098     string varname() const { return _varname; }
00099     void set_tag() { _is_tag = true; }
00100     bool is_tag() const { return _is_tag; }
00101     string subtree_str() const;
00102     TemplateTreeNode* parent() const { return _parent; }
00103     const list<TemplateTreeNode*>& children() const { return _children; }
00104     const string& module_name() const { return _module_name; }
00105     const string& default_target_name() const { return _default_target_name; }
00106     void set_subtree_module_name(const string& module_name);
00107     void set_subtree_default_target_name(const string& default_target_name);
00108     const string& segname() const { return _segname; }
00109     string path() const;
00110     bool is_module_root_node() const;
00111     bool is_leaf_value() const;
00112 
00113     list<ConfigOperator> allowed_operators() const;
00114 
00115 #if 0
00116     bool check_template_tree(string& error_msg) const;
00117 #endif
00118     bool check_command_tree(const list<string>& commands, 
00119                 bool include_intermediate_nodes,
00120                 bool include_read_only_nodes,
00121                 bool include_permanent_nodes,
00122                 bool include_user_hidden_nodes,
00123                 size_t depth) const;
00124     bool has_default() const { return _has_default; }
00125     bool check_variable_name(const vector<string>& parts, size_t part) const;
00126     string get_module_name_by_variable(const string& varname) const;
00127     string get_default_target_name_by_variable(const string& varname) const;
00128     bool expand_variable(const string& varname, string& value,
00129              bool ignore_deleted_nodes) const;
00130     bool expand_expression(const string& expression, string& value) const;
00131     TemplateTreeNode* find_varname_node(const string& varname);
00132     const TemplateTreeNode* find_const_varname_node(const string& varname) const;
00133 
00134     bool check_allowed_value(const string& value, string& error_msg) const;
00135     bool verify_variables(const ConfigTreeNode& ctn, string& error_msg) const;
00136 
00137     const list<string>& mandatory_config_nodes() const { return _mandatory_config_nodes; }
00138     const string& help() const;
00139     const string& help_long() const;
00140 
00141     int child_number() const { return _child_number;}
00142 
00143     TTSortOrder order() const { return _order; }
00144     void set_order(TTSortOrder o) { _order = o; }
00145 
00146     bool is_deprecated() const { return _is_deprecated; }
00147     void set_deprecated(bool v) { _is_deprecated = v; }
00148     const string& deprecated_reason() const { return _deprecated_reason; }
00149     void set_deprecated_reason(const string& v) { _deprecated_reason = v; }
00150     bool is_user_hidden() const { return _is_user_hidden; }
00151     void set_user_hidden(bool v) { _is_user_hidden = v; }
00152     const string& user_hidden_reason() const { return _user_hidden_reason; }
00153     void set_user_hidden_reason(const string& v) { _user_hidden_reason = v; }
00154     bool is_mandatory() const { return _is_mandatory; }
00155     void set_mandatory(bool v) { _is_mandatory = v; }
00156     bool is_read_only() const { return _is_read_only; }
00157     const string& read_only_reason() const { return _read_only_reason; }
00158     bool is_permanent() const { return _is_permanent; }
00159     const string& permanent_reason() const { return _permanent_reason; }
00160 
00165     const TemplateTreeNode* find_first_deprecated_ancestor() const;
00166 
00171     const TemplateTreeNode* find_first_user_hidden_ancestor() const;
00172 
00173     void add_allowed_value(const string& value, const string& help);
00174     void add_allowed_range(int64_t lower_value, int64_t upper_value,
00175                const string& help);
00176     const map<string, string>& allowed_values() const { return _allowed_values; }
00177     const map<pair<int64_t, int64_t>, string>& allowed_ranges() const {
00178     return _allowed_ranges;
00179     }
00180 
00181 protected:
00182     void add_child(TemplateTreeNode* child);
00183 
00184     string strip_quotes(const string& s) const;
00185     void set_has_default() { _has_default = true; }
00186     bool name_is_variable() const;
00187 
00188     map<string, BaseCommand *> _cmd_map;
00189     TemplateTreeNode*   _parent;
00190     list<TemplateTreeNode*> _children;
00191 
00192 private:
00193     bool split_up_varname(const string& varname,
00194               list<string>& var_parts) const;
00195     TemplateTreeNode* find_parent_varname_node(const list<string>& var_parts);
00196     TemplateTreeNode* find_child_varname_node(const list<string>& var_parts);
00197 
00198     TemplateTree&   _template_tree;
00199 
00200     string _module_name;
00201     string _default_target_name;
00202     string _segname;
00203 
00204     // If this node has a variable name associated with it, _varname is
00205     // where its stored. Otherwise it is an empty string.
00206     string _varname;
00207     map<string, string> _allowed_values; // Allowed values for this template
00208     map<pair<int64_t, int64_t>, string> _allowed_ranges; // Allowed int ranges
00209 
00210     // Does the node have a default value?
00211     bool _has_default;
00212 
00213     // Is the node to be regarded as a tag for it's children rather
00214     // than a true tree node.
00215     bool _is_tag;
00216 
00217     // The CLI help information associated with this node.
00218     string _help;
00219     string _help_long;
00220 
00221     list<string>    _mandatory_config_nodes;
00222 
00223     int _child_number;
00224 
00225     TTSortOrder _order;
00226 
00227     bool        _verbose;     // Set to true if output is verbose
00228     bool        _is_deprecated;   // True if node's usage is deprecated
00229     string      _deprecated_reason;  // The reason for deprecation
00230     bool        _is_user_hidden;     // True if hidden from the user
00231     string      _user_hidden_reason; // The reason for user-hidden
00232     bool        _is_mandatory;       // True if this node is mandatory
00233     bool        _is_read_only;       // True if a read-only node
00234     string      _read_only_reason;   // The reason for read-only
00235     bool        _is_permanent;       // True if a permanent node
00236     string      _permanent_reason;   // The reason for permanent
00237 };
00238 
00239 class UIntTemplate : public TemplateTreeNode {
00240 public:
00241     UIntTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00242          const string& path, const string& varname, 
00243          const string& initializer) throw (ParseError);
00244 
00245     string typestr() const { return string("uint"); }
00246     TTNodeType type() const { return NODE_UINT; }
00247     unsigned int default_value() const { return _default; }
00248     string default_str() const;
00249     bool type_match(const string& s, string& error_msg) const;
00250 
00251 private:
00252     unsigned int _default;
00253 };
00254 
00255 class UIntRangeTemplate : public TemplateTreeNode {
00256 public:
00257     UIntRangeTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00258          const string& path, const string& varname, 
00259          const string& initializer) throw (ParseError);
00260     ~UIntRangeTemplate();
00261 
00262     string typestr() const { return string("uintrange"); }
00263     TTNodeType type() const { return NODE_UINTRANGE; }
00264     U32Range* default_value() const { return _default; }
00265     string default_str() const;
00266     bool type_match(const string& s, string& error_msg) const;
00267 
00268 private:
00269     U32Range* _default;
00270 };
00271 
00272 class IntTemplate : public TemplateTreeNode {
00273 public:
00274     IntTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00275         const string& path, const string& varname, 
00276         const string& initializer) throw (ParseError);
00277     string typestr() const { return string("int"); }
00278     TTNodeType type() const { return NODE_INT; }
00279     int default_value() const { return _default; }
00280     string default_str() const;
00281     bool type_match(const string& s, string& error_msg) const;
00282 
00283 private:
00284     int _default;
00285 };
00286 
00287 class ArithTemplate : public TemplateTreeNode {
00288 public:
00289     ArithTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00290           const string& path, const string& varname, 
00291           const string& initializer) throw (ParseError);
00292     string typestr() const { return string("uint"); }
00293     TTNodeType type() const { return NODE_ARITH; }
00294     string default_value() const { return _default; }
00295     string default_str() const { return _default; };
00296     bool type_match(const string& s, string& error_msg) const;
00297 
00298 private:
00299     string _default;
00300 };
00301 
00302 class TextTemplate : public TemplateTreeNode {
00303 public:
00304     TextTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00305          const string& path, const string& varname, 
00306          const string& initializer) throw (ParseError);
00307 
00308     string typestr() const { return string("text"); }
00309     TTNodeType type() const { return NODE_TEXT; }
00310     string default_value() const { return _default; }
00311     string default_str() const { return _default; }
00312     bool type_match(const string& s, string& error_msg) const;
00313 
00314 private:
00315     string _default;
00316 };
00317 
00318 class BoolTemplate : public TemplateTreeNode {
00319 public:
00320     BoolTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00321          const string& path, const string& varname, 
00322          const string& initializer) throw (ParseError);
00323 
00324     string typestr() const { return string("bool"); }
00325     TTNodeType type() const { return NODE_BOOL; }
00326     bool default_value() const { return _default; }
00327     string default_str() const;
00328     bool type_match(const string& s, string& error_msg) const;
00329 
00330 private:
00331     bool _default;
00332 };
00333 
00334 class IPv4Template : public TemplateTreeNode {
00335 public:
00336     IPv4Template(TemplateTree& template_tree, TemplateTreeNode* parent, 
00337          const string& path, const string& varname,
00338          const string& initializer) throw (ParseError);
00339     ~IPv4Template();
00340 
00341     string typestr() const { return string("IPv4"); }
00342     TTNodeType type() const { return NODE_IPV4; }
00343     IPv4 default_value() const { return *_default; }
00344     string default_str() const;
00345     bool type_match(const string& s, string& error_msg) const;
00346 
00347 private:
00348     IPv4* _default;
00349 };
00350 
00351 class IPv4NetTemplate : public TemplateTreeNode {
00352 public:
00353     IPv4NetTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00354             const string& path, const string& varname, 
00355             const string& initializer) throw (ParseError);
00356     ~IPv4NetTemplate();
00357 
00358     string typestr() const { return string("IPv4Net"); }
00359     TTNodeType type() const { return NODE_IPV4NET; }
00360     IPv4Net default_value() const { return *_default; }
00361     string default_str() const;
00362     bool type_match(const string& s, string& error_msg) const;
00363 
00364 private:
00365     IPv4Net* _default;
00366 };
00367 
00368 class IPv4RangeTemplate : public TemplateTreeNode {
00369 public:
00370     IPv4RangeTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00371             const string& path, const string& varname, 
00372             const string& initializer) throw (ParseError);
00373     ~IPv4RangeTemplate();
00374 
00375     string typestr() const { return string("IPv4Range"); }
00376     TTNodeType type() const { return NODE_IPV4RANGE; }
00377     IPv4Range default_value() const { return *_default; }
00378     string default_str() const;
00379     bool type_match(const string& s, string& error_msg) const;
00380 
00381 private:
00382     IPv4Range* _default;
00383 };
00384 
00385 class IPv6Template : public TemplateTreeNode {
00386 public:
00387     IPv6Template(TemplateTree& template_tree, TemplateTreeNode* parent,
00388          const string& path, const string& varname, 
00389          const string& initializer) throw (ParseError);
00390     ~IPv6Template();
00391 
00392     string typestr() const { return string("IPv6"); }
00393     TTNodeType type() const { return NODE_IPV6; }
00394     IPv6 default_value() const { return *_default; }
00395     string default_str() const;
00396     bool type_match(const string& s, string& error_msg) const;
00397 
00398 private:
00399     IPv6* _default;
00400 };
00401 
00402 class IPv6NetTemplate : public TemplateTreeNode {
00403 public:
00404     IPv6NetTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00405             const string& path, const string& varname, 
00406             const string& initializer) throw (ParseError);
00407     ~IPv6NetTemplate();
00408 
00409     string typestr() const { return string("IPv6Net"); }
00410     TTNodeType type() const { return NODE_IPV6NET; }
00411     IPv6Net default_value() const { return *_default; }
00412     string default_str() const;
00413     bool type_match(const string& s, string& error_msg) const;
00414 
00415 private:
00416     IPv6Net* _default;
00417 };
00418 
00419 class IPv6RangeTemplate : public TemplateTreeNode {
00420 public:
00421     IPv6RangeTemplate(TemplateTree& template_tree, TemplateTreeNode* parent,
00422          const string& path, const string& varname, 
00423          const string& initializer) throw (ParseError);
00424     ~IPv6RangeTemplate();
00425 
00426     string typestr() const { return string("IPv6Range"); }
00427     TTNodeType type() const { return NODE_IPV6RANGE; }
00428     IPv6Range default_value() const { return *_default; }
00429     string default_str() const;
00430     bool type_match(const string& s, string& error_msg) const;
00431 
00432 private:
00433     IPv6Range* _default;
00434 };
00435 
00436 class MacaddrTemplate : public TemplateTreeNode {
00437 public:
00438     MacaddrTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00439             const string& path, const string& varname, 
00440             const string& initializer) throw (ParseError);
00441     ~MacaddrTemplate();
00442 
00443     string typestr() const { return string("macaddr"); }
00444     TTNodeType type() const { return NODE_MACADDR; }
00445     Mac default_value() const { return *_default; }
00446     string default_str() const;
00447     bool type_match(const string& s, string& error_msg) const;
00448 
00449 private:
00450     Mac* _default;
00451 };
00452 
00453 class UrlFileTemplate : public TemplateTreeNode {
00454 public:
00455     UrlFileTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00456             const string& path, const string& varname,
00457             const string& initializer) throw (ParseError);
00458 
00459     string typestr() const { return string("URL_FILE"); }
00460     TTNodeType type() const { return NODE_URL_FILE; }
00461     string default_value() const { return _default; }
00462     string default_str() const { return _default; }
00463     bool type_match(const string& s, string& error_msg) const;
00464 
00465 private:
00466     string _default;
00467 };
00468 
00469 class UrlFtpTemplate : public TemplateTreeNode {
00470 public:
00471     UrlFtpTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00472            const string& path, const string& varname,
00473            const string& initializer) throw (ParseError);
00474 
00475     string typestr() const { return string("URL_FTP"); }
00476     TTNodeType type() const { return NODE_URL_FTP; }
00477     string default_value() const { return _default; }
00478     string default_str() const { return _default; }
00479     bool type_match(const string& s, string& error_msg) const;
00480 
00481 private:
00482     string _default;
00483 };
00484 
00485 class UrlHttpTemplate : public TemplateTreeNode {
00486 public:
00487     UrlHttpTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00488             const string& path, const string& varname,
00489             const string& initializer) throw (ParseError);
00490 
00491     string typestr() const { return string("URL_HTTP"); }
00492     TTNodeType type() const { return NODE_URL_HTTP; }
00493     string default_value() const { return _default; }
00494     string default_str() const { return _default; }
00495     bool type_match(const string& s, string& error_msg) const;
00496 
00497 private:
00498     string _default;
00499 };
00500 
00501 class UrlTftpTemplate : public TemplateTreeNode {
00502 public:
00503     UrlTftpTemplate(TemplateTree& template_tree, TemplateTreeNode* parent, 
00504             const string& path, const string& varname,
00505             const string& initializer) throw (ParseError);
00506 
00507     string typestr() const { return string("URL_TFTP"); }
00508     TTNodeType type() const { return NODE_URL_TFTP; }
00509     string default_value() const { return _default; }
00510     string default_str() const { return _default; }
00511     bool type_match(const string& s, string& error_msg) const;
00512 
00513 private:
00514     string _default;
00515 };
00516 
00517 #endif // __RTRMGR_TEMPLATE_TREE_NODE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations