xorp

configuration.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/policy/configuration.hh,v 1.20 2008/10/02 21:57:58 bms Exp $
00022 
00023 #ifndef __POLICY_CONFIGURATION_HH__
00024 #define __POLICY_CONFIGURATION_HH__
00025 
00026 
00027 
00028 #include "policy/common/policy_exception.hh"
00029 
00030 #include "process_watch_base.hh"
00031 #include "set_map.hh"
00032 #include "policy_map.hh"
00033 #include "policy_list.hh"
00034 #include "filter_manager_base.hh"
00035 #include "var_map.hh"
00036 
00037 typedef list<string>        POLICIES;
00038 typedef Code::TargetSet     TARGETSET;
00039 typedef uint32_t        tag_t;
00040 typedef set<tag_t>      TagSet;
00041 typedef map<string, string> RATTR;
00042 typedef map<string, string> RESOURCES;
00043 
00044 // XXX we go reverse in order to make peer specific policies override global
00045 // ones.  Global is "" so it's always smallest (first).
00046 #define FOR_ALL_POLICIES(n) \
00047     for (PROTOCOL::reverse_iterator i = _protocols.rbegin(); \
00048      i != _protocols.rend(); ++i) \
00049     for (POLICY::reverse_iterator n = i->second->rbegin(); \
00050         n != i->second->rend(); ++n)
00051 
00052 class IEMap {
00053 public:
00054     typedef map<string, PolicyList*>    POLICY; // modifier -> policy list
00055     typedef map<string, POLICY*>    PROTOCOL; // protocol -> modifiers
00056 
00057     IEMap();
00058     ~IEMap();
00059 
00060     PolicyList* find(const string& proto, const string& mod);
00061     void    insert(const string& proto, const string& mod, PolicyList* pl);
00062     void    clear();
00063     void    clear(TARGETSET& ts);
00064     void    get_targets(const string& proto, const string& mod,
00065                 TARGETSET& targets);
00066     void    compile(PolicyStatement& ps, TARGETSET& targets, tag_t& tag, map<string, set<uint32_t> >& ptags);
00067     void    compile(TARGETSET& targets, tag_t& tag, map<string, set<uint32_t> >& ptags);
00068     void    link_code(Code& code);
00069     void    link_code(const string& proto, Code& code);
00070     void    get_redist_tags(const string& proto, TagSet& ts);
00071 
00072 private:
00073     POLICY* find_policy(const string& proto);
00074     void    clear(POLICY* p);
00075 
00076     PROTOCOL    _protocols;
00077 };
00078 
00087 class Configuration :
00088     public NONCOPYABLE
00089 {
00090 public:
00091     typedef map<string,Code*> CodeMap;
00092     typedef map<string,TagSet*> TagMap;
00093 
00094    
00098     class ConfError : public PolicyException {
00099     public:
00100         ConfError(const char* file, size_t line, const string& init_why = "")   
00101             : PolicyException("ConfError", file, line, init_why) {}  
00102 
00103     };
00104 
00105 
00109     Configuration(ProcessWatchBase& pw);
00110     ~Configuration();
00111    
00119     void delete_term(const string& policy, const string& term);
00120 
00134     void update_term_block(const string& policy,
00135                            const string& term,
00136                            const uint32_t& block,
00137                const ConfigNodeId& order,
00138                            const string& statement);
00139     
00150     void create_term(const string& policy, const ConfigNodeId& order,
00151              const string& term);
00152   
00159     void create_policy(const string& policy);
00160     
00167     void delete_policy(const string& policy);
00168 
00175     void create_set(const string& set);
00176 
00185     void update_set(const string& type, const string& set, 
00186             const string& elements);
00187 
00194     void delete_set(const string& set);
00195 
00206     void add_to_set(const string& type, const string& name, 
00207             const string& element);
00208 
00219     void delete_from_set(const string& type, const string& name, 
00220              const string& element);
00221   
00229     void update_imports(const string& protocol, const POLICIES& imports,
00230                 const string& mod);
00231 
00239     void update_exports(const string& protocol, const POLICIES& exports,
00240             const string& mod);
00241 
00245     string str();
00246    
00261     void commit(uint32_t msec);
00262 
00272     void add_varmap(const string& protocol, const string& name,
00273             const string& type, const string& access,
00274             const VarRW::Id& id);
00275 
00282     void set_filter_manager(FilterManagerBase&);
00283 
00291     CodeMap& import_filters() { return _import_filters; }
00292 
00296     CodeMap& sourcematch_filters() { return _sourcematch_filters; }
00297 
00301     CodeMap& export_filters() { return _export_filters; }
00302 
00306     SetMap&  sets() { return _sets; }
00307 
00311     TagMap&  tagmap() { return _tagmap; }
00312 
00319     string dump_state(uint32_t id);
00320 
00321     void clear_imports(const string& protocol);
00322     void clear_exports(const string& protocol);
00323     bool test_policy(const string& policy, const RATTR& attrs, RATTR& mods);
00324     void show(const string& type, const string& name, RESOURCES& res);
00325     void show_sets(const string& type, const string& name, RESOURCES& res);
00326     void show_policies(const string& name, RESOURCES& res);
00327 
00328 private:
00336     Term& find_term(const string& policy, const string& term);
00337 
00344     void update_dependencies(PolicyStatement& policy);
00345 
00352     void compile_policy(const string& name);
00353 
00358     void compile_policies();
00359 
00367     void link_sourcematch_code(const Code::Target& target);
00368     
00374     void update_tagmap(const string& protocol);
00375   
00379     void link_code();
00380 
00381     void update_ie(const string& protocol, const POLICIES& policies, 
00382            IEMap& iemap, PolicyList::PolicyType pt, const string& mod);
00383 
00384     void link_code(const Code::Target& target, IEMap& iemap, CodeMap& codemap);
00385 
00386     string codemap_str(CodeMap& cm);
00387     void   policy_modified(const string& policy);
00388 
00389     typedef set<string> PolicySet;
00390 
00391     PolicyMap           _policies;
00392     IEMap           _imports;
00393     IEMap           _exports;
00394     SetMap          _sets;
00395     PolicySet           _modified_policies;
00396     TARGETSET           _modified_targets;
00397     ElementFactory      _ef;
00398     CodeMap         _import_filters;
00399     CodeMap         _sourcematch_filters;
00400     CodeMap         _export_filters;
00401     tag_t           _currtag;
00402     map<string, set<uint32_t> > _protocol_tags;
00403     TagMap          _tagmap;
00404     VarMap          _varmap;
00405     FilterManagerBase*      _filter_manager; // do not delete
00406 };
00407 
00408 #endif // __POLICY_CONFIGURATION_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations