diff --git a/.gitignore b/.gitignore index 80129c6c864..15c0fff4483 100644 --- a/.gitignore +++ b/.gitignore @@ -45,8 +45,25 @@ src/sunstone/public/css/app.min.css src/sunstone/public/dist/ src/sunstone/public/locale/languages/*.js src/sunstone/public/package-lock.json +src/sunstone/guac/node_modules +src/sunstone/guac/dist +src/sunstone/guac/package-lock.json +src/sunstone/vmrc/node_modules +src/sunstone/vmrc/dist +src/sunstone/vmrc/package-lock.json +src/sunstone/vmrc/v8-compile-cache-0/ .tx/config +src/fireedge/node_modules +src/fireedge/dist +src/fireedge/test +src/fireedge/.vscode/launch.json +src/fireedge/yarn-error.log +src/fireedge/.DS_Store +src/fireedge/cypress/screenshots +src/fireedge/yarn.lock +src/fireedge/package-lock.json + src/onedb/local/ src/onedb/shared/ @@ -57,4 +74,4 @@ share/esx-fw-vnc/.vagrant* share/context/* !share/context/download_context.sh -!share/context/SConstruct +!share/context/SConstruct \ No newline at end of file diff --git a/README.md b/README.md index 8bdb1e5a04e..7167ea03f43 100644 --- a/README.md +++ b/README.md @@ -1,187 +1,61 @@ -# OpenNebula +# [![OpenNebula Logo](https://opennebula.io/wp-content/uploads/2019/04/img-logo-blue.svg)](https://opennebula.io/) [![Build Status](https://travis-ci.org/OpenNebula/one.svg?branch=master)](https://travis-ci.org/OpenNebula/one) ## Description -OpenNebula is an open-source project delivering a simple but feature-rich and -flexible solution to build and manage enterprise clouds and virtualized data centers. +[OpenNebula](https://opennebula.io/) is an open source platform delivering a simple but feature-rich and flexible solution to build and manage enterprise clouds for virtualized services, containerized applications and serverless computing. -Complete documentation: https://docs.opennebula.io +[![OpenNebula Architecture](https://opennebula.io/wp-content/uploads/2020/08/KeyFeatures_small.png)](https://opennebula.io/discover/) -How to contribute: https://github.com/OpenNebula/one/wiki/How-to-Contribute-to-Development +### To Start Using OpenNebula -## Installation - -### Requirements - -This machine will act as the OpenNebula server and therefore needs to have -installed the following software: - -* **ruby** >= 1.8.7 -* **sqlite3** >= 3.5.2 -* **xmlrpc-c** >= 1.06 -* **openssl** >= 0.9 -* **ssh** -* **sqlite3-ruby** gem - -Additionally, to build OpenNebula from source you need: - -* Development versions of the **sqlite3**, **xmlrpc-c** and **openssl** - packages, if your distribution does not install them with the libraries. -* **scons** >= 0.97 -* **g++** >= 4 -* **flex** >= 2.5 (optional, only needed to rebuild the parsers) -* **bison** >= 2.3 (optional, only needed to rebuild the parsers) -* **libxml2-dev** -* **libvncserver-dev** (optional, only needed to build svncterm_server) - -### Ruby Libraries Requirements - -A set of gem requirements are needed to make several components work. We -include a handy script to install them and the requirements. It is located at -`share/install_gems/install_gems` and you should use it to install the -required gems. You have more information at: - - https://docs.opennebula.io/stable/integration/references/compile.html - -If you want to install them manually here are the list of required rubygems: - -* OpenNebula and clients (plus cloud interfaces) - * sqlite3 - * json - * sequel - * mysql - * net-ldap - * amazon-ec2 - * rack - * sinatra - * thin - * uuidtools - * curb - * nokogiri - -* Sunstone server - * json - * rack - * sinatra - * thin - * sequel - * nokogiri - -### Optional Packages - -These packages are not needed to run or build OpenNebula. They improve the -performance of the user-land libraries and tools of OpenNebula, nor the core -system. You will probably experiment a more responsive CLI. - -First install rubygems and ruby development libraries - -* **ruby-dev** -* **rubygems** -* **rake** -* **make** - -Then install the following packages: +* Explore OpenNebula’s **key features** [on our website](https://opennebula.io/discover/). +* Have a look at our [introductory datasheet](https://support.opennebula.pro/hc/en-us/articles/360036935791-OpenNebula-Key-Features-Datasheet). +* Browse our catalog of [screencasts and video tutorials](https://opennebula.io/screencasts/). +* Download our [technical white papers](https://support.opennebula.pro/hc/en-us/sections/200820599-OpenNebula-Product-White-Papers). +* See our [Documentation](https://docs.opennebula.io). +* Join our [Community Forum](https://forum.opennebula.io/). -* **ruby xmlparser**, some distributions include a binary package for this - (**libxml-parser-ruby1.8**). If it is not available in your distribution - install expat libraries with its development files and install xmlparser - using gem: +[![OpenNebula Intro](https://opennebula.io/wp-content/uploads/2020/08/Intro_Screencast_small.png)](https://opennebula.io/screencast-overview/) - $ sudo gem install xmlparser --no-document +### Contributing to OpenNebula - Note the extra parameters to gem install. Some versions of xmlparser have - problems building the documentation and we can use it without documentation - installed. +* Contribute to [Development](https://github.com/OpenNebula/one/wiki/How-to-Contribute-to-Development). +* Learn about how to expand our [Add-on Catalog](https://github.com/OpenNebula/one/wiki/How-to-participate-in-Add_on-Development). +* Help us [translate OpenNebula](https://www.transifex.com/opennebula/one/) to your language. +* Report a [security vulnerability](https://github.com/OpenNebula/one/wiki/Vulnerability-Management-Process). -* **ruby ox**, fast xml parsing library: - - $ sudo gem install ox --no-document - - -### Building - -Compilation is done using **scons** command: - - $ scons [OPTION=VALUE] - -The argument expression *[OPTIONAL]* is used to set non-default values for: - - OPTION VALUE - sqlite_db path-to-sqlite-install - sqlite no if you don't want to build sqlite support - mysql yes if you want to build mysql support - xmlrpc path-to-xmlrpc-install - parsers yes if you want to rebuild flex/bison files - new_xmlrpc yes if you have an xmlrpc-c version >= 1.31 - sunstone yes if you want to build sunstone minified files - systemd yes if you want to build systemd support - svncterm no if you want to skip building vnc support for LXD drivers - - -### Installation - -OpenNebula can be installed in two modes: system-wide, or in self-contained -directory. In either case, you do not need to run OpenNebula as root. These -options can be specified when running the install script: - - $ ./install.sh install_options - -where **install_options** can be one or more of: +## Installation - OPTION VALUE - -u user that will run OpenNebula, defaults to user executing - install.sh - -g group of the user that will run OpenNebula, defaults to user - executing install.sh - -k keep current configuration files, useful when upgrading - -d target installation directory. If defined, it will specified - the path for the self-contained install. If not defined, the - installation will be performed system wide - -c only install client utilities: OpenNebula cli, occi and ec2 - client files - -r remove Opennebula, only useful if -d was not specified, - otherwise rm -rf $ONE_LOCATION would do the job - -p do not install OpenNebula Sunstone non-minified files - -G install only OpenNebula Gate - -f install only OpenNebula Flow - -h prints installer help +You can find more information about OpenNebula’s architecture, installation, configuration and references to configuration files in [this documentation section](https://docs.opennebula.io/stable/deployment/index.html). +It is very useful to learn where [log files of the main OpenNebula components](http://docs.opennebula.io/stable/deployment/references/log_debug.html) are placed. Also check the references about the [main OpenNebula daemon configuration file](https://docs.opennebula.io/stable/deployment/references/oned_conf.html). -## Deployment +### Front-end Installation -You can find the documentation about OpenNebula architecture, installation, -configuration and references to configuration files in this documentation -chapter: +The Front-end is the central part of an OpenNebula installation. This is the machine where the server software is installed and where you connect to manage your cloud. It can be a physical node or a virtual instance. -https://docs.opennebula.io/stable/deployment/index.html +Please, visit the [official documentation](http://docs.opennebula.io/stable/deployment/opennebula_installation/overview.html) for more details and a step-by-step guide. Using the packages provided on our site is the recommended method, to ensure the installation of the latest version, and to avoid possible package divergences with different distributions. There are two alternatives here: you can add our **package repositories** to your system, or visit the [software menu](opennebula.io/use/) to **download the latest package** for your Linux distribution. -The reference about the main configuration file is located here: +If there are no packages for your distribution, please check the [build dependencies](http://docs.opennebula.io/stable/integration/references/build_deps.html#build-deps) for OpenNebula and head to the [Building from Source Code guide](http://docs.opennebula.io/stable/integration/references/compile.html#compile). -https://docs.opennebula.io/stable/deployment/references/oned_conf.html +### Node Installation +After the OpenNebula Front-end is correctly set up, the next step is preparing the hosts where the VMs are going to run. Please, refer to the [documentation](http://docs.opennebula.io/stable/deployment/node_installation/overview.html) site for more details. ## Contact -OpenNebula web page: https://opennebula.io - -Development and issue tracking: https://github.com/OpenNebula/one/issues - -Support: https://opennebula.io/support - +* OpenNebula web page: https://opennebula.io +* Development and issue tracking: https://github.com/OpenNebula/one/issues +* Enterprise Services: https://opennebula.io/enterprise/ ## License -Copyright 2002-2020, OpenNebula Project, OpenNebula Systems (formerly C12G Labs) +Copyright 2002-2020, OpenNebula Project, OpenNebula Systems (formerly C12G Labs). + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -Licensed under the Apache License, Version 2.0 (the "License"); you may -not use this file except in compliance with the License. You may obtain -a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/SConstruct b/SConstruct index f8a0764a24a..e02df5e6875 100755 --- a/SConstruct +++ b/SConstruct @@ -82,7 +82,6 @@ main_env.Append(LIBPATH=[ cwd+'/src/cluster', cwd+'/src/datastore', cwd+'/src/group', - cwd+'/src/mad', cwd+'/src/nebula', cwd+'/src/pool', cwd+'/src/template', @@ -112,14 +111,14 @@ main_env.Append(LIBPATH=[ cwd+'/src/market', cwd+'/src/ipamm', cwd+'/src/data_model', - cwd+'/src/monitor/src/protocol' + cwd+'/src/protocol' ]) # Compile flags main_env.Append(CPPFLAGS=[ "-g", "-Wall", - "-std=c++11" + "-std=c++14" ]) # Linking flags & common libraries @@ -202,6 +201,9 @@ main_env.Append(enterprise=ARGUMENTS.get('enterprise', 'no')) # Sunstone minified files generation main_env.Append(sunstone=ARGUMENTS.get('sunstone', 'no')) +# FireEdge minified files generation +main_env.Append(fireedge=ARGUMENTS.get('fireedge', 'no')) + # TODO this should be aligned with one-ee-tools workflows # Onedb Marshal files generation main_env.Append(marshal=ARGUMENTS.get('marshal', 'no')) @@ -281,7 +283,6 @@ build_scripts = [ 'src/cluster/SConstruct', 'src/datastore/SConstruct', 'src/group/SConstruct', - 'src/mad/SConstruct', 'src/nebula/SConstruct', 'src/pool/SConstruct', 'src/vm/SConstruct', @@ -311,11 +312,13 @@ build_scripts = [ 'src/ipamm/SConstruct', 'src/sunstone/public/locale/languages/SConstruct', 'src/sunstone/public/SConstruct', + 'src/fireedge/SConstruct', 'share/rubygems/SConstruct', 'src/client/SConstruct', 'src/docker_machine/SConstruct', 'src/monitor/SConstruct', 'src/onedb/SConstruct', + 'src/protocol/SConstruct', svncterm_path, 'share/context/SConstruct' ] diff --git a/include/AclManager.h b/include/AclManager.h index c85f7c1e4bc..d4257056b0d 100644 --- a/include/AclManager.h +++ b/include/AclManager.h @@ -17,23 +17,18 @@ #ifndef ACL_MANAGER_H_ #define ACL_MANAGER_H_ -#include "AuthManager.h" +#include "Listener.h" #include "AuthRequest.h" #include "PoolObjectSQL.h" -#include "AclRule.h" -#include "NebulaLog.h" - -using namespace std; +class AclRule; class PoolObjectAuth; class SqlDB; -extern "C" void * acl_action_loop(void *arg); - /** * This class manages the ACL rules and the authorization engine */ -class AclManager : public Callbackable, public ActionListener +class AclManager : public Callbackable { public: /** @@ -56,6 +51,8 @@ class AclManager : public Callbackable, public ActionListener void finalize(); + void join_thread(); + /** * Reload the ACL rules from the DB. This function needs to be used when * a server becomes leader of the zone as the ACL cache maybe out-dated @@ -79,10 +76,10 @@ class AclManager : public Callbackable, public ActionListener * @param op The operation to be authorized * @return true if the authorization is granted by any rule */ - const bool authorize(int uid, - const set& user_groups, - const PoolObjectAuth& obj_perms, - AuthRequest::Operation op); + bool authorize(int uid, + const std::set& user_groups, + const PoolObjectAuth& obj_perms, + AuthRequest::Operation op) const; /** * Takes an authorization request for oneadmin @@ -92,8 +89,8 @@ class AclManager : public Callbackable, public ActionListener * @param op The operation to be authorized * @return true if the authorization is granted for oneadmin */ - const bool oneadmin_authorize(const PoolObjectAuth& obj_perms, - AuthRequest::Operation op); + bool oneadmin_authorize(const PoolObjectAuth& obj_perms, + AuthRequest::Operation op) const; /** * Adds a new rule to the ACL rule set @@ -113,7 +110,7 @@ class AclManager : public Callbackable, public ActionListener long long resource, long long rights, long long zone, - string& error_str); + std::string& error_str); /** * Deletes a rule from the ACL rule set * @@ -121,7 +118,7 @@ class AclManager : public Callbackable, public ActionListener * @param error_str Returns the error reason, if any * @return 0 on success */ - virtual int del_rule(int oid, string& error_str); + virtual int del_rule(int oid, std::string& error_str); /** * Deletes a new rule from the ACL rule set @@ -138,7 +135,7 @@ class AclManager : public Callbackable, public ActionListener long long resource, long long rights, long long zone, - string& error_str); + std::string& error_str); /** * Deletes rules that apply to this user id @@ -190,16 +187,16 @@ class AclManager : public Callbackable, public ActionListener * @param cids Set of object cluster IDs over which the user can operate */ void reverse_search(int uid, - const set& user_groups, + const std::set& user_groups, PoolObjectSQL::ObjectType obj_type, AuthRequest::Operation op, bool disable_all_acl, bool disable_cluster_acl, bool disable_group_acl, bool& all, - vector& oids, - vector& gids, - vector& cids); + std::vector& oids, + std::vector& gids, + std::vector& cids); /* ---------------------------------------------------------------------- */ /* DB management */ @@ -215,20 +212,7 @@ class AclManager : public Callbackable, public ActionListener * @param oss The output stream to dump the rule set contents * @return 0 on success */ - virtual int dump(ostringstream& oss); - - // ------------------------------------------------------------------------- - // Refresh loop thread - // ------------------------------------------------------------------------- - /** - * Gets the AclManager thread identification. The thread is only - * initialized if the refresh_cache flag is true. - * @return pthread_t for the manager thread (that in the action loop). - */ - pthread_t get_thread_id() const - { - return acl_thread; - }; + virtual int dump(std::ostringstream& oss); protected: /** @@ -237,9 +221,11 @@ class AclManager : public Callbackable, public ActionListener * from DB) */ AclManager(int _zone_id) - :zone_id(_zone_id), db(0), is_federation_slave(false) + : zone_id(_zone_id) + , db(0) + , is_federation_slave(false) + , timer_period(-1) { - pthread_mutex_init(&mutex, 0); }; // ------------------------------------------------------------------------- @@ -249,12 +235,12 @@ class AclManager : public Callbackable, public ActionListener * ACL rules. Each rule is indexed by its 'user' long long attibute, * several rules can apply to the same user */ - multimap acl_rules; + std::multimap acl_rules; /** * Rules indexed by oid. Stores the same rules as acl_rules */ - map acl_rules_oids; + std::map acl_rules_oids; private: @@ -276,16 +262,17 @@ class AclManager : public Callbackable, public ActionListener * @return true if any rule grants permission */ bool match_rules( - long long user_req, - long long resource_oid_req, - long long resource_gid_req, - const set& resource_cid_req, - long long resource_all_req, - long long rights_req, - long long resource_oid_mask, - long long resource_gid_mask, - long long resource_cid_mask, - const multimap& rules); + long long user_req, + long long resource_oid_req, + long long resource_gid_req, + const std::set& resource_cid_req, + long long resource_all_req, + long long rights_req, + long long resource_oid_mask, + long long resource_gid_mask, + long long resource_cid_mask, + const std::multimap& rules) const; + /** * Wrapper for match_rules. It will check if any rules in the temporary * multimap or in the internal one grants permission. @@ -304,16 +291,16 @@ class AclManager : public Callbackable, public ActionListener * @return true if any rule grants permission */ bool match_rules_wrapper( - long long user_req, - long long resource_oid_req, - long long resource_gid_req, - const set& resource_cid_req, - long long resource_all_req, - long long rights_req, - long long individual_obj_type, - long long group_obj_type, - long long cluster_obj_type, - const multimap &tmp_rules); + long long user_req, + long long resource_oid_req, + long long resource_gid_req, + const std::set& resource_cid_req, + long long resource_all_req, + long long rights_req, + long long individual_obj_type, + long long group_obj_type, + long long cluster_obj_type, + const std::multimap &tmp_rules) const; /** * Deletes all rules that match the user mask * @@ -348,23 +335,7 @@ class AclManager : public Callbackable, public ActionListener // Mutex synchronization // ------------------------------------------------------------------------- - pthread_mutex_t mutex; - - /** - * Function to lock the manager - */ - void lock() - { - pthread_mutex_lock(&mutex); - }; - - /** - * Function to unlock the manager - */ - void unlock() - { - pthread_mutex_unlock(&mutex); - }; + mutable std::mutex acl_mutex; // ------------------------------------------------------------------------- // DataBase implementation variables @@ -374,15 +345,6 @@ class AclManager : public Callbackable, public ActionListener */ SqlDB * db; - /** - * Tablename for the ACL rules - */ - static const char * table; - - static const char * db_names; - - static const char * db_bootstrap; - /** * Callback function to unmarshall the ACL rules * @param num the number of columns read from the DB @@ -440,33 +402,18 @@ class AclManager : public Callbackable, public ActionListener time_t timer_period; /** - * Thread id for the ACL Manager + * Timer action async execution */ - pthread_t acl_thread; - - /** - * Action engine for the Manager - */ - ActionManager am; - - /** - * Function to execute the Manager action loop method within a new pthread - * (requires C linkage) - */ - friend void * acl_action_loop(void *arg); + std::unique_ptr timer_thread; // ------------------------------------------------------------------------- // Action Listener interface // ------------------------------------------------------------------------- - void timer_action(const ActionRequest& ar) + void timer_action() { select(); }; - void finalize_action(const ActionRequest& ar) - { - NebulaLog::log("ACL",Log::INFO,"Stopping ACL Manager..."); - }; }; #endif /*ACL_MANAGER_H*/ diff --git a/include/AclRule.h b/include/AclRule.h index 8f2ddde329d..0c5a3c3406a 100644 --- a/include/AclRule.h +++ b/include/AclRule.h @@ -17,14 +17,12 @@ #ifndef ACL_RULE_H_ #define ACL_RULE_H_ -#include #include #include #include "PoolObjectSQL.h" #include "AuthRequest.h" -using namespace std; /** * An ACL Rule is composed of three 64 bit numbers: user, resource and rights. @@ -97,7 +95,7 @@ class AclRule * * @return a human readable string for this rule */ - const string& to_str() const + const std::string& to_str() const { return str; }; @@ -108,7 +106,7 @@ class AclRule * @param error_str Returns the error message, if any * @return true if the rule is wrong */ - bool malformed(string& error_str) const; + bool malformed(std::string& error_str) const; /** * Function to print the object into a string in XML format @@ -116,7 +114,7 @@ class AclRule * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const; + std::string& to_xml(std::string& xml) const; /** * Rebuilds the rule from an xml formatted string @@ -247,7 +245,7 @@ class AclRule /** * Human readable representation of the rule */ - string str; + std::string str; /** * Builds the human representation of the ACL diff --git a/include/ActionManager.h b/include/ActionManager.h deleted file mode 100644 index 97a9d63e908..00000000000 --- a/include/ActionManager.h +++ /dev/null @@ -1,250 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#ifndef ACTION_MANAGER_H_ -#define ACTION_MANAGER_H_ - -#include -#include -#include -#include - -/** - * Represents a generic request, pending actions are stored in a queue. - * Each element stores the base action type, additional data is added by each - * ActionListener implementation. - */ -class ActionRequest -{ -public: - /** - * Base Action types - */ - enum Type - { - FINALIZE, - TIMER, - USER - }; - - Type type() const - { - return _type; - } - - ActionRequest(Type __type): _type(__type){}; - - virtual ~ActionRequest(){}; - - virtual ActionRequest * clone() const - { - return new ActionRequest(_type); - } - -protected: - Type _type; -}; - -/** - * ActionListener class. Interface to be implemented by any class that need to - * handle actions. - */ -class ActionListener -{ -protected: - ActionListener(){}; - - virtual ~ActionListener(){}; - - /** - * the user_action() function is executed upon action arrival. - * This function should check the action type, and perform the - * corresponding action. - * @param ar the ActionRequest - */ - virtual void user_action(const ActionRequest& ar){}; - - /** - * Periodic timer action, executed each time the time_out expires. Listener - * needs to re-implement the default timer action if needed. - * @param ar the ActionRequest - */ - virtual void timer_action(const ActionRequest& ar){}; - - /** - * Action executed when the Manager finlizes. Listener needs to re-implement - * the default action if needed. - * @param ar the ActionRequest - */ - virtual void finalize_action(const ActionRequest& ar){}; - -private: - friend class ActionManager; - - /** - * Invoke the action handler - */ - void _do_action(const ActionRequest& ar) - { - switch(ar.type()) - { - case ActionRequest::FINALIZE: - finalize_action(ar); - break; - - case ActionRequest::TIMER: - timer_action(ar); - break; - - case ActionRequest::USER: - user_action(ar); - break; - } - } -}; - - -/** - * ActionManager. Provides action support for a class implementing - * the ActionListener interface. - */ -class ActionManager -{ -public: - - ActionManager(); - - virtual ~ActionManager(); - - /** - * Function to trigger an action to this manager. - * @param action the action name - * @param args arguments for the action - */ - void trigger(const ActionRequest& ar); - - /** - * Trigger the FINALIZE event - */ - void finalize() - { - ActionRequest frequest(ActionRequest::FINALIZE); - - trigger(frequest); - } - - /** - * The calling thread will be suspended until an action is triggered. - * @param timeout for the periodic action. - * @param timer_args arguments for the timer action - */ - void loop(struct timespec& _timeout, const ActionRequest& trequest); - - void loop(time_t timeout, const ActionRequest& trequest) - { - struct timespec _timeout; - - _timeout.tv_sec = timeout; - _timeout.tv_nsec = 0; - - loop(_timeout, trequest); - } - - /** - * The calling thread will be suspended until an action is triggered. - * @param timeout for the periodic action, the timer action will recieve - * an "empty" ActionRequest. - */ - void loop(time_t timeout) - { - ActionRequest trequest(ActionRequest::TIMER); - - struct timespec _timeout; - - _timeout.tv_sec = timeout; - _timeout.tv_nsec = 0; - - loop(_timeout, trequest); - } - - void loop(struct timespec& _timeout) - { - ActionRequest trequest(ActionRequest::TIMER); - - loop(_timeout, trequest); - } - - /** - * The calling thread will be suspended until an action is triggered. No - * periodic action is defined. - */ - void loop() - { - ActionRequest trequest(ActionRequest::TIMER); - struct timespec _timeout; - - _timeout.tv_sec = 0; - _timeout.tv_nsec = 0; - - loop(_timeout, trequest); - } - - /** - * Register the calling object in this action manager. - * @param listener a pointer to the action listner - */ - void addListener(ActionListener * listener) - { - this->listener = listener; - }; - -private: - /** - * Queue of pending actions, processed in a FIFO manner - */ - std::queue actions; - - /** - * Action synchronization is implemented using the pthread library, - * with condition variable and its associated mutex - */ - pthread_mutex_t mutex; - pthread_cond_t cond; - - /** - * The listener notified by this manager - */ - ActionListener * listener; - - /** - * Function to lock the Manager mutex - */ - void lock() - { - pthread_mutex_lock(&mutex); - }; - - /** - * Function to unlock the Manager mutex - */ - void unlock() - { - pthread_mutex_unlock(&mutex); - }; - -}; - -#endif /*ACTION_MANAGER_H_*/ diff --git a/include/AddressRange.h b/include/AddressRange.h index d1862ebb179..78883b3d083 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -24,8 +24,6 @@ #include "PoolObjectSQL.h" #include "AddressRangePool.h" -using namespace std; - class VectorAttribute; /** @@ -70,14 +68,14 @@ class AddressRange * @param ob the type * @return the string */ - static string type_to_str(AddressType ob); + static std::string type_to_str(AddressType ob); /** * Return the string representation of an AddressType * @param ob the type * @return the string */ - static AddressType str_to_type(string& str_type); + static AddressType str_to_type(std::string& str_type); /** * Return true if the address range includes IPv4 addresses @@ -148,7 +146,7 @@ class AddressRange * implementation may contact an external IPAM to complete or validate * the AR allocation request. */ - virtual int from_vattr(VectorAttribute * attr, string& error_msg) = 0; + virtual int from_vattr(VectorAttribute * attr, std::string& error_msg) = 0; /** * Builds an Address Range from a vector attribute stored in the DB @@ -167,13 +165,13 @@ class AddressRange * @param vrs list of VRouter the user can access VNET usage info from. * A vector containing just -1 means all VRouters. */ - void to_xml(ostringstream &oss, const vector& vms, - const vector& vnets, const vector& vrs) const; + void to_xml(std::ostringstream &oss, const std::vector& vms, + const std::vector& vnets, const std::vector& vrs) const; /** * Same as above but without the LEASES section */ - void to_xml(ostringstream &oss) const; + void to_xml(std::ostringstream &oss) const; // ************************************************************************* @@ -190,7 +188,7 @@ class AddressRange * @return 0 if success */ int allocate_addr(PoolObjectSQL::ObjectType ot, int obid, - VectorAttribute * nic, const vector &inherit); + VectorAttribute * nic, const std::vector &inherit); /** * Returns the specific address by mac/ip if is not allocated. The NIC attr @@ -202,25 +200,25 @@ class AddressRange * @param inherit attributes to be added to the NIC attribute * @return 0 if success */ - int allocate_by_mac(const string& mac, PoolObjectSQL::ObjectType ot, - int obid, VectorAttribute * nic, const vector &inherit); + int allocate_by_mac(const std::string& mac, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, const std::vector &inherit); - int allocate_by_ip(const string& ip, PoolObjectSQL::ObjectType ot, - int obid, VectorAttribute * nic, const vector &inherit); + int allocate_by_ip(const std::string& ip, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, const std::vector &inherit); - int allocate_by_ip6(const string& ip6, PoolObjectSQL::ObjectType ot, - int obid, VectorAttribute * nic, const vector &inherit); + int allocate_by_ip6(const std::string& ip6, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, const std::vector &inherit); /** * Sets the given ip/mac on hold, the address is associated to a VM of * id -1. * @param ip/mac the ip to hold */ - int hold_by_mac(const string& mac); + int hold_by_mac(const std::string& mac); - int hold_by_ip(const string& ip); + int hold_by_ip(const std::string& ip); - int hold_by_ip6(const string& ip); + int hold_by_ip6(const std::string& ip); /** * Frees a previous allocated address, referenced by its MAC/IP address @@ -229,11 +227,11 @@ class AddressRange * @param mac/ip the MAC/IP address in string form * @return 0 if the address was freed */ - int free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); + int free_addr(PoolObjectSQL::ObjectType ot, int obid, const std::string& mac); - int free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + int free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const std::string& ip); - int free_addr_by_ip6(PoolObjectSQL::ObjectType ot, int id,const string& ip); + int free_addr_by_ip6(PoolObjectSQL::ObjectType ot, int id,const std::string& ip); /** * Frees all previous allocated address to the given object @@ -253,7 +251,7 @@ class AddressRange * @return the number of addresses freed */ int free_addr_by_range(PoolObjectSQL::ObjectType ot, int obid, - const string& mac, unsigned int rsize); + const std::string& mac, unsigned int rsize); /** * Adds the relevant AR definition attributes to the Security Group rule @@ -283,13 +281,13 @@ class AddressRange * @param ip/mac the firs ip in the Reservation * @return 0 on success */ - int reserve_addr_by_mac(int vid, unsigned int rsize, const string& mac, + int reserve_addr_by_mac(int vid, unsigned int rsize, const std::string& mac, AddressRange *rar); - int reserve_addr_by_ip(int vid, unsigned int rsize, const string& ip, + int reserve_addr_by_ip(int vid, unsigned int rsize, const std::string& ip, AddressRange *rar); - int reserve_addr_by_ip6(int vid, unsigned int rsize, const string& ip, + int reserve_addr_by_ip6(int vid, unsigned int rsize, const std::string& ip, AddressRange *rar); // ************************************************************************* @@ -333,7 +331,7 @@ class AddressRange * @param name of the attribute * @return the value of the attribute if found, empty otherwise */ - string get_attribute(const string& name) const + std::string get_attribute(const std::string& name) const { return attr->vector_value(name); } @@ -344,7 +342,7 @@ class AddressRange * @param value of the attribute * @return 0 on success */ - int get_attribute(const string& name, int& value) const + int get_attribute(const std::string& name, int& value) const { return attr->vector_value(name, value); } @@ -361,18 +359,18 @@ class AddressRange int update_attributes( VectorAttribute * vup, bool keep_restricted, - string& error_msg); + std::string& error_msg); /** * Helper function to initialize restricte attributes of an AddressRange */ - static void set_restricted_attributes(vector& ras); + static void set_restricted_attributes(std::vector& ras); /** * Get the security groups for this AR. * @return a reference to the security group set */ - const set& get_security_groups() const + const std::set& get_security_groups() const { return security_groups; } @@ -380,13 +378,11 @@ class AddressRange /** * Copy security groups into set */ - void get_security_groups(set& sgs) + void get_security_groups(std::set& sgs) const { - std::set::const_iterator it; - - for (it = security_groups.begin(); it != security_groups.end(); ++it) + for (auto sg : security_groups) { - sgs.insert(*it); + sgs.insert(sg); } } @@ -405,13 +401,14 @@ class AddressRange * rm_ar from AddressRangePool needs to access the internal representation * of the AR to remove it from the ARPool template. */ - friend int AddressRangePool::rm_ar(unsigned int ar_id, bool force, string& error_msg); + friend int AddressRangePool::rm_ar(unsigned int ar_id, bool force, + std::string& error_msg); /* * rm_ars from AddressRangePool needs to access the internal representation * of the AR to remove it from the ARPool template. */ - friend int AddressRangePool::rm_ars(string& error_msg); + friend int AddressRangePool::rm_ars(std::string& error_msg); protected: /** @@ -426,7 +423,7 @@ class AddressRange /** * Builds the AddressRange from its vector attribute representation */ - int from_attr(VectorAttribute * attr, string& error_msg); + int from_attr(VectorAttribute * attr, std::string& error_msg); /** * Builds an address request representation in XML form: @@ -443,7 +440,7 @@ class AddressRange * @param oss string stream to write the request to */ void addr_to_xml(unsigned int index, unsigned int size, - ostringstream& oss) const; + std::ostringstream& oss) const; /** * Check if the given MAC is valid for this address range by verifying: @@ -456,7 +453,8 @@ class AddressRange * * @return true if the MAC is valid */ - bool is_valid_mac(unsigned int& index, const string& mac_s, bool check_free); + bool is_valid_mac(unsigned int& index, const std::string& mac_s, + bool check_free); /** * Check if the given IP is valid for this address range by verifying: @@ -470,7 +468,8 @@ class AddressRange * * @return true if the IP is valid */ - bool is_valid_ip(unsigned int& index, const string& ip_s, bool check_free); + bool is_valid_ip(unsigned int& index, const std::string& ip_s, + bool check_free) const; /** * Check if the given IP is valid for this address range by verifying: @@ -484,7 +483,8 @@ class AddressRange * * @return true if the IP is valid */ - bool is_valid_ip6(unsigned int& index, const string& ip_s, bool check_free); + bool is_valid_ip6(unsigned int& index, const std::string& ip_s, + bool check_free); /* ---------------------------------------------------------------------- */ /* Implementation specific address management interface */ @@ -497,7 +497,8 @@ class AddressRange * * @return 0 if success */ - virtual int allocate_addr(unsigned int ix, unsigned int sz, string& mg) = 0; + virtual int allocate_addr(unsigned int ix, unsigned int sz, + std::string& mg) = 0; /** * Gets a range of free addresses * @param index the first address in the range @@ -506,7 +507,8 @@ class AddressRange * * @return 0 if success */ - virtual int get_addr(unsigned int& index, unsigned int sz, string& msg) = 0; + virtual int get_addr(unsigned int& index, unsigned int sz, + std::string& msg) = 0; /** * Sets the given address (by index) as free @@ -515,7 +517,7 @@ class AddressRange * * @return 0 if success */ - virtual int free_addr(unsigned int index, string& msg) = 0; + virtual int free_addr(unsigned int index, std::string& msg) = 0; /* ---------------------------------------------------------------------- */ /* Allocated addresses */ @@ -531,7 +533,7 @@ class AddressRange * * Address = First Address + index */ - map allocated; + std::map allocated; private: /* ---------------------------------------------------------------------- */ @@ -542,27 +544,27 @@ class AddressRange * @param mac in string form 00:02:01:02:03:04 * @return 0 on success */ - int mac_to_i(string mac, unsigned int i_mac[]) const; + int mac_to_i(std::string mac, unsigned int i_mac[]) const; /** * MAC to string * @param mac in array form */ - string mac_to_s(const unsigned int mac[]) const; + std::string mac_to_s(const unsigned int mac[]) const; /** * IP version 4 to binary (32 bits) * @param ip in string form 192.168.0.2 * @return 0 on success */ - int ip_to_i(const string& _ip, unsigned int& i_ip) const; + int ip_to_i(const std::string& _ip, unsigned int& i_ip) const; /** * IP version 6 to binary (32 bits) * @param ip string form 2a00:1bc0:b001:A::3 * @return 0 on success */ - int ip6_to_i(const string& _ip, unsigned int i_ip[]) const; + int ip6_to_i(const std::string& _ip, unsigned int i_ip[]) const; /** * IP version 4 to dot notation @@ -570,14 +572,14 @@ class AddressRange * @param i_ip Numeric (32 bits) IP * @return dot notation */ - string ip_to_s(unsigned int i_ip) const; + std::string ip_to_s(unsigned int i_ip) const; /** * IPv6 64bits prefix conversion * @param prefix in string form 2a00:1bc0:b001:A:: * @return 0 on success */ - int prefix6_to_i(const string& prefix, unsigned int ip[]) const; + int prefix6_to_i(const std::string& prefix, unsigned int ip[]) const; /** * IPv6 to string @@ -587,9 +589,9 @@ class AddressRange * @return 0 on success */ int ip6_to_s(const unsigned int prefix[], const unsigned int mac[], - string& ip6_s) const; + std::string& ip6_s) const; - int ip6_to_s(const unsigned int ip6_i[], string& ip6_s) const; + int ip6_to_s(const unsigned int ip6_i[], std::string& ip6_s) const; /* ---------------------------------------------------------------------- */ /* NIC setup functions */ @@ -629,7 +631,8 @@ class AddressRange * @param addr_index internal index for the lease * @param nic attribute of a VMTemplate */ - void set_vnet(VectorAttribute *nic, const vector &inherit) const; + void set_vnet(VectorAttribute *nic, + const std::vector &inherit) const; /* ---------------------------------------------------------------------- */ /* Address index map helper functions */ @@ -647,7 +650,7 @@ class AddressRange * generated by allocated_to_attr() * @return 0 on success */ - int attr_to_allocated(const string& allocated_s); + int attr_to_allocated(const std::string& allocated_s); /** * Adds a new allocated address to the map. Updates the ALLOCATED attribute @@ -666,10 +669,10 @@ class AddressRange * @return 0 if success */ void allocate_by_index(unsigned int index, - PoolObjectSQL::ObjectType ot, - int obid, - VectorAttribute* nic, - const vector& inherit); + PoolObjectSQL::ObjectType ot, + int obid, + VectorAttribute* nic, + const std::vector& inherit); /** * Frees an address from the map. Updates the ALLOCATED attribute @@ -696,7 +699,7 @@ class AddressRange * @param error_msg if any * @return 0 on success */ - int init_ipv4(string& error_msg); + int init_ipv4(std::string& error_msg); /** * Function to parse the IPv6 attributes ("GLOBAL_PREFIX" and "ULA_PREFIX") @@ -704,7 +707,7 @@ class AddressRange * @param error_msg if any * @return 0 on success */ - int init_ipv6(string& error_msg); + int init_ipv6(std::string& error_msg); /** * Function to parse the IPv6 attributes no slaac ("IP6") for IP6_STATIC @@ -712,19 +715,19 @@ class AddressRange * @param error_msg if any * @return 0 on success */ - int init_ipv6_static(string& error_msg); + int init_ipv6_static(std::string& error_msg); /** * Function to parse the MAC attributes ("MAC") for all AR types * @param error_msg if any * @return 0 on success */ - int init_mac(string& error_msg); + int init_mac(std::string& error_msg); /** * Checks for restricted attributes, returns the first one found */ - bool check(string& rs_attr) const; + bool check(std::string& rs_attr) const; /** * Deletes all restricted attributes @@ -762,27 +765,27 @@ class AddressRange /** * First MAC in the range */ - unsigned int mac[2]; + unsigned int mac[2] = {0}; /** * Binary representation of the IPv6 address global unicast prefix */ - unsigned int global6[2]; + unsigned int global6[2] = {0}; /** * Binary representation of the IPv6 address site unicast prefix */ - unsigned int ula6[2]; + unsigned int ula6[2] = {0}; /** * Binary representation of the first IPv6 address in the AR. No SLAAC ARs */ - unsigned int ip6[4]; + unsigned int ip6[4] = {0}; /** * Security Group IDs for this Address Range */ - set security_groups; + std::set security_groups; /** * The Address Range attributes as a Template VectorAttribute. This is @@ -801,7 +804,7 @@ class AddressRange /** * The restricted attributes from oned.conf */ - static set restricted_attributes; + static std::set restricted_attributes; /** * Attributes to be process for Security Group rules diff --git a/include/AddressRangeIPAM.h b/include/AddressRangeIPAM.h index 9b61038b67a..a52ee28ad5f 100644 --- a/include/AddressRangeIPAM.h +++ b/include/AddressRangeIPAM.h @@ -20,7 +20,6 @@ #include #include "AddressRange.h" -#include "AddressRangePool.h" class VectorAttribute; @@ -50,7 +49,8 @@ class AddressRangeIPAM : public AddressRange * * @return 0 if success */ - int allocate_addr(unsigned int index, unsigned int rsize, string& error_msg); + int allocate_addr(unsigned int index, unsigned int rsize, + std::string& error_msg); /** * Gets a range of free addresses @@ -60,7 +60,8 @@ class AddressRangeIPAM : public AddressRange * * @return 0 if success */ - int get_addr(unsigned int& index, unsigned int rsize, string& error_msg); + int get_addr(unsigned int& index, unsigned int rsize, + std::string& error_msg); /** * Sets the given address (by index) as free @@ -69,7 +70,7 @@ class AddressRangeIPAM : public AddressRange * * @return 0 if success */ - int free_addr(unsigned int index, string& msg); + int free_addr(unsigned int index, std::string& msg); }; #endif diff --git a/include/AddressRangeInternal.h b/include/AddressRangeInternal.h index ddda4f76818..e851a862209 100644 --- a/include/AddressRangeInternal.h +++ b/include/AddressRangeInternal.h @@ -20,7 +20,6 @@ #include #include "AddressRange.h" -#include "AddressRangePool.h" class VectorAttribute; @@ -53,7 +52,8 @@ class AddressRangeInternal : public AddressRange * * @return 0 if success */ - int allocate_addr(unsigned int index, unsigned int rsize, string& error_msg) + int allocate_addr(unsigned int index, unsigned int rsize, + std::string& error_msg) { return 0; } @@ -66,7 +66,8 @@ class AddressRangeInternal : public AddressRange * * @return 0 if success */ - int get_addr(unsigned int& index, unsigned int rsize, string& error_msg) + int get_addr(unsigned int& index, unsigned int rsize, + std::string& error_msg) { if ( rsize == 1 ) { @@ -83,7 +84,7 @@ class AddressRangeInternal : public AddressRange * * @return 0 if success */ - int free_addr(unsigned int index, string& msg) + int free_addr(unsigned int index, std::string& msg) { return 0; }; @@ -109,7 +110,7 @@ class AddressRangeInternal : public AddressRange * @param msg with error description if any * @return 0 on success -1 otherwise */ - int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg); + int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg) const; }; #endif diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index f335ed831e5..94cf2f323ed 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -22,15 +22,12 @@ #include #include -#include - #include "VirtualNetworkTemplate.h" #include "PoolObjectSQL.h" class VectorAttribute; class AddressRange; -using namespace std; class AddressRangePool { @@ -50,7 +47,7 @@ class AddressRangePool * @param error_msg describing the error * @return 0 on success */ - int from_vattr(VectorAttribute * ar, string& error_msg); + int from_vattr(VectorAttribute * ar, std::string& error_msg); /** * Builds the address range set from its XML representation. This function @@ -71,14 +68,14 @@ class AddressRangePool * @param force force remove, even if active leases exists * @return 0 on success, -1 if not exists or has used addresses */ - int rm_ar(unsigned int ar_id, bool force, string& error_msg); + int rm_ar(unsigned int ar_id, bool force, std::string& error_msg); /** * Removes all address ranges from the pool if it does not contain any used * leases * @return 0 on success, -1 if not exists or has used addresses */ - int rm_ars(string& error_msg); + int rm_ars(std::string& error_msg); /** * Updates the given address ranges @@ -90,8 +87,8 @@ class AddressRangePool * the reason. * @return 0 on success */ - int update_ar(vector ars, bool keep_restricted, - string& error_msg); + int update_ar(std::vector ars, bool keep_restricted, + std::string& error_msg); /** * Allocates a new *empty* address range. It is not added to the pool as it * needs to be initialized. Only the AR_ID is set. @@ -99,7 +96,7 @@ class AddressRangePool * IPAM... * @return the new address range. */ - AddressRange * allocate_ar(const string& ipam_mad); + AddressRange * allocate_ar(const std::string& ipam_mad); /** * Adds a new address range to the pool. It should be allocated by the @@ -122,7 +119,7 @@ class AddressRangePool * @return 0 if success */ int allocate_addr(PoolObjectSQL::ObjectType ot, int obid, - VectorAttribute * nic, const vector &inherit); + VectorAttribute * nic, const std::vector &inherit); /** * Allocates an address in a suitable address range from the pool by mac/ip @@ -133,14 +130,17 @@ class AddressRangePool * @param inherit attributes to be added to the NIC * @return 0 if success */ - int allocate_by_mac(const string &mac, PoolObjectSQL::ObjectType ot, int obid, - VectorAttribute * nic, const vector &inherit); + int allocate_by_mac(const std::string &mac, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, + const std::vector &inherit); - int allocate_by_ip(const string &ip, PoolObjectSQL::ObjectType ot, int obid, - VectorAttribute * nic, const vector &inherit); + int allocate_by_ip(const std::string &ip, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, + const std::vector &inherit); - int allocate_by_ip6(const string &ip, PoolObjectSQL::ObjectType ot, int obid, - VectorAttribute * nic, const vector &inherit); + int allocate_by_ip6(const std::string &ip, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, + const std::vector &inherit); /** * Holds an address from the specified address range. @@ -148,22 +148,22 @@ class AddressRangePool * @param mac/ip the mac/ip to hold * @return 0 on success */ - int hold_by_mac(unsigned int arid, const string& mac); + int hold_by_mac(unsigned int arid, const std::string& mac); - int hold_by_ip(unsigned int arid, const string& ip); + int hold_by_ip(unsigned int arid, const std::string& ip); - int hold_by_ip6(unsigned int arid, const string& ip); + int hold_by_ip6(unsigned int arid, const std::string& ip); /** * Holds an address from the first address range containing the MAC * @param mac/ip the mac/ip to hold * @return 0 on success */ - int hold_by_mac(const string& mac); + int hold_by_mac(const std::string& mac); - int hold_by_ip(const string& ip); + int hold_by_ip(const std::string& ip); - int hold_by_ip6(const string& ip); + int hold_by_ip6(const std::string& ip); /** * Frees the given address by MAC/IP on the given address range @@ -173,13 +173,13 @@ class AddressRangePool * @param mac/ip the specific MAC/IP address requested */ void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid, - const string& mac); + const std::string& mac); void free_addr_by_ip(unsigned int arid, PoolObjectSQL::ObjectType ot, - int obid, const string& ip); + int obid, const std::string& ip); void free_addr_by_ip6(unsigned int arid, PoolObjectSQL::ObjectType ot, - int obid, const string& ip); + int obid, const std::string& ip); /** * Frees the given address by MAC/IP from all address ranges containing @@ -188,11 +188,14 @@ class AddressRangePool * @param obid the id of the object requesting the address * @param mac/ip the specific MAC/IP address requested */ - void free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); + void free_addr(PoolObjectSQL::ObjectType ot, int obid, + const std::string& mac); - void free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + void free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, + const std::string& ip); - void free_addr_by_ip6(PoolObjectSQL::ObjectType ot, int id,const string& ip); + void free_addr_by_ip6(PoolObjectSQL::ObjectType ot, int id, + const std::string& ip); /** * Frees all the addressed owned by the given object @@ -211,7 +214,7 @@ class AddressRangePool * @param rsize size of the address range */ int free_addr_by_range(unsigned int arid, PoolObjectSQL::ObjectType ot, - int obid, const string& mac, unsigned int rsize); + int obid, const std::string& mac, unsigned int rsize); /** * From a Security Group rule that uses this vnet, creates a new rule @@ -222,8 +225,8 @@ class AddressRangePool * be deleted by the caller */ void process_security_rule( - VectorAttribute * rule, - vector &new_rules); + VectorAttribute * rule, + std::vector &new_rules); // ************************************************************************* // Address reservation @@ -260,13 +263,13 @@ class AddressRangePool * @return 0 on success */ int reserve_addr_by_mac(int vid, unsigned int rsize, unsigned int ar_id, - const string& mac, AddressRange *rar); + const std::string& mac, AddressRange *rar); int reserve_addr_by_ip(int vid, unsigned int rsize, unsigned int ar_id, - const string& ip, AddressRange *rar); + const std::string& ip, AddressRange *rar); int reserve_addr_by_ip6(int vid, unsigned int rsize, unsigned int ar_id, - const string& ip, AddressRange *rar); + const std::string& ip, AddressRange *rar); // ************************************************************************* @@ -299,7 +302,7 @@ class AddressRangePool * @param value of the attribute * @param ar_id to get the attribute from */ - void get_attribute(const string& name, string& value, int ar_id) const; + void get_attribute(const std::string& name, std::string& value, int ar_id) const; /** * Gets an attribute from the Address Range, int version @@ -308,19 +311,19 @@ class AddressRangePool * @param ar_id to get the attribute from * @return 0 on success */ - int get_attribute(const string& name, int& value, int ar_id) const; + int get_attribute(const std::string& name, int& value, int ar_id) const; /** * Gets a reference to a the security group set of an AR * @return a reference to the security group set or empty set if error */ - const set& get_security_groups(int ar_id) const; + const std::set& get_security_groups(int ar_id) const; /** * Gets a the security group set of all ARs * @param sgs set with all the SG ids */ - void get_all_security_groups(set& sgs); + void get_all_security_groups(std::set& sgs) const; /** * Generate a XML representation of the Address Range Pool @@ -334,8 +337,11 @@ class AddressRangePool * A vector containing just -1 means all VRouters. * @return the string with the XML */ - string& to_xml(string& sstream, bool extended, const vector& vms, - const vector& vnets, const vector& vrs) const; + std::string& to_xml(std::string& sstream, + bool extended, + const std::vector& vms, + const std::vector& vnets, + const std::vector& vrs) const; /** * Encrypt all secret attributes @@ -368,7 +374,7 @@ class AddressRangePool /** * Map to access each range */ - map ar_pool; + std::map ar_pool; /** * Used addresses @@ -383,7 +389,7 @@ class AddressRangePool * @param ar_id for the AddressRange * @return the new address range. */ - AddressRange * allocate_ar(const string& ipam_mad, unsigned int ar_id); + AddressRange * allocate_ar(const std::string& ipam_mad, unsigned int ar_id); }; #endif diff --git a/include/Attribute.h b/include/Attribute.h index eebef005aba..0ddc72b9df0 100644 --- a/include/Attribute.h +++ b/include/Attribute.h @@ -24,8 +24,6 @@ #include "NebulaUtil.h" -using namespace std; - /** * Attribute base class for name-value pairs. This class provides a generic * interface to implement @@ -34,7 +32,7 @@ class Attribute { public: - Attribute(const string& aname):attribute_name(aname) + Attribute(const std::string& aname):attribute_name(aname) { transform ( attribute_name.begin(), @@ -66,17 +64,16 @@ class Attribute * Gets the name of the attribute. * @return the attribute name */ - const string& name() const + const std::string& name() const { return attribute_name; }; /** - * Marshall the attribute in a single string. The string MUST be freed - * by the calling function. - * @return a string (allocated in the heap) holding the attribute value. + * Marshall the attribute in a single string. + * @return a string holding the attribute value. */ - virtual string * marshall(const char * _sep = 0) const = 0; + virtual std::string marshall(const char * _sep = 0) const = 0; /** * Write the attribute using a simple XML format. The string MUST be freed @@ -92,7 +89,7 @@ class Attribute /** * Builds a new attribute from a string. */ - virtual void unmarshall(const string& sattr, const char * _sep = 0) = 0; + virtual void unmarshall(const std::string& sattr, const char * _sep = 0) = 0; /** * Returns the attribute type @@ -107,19 +104,19 @@ class Attribute /** * Encrypt all secret attributes */ - virtual void encrypt(const string& one_key, const set& eas) {}; + virtual void encrypt(const std::string& one_key, const std::set& eas) {}; /** * Decrypt all secret attributes */ - virtual void decrypt(const string& one_key, const set& eas) {}; + virtual void decrypt(const std::string& one_key, const std::set& eas) {}; protected: /** * The attribute name. */ - string attribute_name; + std::string attribute_name; }; /* -------------------------------------------------------------------------- */ @@ -134,9 +131,9 @@ class SingleAttribute : public Attribute { public: - SingleAttribute(const string& name):Attribute(name){}; + SingleAttribute(const std::string& name):Attribute(name){}; - SingleAttribute(const string& name, const string& value): + SingleAttribute(const std::string& name, const std::string& value): Attribute(name),attribute_value(value){}; SingleAttribute(const SingleAttribute& sa):Attribute(sa.attribute_name) @@ -149,23 +146,18 @@ class SingleAttribute : public Attribute /** * Returns the attribute value, a string. */ - const string& value() const + const std::string& value() const { return attribute_value; }; /** - * Marshall the attribute in a single string. The string MUST be freed - * by the calling function. - * @return a string (allocated in the heap) holding the attribute value. + * Marshall the attribute in a single string. + * @return a string holding the attribute value. */ - string * marshall(const char * _sep = 0) const override + std::string marshall(const char * _sep = 0) const override { - string * rs = new string; - - *rs = attribute_value; - - return rs; + return attribute_value; }; /** @@ -203,7 +195,7 @@ class SingleAttribute : public Attribute /** * Builds a new attribute from a string. */ - void unmarshall(const string& sattr, const char * _sep = 0) override + void unmarshall(const std::string& sattr, const char * _sep = 0) override { attribute_value = sattr; }; @@ -211,7 +203,7 @@ class SingleAttribute : public Attribute /** * Replaces the attribute value from a string. */ - void replace(const string& sattr) + void replace(const std::string& sattr) { attribute_value = sattr; }; @@ -235,16 +227,18 @@ class SingleAttribute : public Attribute /** * Encrypt all secret attributes */ - virtual void encrypt(const string& one_key, const set& eas) override; + void encrypt(const std::string& one_key, + const std::set& eas) override; /** * Decrypt all secret attributes */ - virtual void decrypt(const string& one_key, const set& eas) override; + void decrypt(const std::string& one_key, + const std::set& eas) override; private: - string attribute_value; + std::string attribute_value; }; /* -------------------------------------------------------------------------- */ @@ -259,9 +253,10 @@ class VectorAttribute : public Attribute { public: - VectorAttribute(const string& name):Attribute(name){}; + VectorAttribute(const std::string& name):Attribute(name){}; - VectorAttribute(const string& name,const map& value): + VectorAttribute(const std::string& name, + const std::map& value): Attribute(name),attribute_value(value){}; VectorAttribute(const VectorAttribute& va):Attribute(va.attribute_name) @@ -279,7 +274,7 @@ class VectorAttribute : public Attribute /** * Returns the attribute value, a string. */ - const map& value() const + const std::map& value() const { return attribute_value; }; @@ -290,7 +285,7 @@ class VectorAttribute : public Attribute * * @return the value of the attribute if found, empty otherwise */ - string vector_value(const string& name) const; + std::string vector_value(const std::string& name) const; /** * Returns the value of the given element of the VectorAttribute @@ -301,11 +296,9 @@ class VectorAttribute : public Attribute * @return 0 on success, -1 otherwise */ template - int vector_value(const string& name, T& value) const + int vector_value(const std::string& name, T& value) const { - map::const_iterator it; - - it = attribute_value.find(name); + auto it = attribute_value.find(name); if ( it == attribute_value.end() ) { @@ -317,7 +310,7 @@ class VectorAttribute : public Attribute return -1; } - istringstream iss(it->second); + std::istringstream iss(it->second); iss >> value; if (iss.fail() || !iss.eof()) @@ -337,7 +330,9 @@ class VectorAttribute : public Attribute * @param default_value used if element is invalid */ template - void vector_value(const string& name, T& value, const T& default_value) const + void vector_value(const std::string& name, + T& value, + const T& default_value) const { if (vector_value(name, value) != 0) { @@ -345,9 +340,9 @@ class VectorAttribute : public Attribute } } - int vector_value(const string& name, string& value) const; + int vector_value(const std::string& name, std::string& value) const; - int vector_value(const string& name, bool& value) const; + int vector_value(const std::string& name, bool& value) const; /** * Returns the value of the given element of the VectorAttribute @@ -359,11 +354,9 @@ class VectorAttribute : public Attribute * @return the value in string form on success, "" otherwise */ template - string vector_value_str(const string& name, T& value) const + std::string vector_value_str(const std::string& name, T& value) const { - map::const_iterator it; - - it = attribute_value.find(name); + auto it = attribute_value.find(name); if ( it == attribute_value.end() ) { @@ -375,7 +368,7 @@ class VectorAttribute : public Attribute return ""; } - istringstream iss(it->second); + std::istringstream iss(it->second); iss >> value; if (iss.fail() || !iss.eof()) @@ -392,7 +385,7 @@ class VectorAttribute : public Attribute * "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N". * @return a string (allocated in the heap) holding the attribute value. */ - string * marshall(const char * _sep = 0) const override; + std::string marshall(const char * _sep = 0) const override; /** * Write the attribute using a simple XML format: @@ -416,12 +409,12 @@ class VectorAttribute : public Attribute * Builds a new attribute from a string of the form: * "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N". */ - void unmarshall(const string& sattr, const char * _sep = 0) override; + void unmarshall(const std::string& sattr, const char * _sep = 0) override; /** * Replace the value of the given attribute with the provided map */ - void replace(const map& attr); + void replace(const std::map& attr); /** * The attributes from vattr will be copied to this vector @@ -435,16 +428,16 @@ class VectorAttribute : public Attribute * Replace the value of the given vector attribute */ template - void replace(const string& name, T value) + void replace(const std::string& name, T value) { - ostringstream oss; + std::ostringstream oss; oss << value; replace(name, oss.str()); } - void replace(const string& name, bool value) + void replace(const std::string& name, bool value) { if (value == true) { @@ -456,13 +449,13 @@ class VectorAttribute : public Attribute } } - void replace(const string& name, const string& value); + void replace(const std::string& name, const std::string& value); /** * Removes given the vector attribute * @param name of the vector attribute */ - void remove(const string& name); + void remove(const std::string& name); /** * Returns the attribute type @@ -491,7 +484,7 @@ class VectorAttribute : public Attribute /** * @return true if the vector attribute contains no values */ - bool empty() + bool empty() const { return attribute_value.empty(); } @@ -499,12 +492,14 @@ class VectorAttribute : public Attribute /** * Encrypt all secret attributes */ - virtual void encrypt(const string& one_key, const set& eas) override; + void encrypt(const std::string& one_key, + const std::set& eas) override; /** * Decrypt all secret attributes */ - virtual void decrypt(const string& one_key, const set& eas) override; + void decrypt(const std::string& one_key, + const std::set& eas) override; private: @@ -512,7 +507,7 @@ class VectorAttribute : public Attribute static const int magic_sep_size; - map attribute_value; + std::map attribute_value; }; #endif /*ATTRIBUTE_H_*/ diff --git a/include/AuthManager.h b/include/AuthManager.h index 9021d335efa..dd084e77733 100644 --- a/include/AuthManager.h +++ b/include/AuthManager.h @@ -19,87 +19,28 @@ #include -#include "MadManager.h" #include "NebulaLog.h" -#include "ActionManager.h" -#include "AuthManagerDriver.h" -#include "PoolObjectSQL.h" - -using namespace std; +#include "Listener.h" +#include "ProtocolMessages.h" +#include "DriverManager.h" //Forward definitions class AuthRequest; -class PoolObjectAuth; -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ -class AMAction : public ActionRequest -{ -public: - enum Actions - { - AUTHENTICATE, - AUTHORIZE - }; - - AMAction(Actions a, AuthRequest *r):ActionRequest(ActionRequest::USER), - _action(a), _request(r){}; - - AMAction(const AMAction& o):ActionRequest(o._type), _action(o._action), - _request(o._request){}; - - Actions action() const - { - return _action; - } - - AuthRequest * request() const - { - return _request; - } - - ActionRequest * clone() const - { - return new AMAction(*this); - } - -private: - Actions _action; - - AuthRequest * _request; -}; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -extern "C" void * authm_action_loop(void *arg); - -class AuthManager : public MadManager, public ActionListener +class AuthManager : + public DriverManager>, + public Listener { public: AuthManager( - time_t timer, - vector& _mads): - MadManager(_mads), timer_period(timer) - { - am.addListener(this); - }; - - ~AuthManager(){}; - - /** - * Triggers specific actions to the Auth Manager. This function - * wraps the ActionManager trigger function. - * @param action the Auth Manager action - * @param request an auth request - */ - void trigger(AMAction::Actions action, AuthRequest* request) + time_t timer, + const std::string& mads_location): + DriverManager(mads_location), + Listener("Authorization Manager"), + timer_thread(timer, [this](){timer_action();}) { - AMAction auth_ar(action, request); - - am.trigger(auth_ar); } /** @@ -110,30 +51,11 @@ class AuthManager : public MadManager, public ActionListener */ int start(); - /** - * - */ - void finalize() - { - am.finalize(); - }; - /** * Loads Virtual Machine Manager Mads defined in configuration file - * @param uid of the user executing the driver. When uid is 0 the nebula - * identity will be used. Otherwise the Mad will be loaded through the - * sudo application. + * @param _mads configuration of drivers */ - int load_mads(int uid); - - /** - * Gets the thread identification. - * @return pthread_t for the manager thread (that in the action loop). - */ - pthread_t get_thread_id() const - { - return authm_thread; - }; + int load_drivers(const std::vector& _mads); /** * Returns true if there is an authorization driver enabled @@ -143,33 +65,38 @@ class AuthManager : public MadManager, public ActionListener bool is_authz_enabled() { return authz_enabled; - }; + } -private: /** - * Thread id for the Transfer Manager + * This function authenticates a user */ - pthread_t authm_thread; + void trigger_authenticate(AuthRequest& ar); /** - * Action engine for the Manager + * This function authorizes a user request */ - ActionManager am; + void trigger_authorize(AuthRequest& ar); +private: /** - * Timer for the Manager (periocally triggers timer action) + * Timer action async execution */ - time_t timer_period; + Timer timer_thread; /** * Generic name for the Auth driver */ - static const char * auth_driver_name; + static const char * auth_driver_name; /** * True if there is an authorization driver enabled */ - bool authz_enabled; + bool authz_enabled; + + /** + * + */ + static const int drivers_timeout = 10; /** * Returns a pointer to a Auth Manager driver. @@ -178,13 +105,10 @@ class AuthManager : public MadManager, public ActionListener * @return the Auth driver with attribute name equal to value * or 0 in not found */ - const AuthManagerDriver * get( - const string& name, - const string& value) + const Driver * get(const std::string& name) const { - return static_cast - (MadManager::get(0,name,value)); - }; + return DriverManager::get_driver(name); + } /** * Returns a pointer to a Auth Manager driver. The driver is @@ -193,46 +117,43 @@ class AuthManager : public MadManager, public ActionListener * @return the TM driver owned by uid with attribute name equal to value * or 0 in not found */ - const AuthManagerDriver * get() + const Driver * get() const { - string name("NAME"); + return DriverManager::get_driver(auth_driver_name); + } - return static_cast - (MadManager::get(0,name,auth_driver_name)); - }; + // ------------------------------------------------------------------------- + // Protocol implementation, procesing messages from driver + // ------------------------------------------------------------------------- + /** + * + */ + static void _undefined(std::unique_ptr msg); /** - * This function authenticates a user + * */ - void authenticate_action(AuthRequest * ar); + void _authorize(std::unique_ptr msg); /** - * This function authorizes a user request + * */ - void authorize_action(AuthRequest * ar); + void _authenticate(std::unique_ptr msg); /** - * Function to execute the Manager action loop method within a new pthread - * (requires C linkage) + * */ - friend void * authm_action_loop(void *arg); + static void _log(std::unique_ptr msg); // ------------------------------------------------------------------------- // Action Listener interface // ------------------------------------------------------------------------- - void timer_action(const ActionRequest& ar) + void timer_action() { check_time_outs_action(); - }; - - void finalize_action(const ActionRequest& ar) - { - NebulaLog::log("AuM",Log::INFO,"Stopping Authorization Manager..."); - - MadManager::stop(); - }; + } - void user_action(const ActionRequest& ar); + void finalize_action() override; }; #endif /*AUTH_MANAGER_H*/ diff --git a/include/AuthManagerDriver.h b/include/AuthManagerDriver.h deleted file mode 100644 index 08ac656148d..00000000000 --- a/include/AuthManagerDriver.h +++ /dev/null @@ -1,101 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#ifndef AUTH_MANAGER_DRIVER_H_ -#define AUTH_MANAGER_DRIVER_H_ - -#include -#include -#include - -#include "Mad.h" - -using namespace std; - -//Forward definition of the AuthManager Class -class AuthManager; - -/** - * AuthManagerDriver provides a base class to implement TM - * Drivers. This class implements the protocol and recover functions - * from the Mad interface. - */ -class AuthManagerDriver : public Mad -{ -public: - - AuthManagerDriver( - int userid, - const map& attrs, - bool sudo, - AuthManager * _authm): - Mad(userid,attrs,sudo), authm(_authm){}; - - virtual ~AuthManagerDriver(){}; - - /** - * Implements the VM Manager driver protocol. - * @param message the string read from the driver - */ - void protocol(const string& message) const; - - /** - * Re-starts the driver - */ - void recover(); - -private: - friend class AuthManager; - - /** - * The AuthManager to notify results. - */ - AuthManager * authm; - - /** - * Sends an authorization request to the MAD: - * "AUTHORIZE OPERATION_ID USER_ID REQUEST1 REQUEST2..." - * @param oid an id to identify the request. - * @param uid the user id. - * @param requests space separated list of requests in the form OP:OB:ID - * @param acl is the authorization result using the ACL engine for - * this request - */ - void authorize(int oid, int uid, const string& requests, bool acl) const; - - /** - * Sends an authorization request to the MAD: - * "AUTHENTICATE REQUEST_ID USER_ID USER_NAME PASSWORD XMLRPC_TOKEN" - * @param oid an id to identify the request. - * @param uid the user id. - * @param auth_driver - * @param username - * @param password - * @param session token from the xml-rpc parameter - */ - void authenticate(int oid, - int uid, - const string& auth_driver, - const string& username, - const string& password, - const string& session) const; -}; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -#endif /*AUTH_MANAGER_DRIVER_H_*/ - diff --git a/include/AuthRequest.h b/include/AuthRequest.h index 237cf9dc017..24dbb66d8fa 100644 --- a/include/AuthRequest.h +++ b/include/AuthRequest.h @@ -17,17 +17,14 @@ #ifndef AUTH_REQUEST_H_ #define AUTH_REQUEST_H_ -#include #include -#include "ActionManager.h" #include "PoolObjectAuth.h" -#include "AuthManager.h" #include "NebulaUtil.h" #include "SyncRequest.h" -using namespace std; +class AuthManager; /** * The AuthRequest class is used to pass an Authorization or Authentication @@ -37,7 +34,11 @@ using namespace std; class AuthRequest : public SyncRequest { public: - AuthRequest(int _uid, set _gids): uid(_uid),gids(_gids),self_authorize(true){}; + AuthRequest(int _uid, const std::set& _gids) + : uid(_uid) + , gids(_gids) + , self_authorize(true) + {} ~AuthRequest(){}; @@ -57,7 +58,7 @@ class AuthRequest : public SyncRequest NONE = 0x0LL }; - static string operation_to_str(Operation op) + static std::string operation_to_str(Operation op) { switch (op) { @@ -75,7 +76,7 @@ class AuthRequest : public SyncRequest return ""; }; - static Operation str_to_operation(string str) + static Operation str_to_operation(std::string str) { if (str == "USE") return USE; else if (str == "MANAGE") return MANAGE; @@ -88,10 +89,10 @@ class AuthRequest : public SyncRequest * Sets the challenge to authenticate an user * @param challenge a driver specific authentication challenge */ - void add_authenticate(const string &_driver, - const string &_username, - const string &_password, - const string &_session) + void add_authenticate(const std::string &_driver, + const std::string &_username, + const std::string &_password, + const std::string &_session) { username = _username; password = _password; @@ -110,7 +111,8 @@ class AuthRequest : public SyncRequest * @param type of the object to be created * @param txml template of the new object */ - void add_create_auth(int uid, int gid, PoolObjectSQL::ObjectType type, const string& txml) + void add_create_auth(int uid, int gid, PoolObjectSQL::ObjectType type, + const std::string& txml) { PoolObjectAuth perms; //oid & gid set to -1 @@ -140,14 +142,14 @@ class AuthRequest : public SyncRequest * @return a space separated list of auth requests, or an empty string if * no auth requests were added */ - string get_auths() + std::string get_auths() const { - ostringstream oss; + std::ostringstream oss; unsigned int i; if ( auths.empty() ) { - return string(); + return std::string(); } for (i=0; i gids; + std::set gids; /** * Username to authenticate the user */ - string username; + std::string username; /** * User password to authenticate the user */ - string password; + std::string password; /** * Authentication token as sent in the XML-RPC call (user:session) */ - string session; + std::string session; /** * Authentication driver to be used with this request */ - string driver; + std::string driver; /** * A list of authorization requests */ - vector auths; + std::vector auths; /** * Plain authorization for the request @@ -230,7 +232,7 @@ class AuthRequest : public SyncRequest */ void add_auth(Operation op, const PoolObjectAuth& ob_perms, - string ob_template); + const std::string& ob_template); }; #endif diff --git a/include/BitMap.h b/include/BitMap.h index 13b236749e8..cdc243a18ba 100644 --- a/include/BitMap.h +++ b/include/BitMap.h @@ -21,6 +21,7 @@ #include "Attribute.h" #include "Callbackable.h" +#include "SSLUtil.h" class SqlDB; @@ -92,18 +93,16 @@ class BitMap : public Callbackable */ int select(int _id, SqlDB * db) { - std::string * uzbs; + std::string uzbs; id = _id; - if ( select(db, &uzbs) != 0 ) + if ( select(db, uzbs) != 0 ) { return -1; } - bs = new std::bitset(*uzbs); - - delete uzbs; + bs = new std::bitset(uzbs); return 0; } @@ -134,7 +133,7 @@ class BitMap : public Callbackable * @param bit the bit number reserved * @return -1 in case of error */ - int get(unsigned int hint, unsigned int& bit) + int get(unsigned int hint, unsigned int& bit) const { if ( hint != 0 ) { @@ -205,7 +204,7 @@ class BitMap : public Callbackable /** * Return the start_bit of the bitmap */ - unsigned int get_start_bit() + unsigned int get_start_bit() const { return start_bit; } @@ -248,7 +247,7 @@ class BitMap : public Callbackable * be freed by caller. * @return 0 on success */ - int select(SqlDB * db, std::string ** uzbs) + int select(SqlDB * db, std::string &uzbs) { int rc; @@ -256,8 +255,6 @@ class BitMap : public Callbackable std::string zbs; - *uzbs = 0; - set_callback(static_cast(&BitMap::select_cb), static_cast(&zbs)); @@ -276,17 +273,9 @@ class BitMap : public Callbackable return -1; } - *uzbs = one_util::zlib_decompress(zbs, true); - - if ( *uzbs == 0 ) - { - rc = -1; - } - - return rc; + return ssl_util::zlib_decompress64(zbs, uzbs); } - /** * Insert a Bitmap in the DB, the bitmap is stored in a compressed (zlib) * string form. @@ -297,15 +286,14 @@ class BitMap : public Callbackable { std::ostringstream oss; - std::string * zipped = one_util::zlib_compress(bs->to_string(), true); + std::string zipped; - if (zipped == 0) + if (ssl_util::zlib_compress64(bs->to_string(), zipped) != 0) { return -1; } - char * ezipped64 = db->escape_str(*zipped); - + char * ezipped64 = db->escape_str(zipped); if (replace) { @@ -321,8 +309,6 @@ class BitMap : public Callbackable int rc = db->exec_wr(oss); - delete zipped; - db->free_str(ezipped64); return rc; @@ -338,7 +324,6 @@ class BitMap : public Callbackable { std::vector strings; std::vector range; - std::vector::const_iterator it; std::istringstream iss; @@ -346,10 +331,10 @@ class BitMap : public Callbackable strings = one_util::split(reserved, ',', true); - for (it = strings.begin(); it != strings.end(); it++) + for (const auto& str : strings) { // Try to split it by ':' - range = one_util::split(*it, ':', true); + range = one_util::split(str, ':', true); iss.clear(); iss.str(range[0]); diff --git a/include/CachePool.h b/include/CachePool.h index dd0b2f0b1ba..91343be679c 100644 --- a/include/CachePool.h +++ b/include/CachePool.h @@ -17,15 +17,9 @@ #ifndef CACHE_POOL_H_ #define CACHE_POOL_H_ -#include -#include -#include +#include +#include -#include - -#include - -using namespace std; /** * The Cache Pool class. This class is used to store volatile pool data. @@ -33,34 +27,25 @@ using namespace std; template class CachePool { public: - CachePool() - { - pthread_mutex_init(&resource_lock, 0); - } + CachePool() = default; ~CachePool() { - typename std::map::iterator it; - - pthread_mutex_lock(&resource_lock); + std::lock_guard lock(resource_lock); - for (it=resources.begin(); it != resources.end() ; ++it) + for (auto it = resources.begin(); it != resources.end() ; ++it) { delete it->second; } - - pthread_mutex_unlock(&resource_lock); - - pthread_mutex_destroy(&resource_lock); - }; + } T * get_resource(int oid) { T * res; - pthread_mutex_lock(&resource_lock); + std::lock_guard lock(resource_lock); - typename std::map::iterator it = resources.find(oid); + auto it = resources.find(oid); if ( it == resources.end() ) { @@ -73,17 +58,15 @@ template class CachePool res = it->second; } - pthread_mutex_unlock(&resource_lock); - return res; } void delete_resource(int oid) { - pthread_mutex_lock(&resource_lock); + std::lock_guard lock(resource_lock); - typename std::map::iterator it = resources.find(oid); + auto it = resources.find(oid); if ( it != resources.end() ) { @@ -91,13 +74,11 @@ template class CachePool resources.erase(it); } - - pthread_mutex_unlock(&resource_lock); } private: - pthread_mutex_t resource_lock; + std::mutex resource_lock; std::map resources; }; diff --git a/include/Callbackable.h b/include/Callbackable.h index 171fc97d89c..d4738a1b644 100644 --- a/include/Callbackable.h +++ b/include/Callbackable.h @@ -17,15 +17,13 @@ #ifndef CALLBACKABLE_H_ #define CALLBACKABLE_H_ -#include +#include #include #include #include #include -using namespace std; - /** * This class represents a SQL callback */ @@ -33,15 +31,14 @@ class Callbackable { public: - Callbackable():cb(0),arg(0),affected_rows(0) + Callbackable() + : cb(nullptr) + , arg(nullptr) + , affected_rows(0) { - pthread_mutex_init(&mutex,0); - }; + } - virtual ~Callbackable() - { - pthread_mutex_destroy(&mutex); - }; + virtual ~Callbackable() = default; /** * Datatype for call back pointers @@ -56,11 +53,11 @@ class Callbackable */ void set_callback(Callback _cb, void * _arg = nullptr) { - pthread_mutex_lock(&mutex); + _mutex.lock(); cb = _cb; arg = _arg; - }; + } /** * Test if the CallBack is set for the object. @@ -68,8 +65,8 @@ class Callbackable */ virtual bool isCallBackSet() { - return (cb != 0); - }; + return (cb != nullptr); + } /** * Call the callback funcion set. This method must be called only if @@ -81,17 +78,17 @@ class Callbackable ++affected_rows; return (this->*cb)(arg, num, values, names); - }; + } /** * Unset the callback function. */ void unset_callback() { - cb = 0; - arg = 0; + cb = nullptr; + arg = nullptr; - pthread_mutex_unlock(&mutex); + _mutex.unlock(); } /** @@ -105,7 +102,7 @@ class Callbackable /** * get affected rows variable */ - int get_affected_rows() + int get_affected_rows() const { return affected_rows; } @@ -130,7 +127,7 @@ class Callbackable /** * Mutex for locking the callback function. */ - pthread_mutex_t mutex; + std::mutex _mutex; }; /* -------------------------------------------------------------------------- */ @@ -233,7 +230,7 @@ class set_cb : public Callbackable private: - set * ids; + std::set * ids; }; /* -------------------------------------------------------------------------- */ @@ -243,7 +240,7 @@ template class vector_cb : public Callbackable { public: - void set_callback(vector * _oids) + void set_callback(std::vector * _oids) { oids = _oids; @@ -271,7 +268,7 @@ class vector_cb : public Callbackable private: - vector * oids; + std::vector * oids; }; /* -------------------------------------------------------------------------- */ @@ -322,7 +319,7 @@ class stream_cb : public Callbackable public: stream_cb(int _total): total_values(_total){}; - void set_callback(ostringstream * _oss) + void set_callback(std::ostringstream * _oss) { oss = _oss; @@ -352,7 +349,7 @@ class stream_cb : public Callbackable int total_values; - ostringstream * oss; + std::ostringstream * oss; }; /* -------------------------------------------------------------------------- */ diff --git a/include/Client.h b/include/Client.h index 23ecea1ba71..c200cdb72c3 100644 --- a/include/Client.h +++ b/include/Client.h @@ -23,8 +23,6 @@ #include -using namespace std; - // ============================================================================= // Doc: // http://xmlrpc-c.sourceforge.net/doc/#clientexamplepp @@ -118,11 +116,11 @@ class Client * @param message_size for XML elements in the client library (in bytes) * @throws Exception if the authorization options are invalid */ - Client(const string& secret, const string& endpoint, size_t message_size, + Client(const std::string& secret, const std::string& endpoint, size_t message_size, unsigned int tout); - string one_auth; - string one_endpoint; + std::string one_auth; + std::string one_endpoint; unsigned int timeout; diff --git a/include/Cluster.h b/include/Cluster.h index 004db415618..4abbeca745c 100644 --- a/include/Cluster.h +++ b/include/Cluster.h @@ -18,12 +18,10 @@ #define CLUSTER_H_ #include "PoolObjectSQL.h" -#include "ObjectCollection.h" -#include "DatastorePool.h" #include "ClusterTemplate.h" #include "BitMap.h" +#include "ObjectCollection.h" -using namespace std; /** * The Cluster class. @@ -31,46 +29,41 @@ using namespace std; class Cluster : public PoolObjectSQL { public: + + virtual ~Cluster() = default; + // ************************************************************************* // Object Collections (Public) // ************************************************************************* - /** - * Returns a copy of the datastore IDs set - */ - set get_datastores() - { - return datastores.clone(); - }; - /** * Returns a system DS for the cluster when none is set at the API level * @return the ID of the System */ - static int get_default_system_ds(const set& ds_collection); + static int get_default_system_ds(const std::set& ds_collection); /** * Returns a copy of the host IDs set */ - set get_host_ids() + const std::set& get_host_ids() const { - return hosts.clone(); + return hosts.get_collection(); } /** * Returns a copy of the datastore IDs set */ - set get_datastore_ids() + const std::set& get_datastore_ids() const { - return datastores.clone(); + return datastores.get_collection(); } /** * Returns a copy of the vnet IDs set */ - set get_vnet_ids() + const std::set& get_vnet_ids() const { - return vnets.clone(); + return vnets.get_collection(); } /** @@ -79,7 +72,7 @@ class Cluster : public PoolObjectSQL * @param cpu reserved cpu (percentage, or absolute) * @param mem reserved mem (in KB) */ - void get_reserved_capacity(string& cpu, string& mem) const + void get_reserved_capacity(std::string& cpu, std::string& mem) const { get_template_attribute("RESERVED_CPU", cpu); @@ -94,7 +87,7 @@ class Cluster : public PoolObjectSQL * @param xml the resulting XML string * @return a reference to the generated string */ - string& to_xml(string& xml) const override; + std::string& to_xml(std::string& xml) const override; /** * Rebuilds the object from an xml formatted string @@ -102,7 +95,7 @@ class Cluster : public PoolObjectSQL * * @return 0 on success, -1 otherwise */ - int from_xml(const string &xml_str) override; + int from_xml(const std::string &xml_str) override; private: @@ -111,6 +104,7 @@ class Cluster : public PoolObjectSQL // ------------------------------------------------------------------------- friend class ClusterPool; + friend class PoolSQL; // ************************************************************************* // VNC Port management function @@ -123,7 +117,7 @@ class Cluster : public PoolObjectSQL * @param port reserved * @return 0 on success */ - int get_vnc_port(int vmid, unsigned int& port) + int get_vnc_port(int vmid, unsigned int& port) const { unsigned int base_port = vnc_bitmap.get_start_bit(); unsigned int hint_port = base_port + (vmid % (65535 - base_port)); @@ -144,11 +138,10 @@ class Cluster : public PoolObjectSQL // ************************************************************************* // Constructor // ************************************************************************* - Cluster(int id, const string& name, ClusterTemplate* cl_template, + Cluster(int id, const std::string& name, + std::unique_ptr cl_template, const VectorAttribute& vnc_conf); - virtual ~Cluster() = default; - // ************************************************************************* // Attributes (Private) // ************************************************************************* @@ -161,19 +154,7 @@ class Cluster : public PoolObjectSQL // ************************************************************************* // DataBase implementation (Private) // ************************************************************************* - static const char * db_names; - static const char * db_bootstrap; - static const char * table; - - static const char * datastore_table; - static const char * datastore_db_names; - static const char * datastore_db_bootstrap; - - static const char * network_table; - static const char * network_db_names; - static const char * network_db_bootstrap; - static const char * bitmap_table; /** * Execute an INSERT or REPLACE Sql query. * @param db The SQL DB @@ -181,35 +162,20 @@ class Cluster : public PoolObjectSQL * @param error_str Returns the error reason, if any * @return 0 one success */ - int insert_replace(SqlDB *db, bool replace, string& error_str); + int insert_replace(SqlDB *db, bool replace, std::string& error_str); /** * Bootstraps the database table(s) associated to the Cluster * @return 0 on success */ - static int bootstrap(SqlDB * db) - { - int rc; - ostringstream oss; - - oss.str(Cluster::db_bootstrap); - rc = db->exec_local_wr(oss); - - oss.str(Cluster::datastore_db_bootstrap); - rc += db->exec_local_wr(oss); - - oss.str(Cluster::network_db_bootstrap); - rc += db->exec_local_wr(oss); - - return rc; - }; + static int bootstrap(SqlDB * db); /** * Writes the Cluster in the database. * @param db pointer to the db * @return 0 on success */ - int insert(SqlDB *db, string& error_str) override + int insert(SqlDB *db, std::string& error_str) override { int rc; @@ -230,7 +196,7 @@ class Cluster : public PoolObjectSQL */ int update(SqlDB *db) override { - string error_str; + std::string error_str; return insert_replace(db, true, error_str); } @@ -267,7 +233,7 @@ class Cluster : public PoolObjectSQL * @param db pointer to the db * @return 0 on success */ - int select(SqlDB *db, const string& _name, int _uid) override + int select(SqlDB *db, const std::string& _name, int _uid) override { int rc = PoolObjectSQL::select(db, _name, _uid); @@ -286,23 +252,24 @@ class Cluster : public PoolObjectSQL * @param error_msg Error message, if any. * @return 0 if cluster can be dropped, -1 otherwise */ - int check_drop(string& error_msg); + int check_drop(std::string& error_msg); /** * Factory method for cluster templates */ - Template * get_new_template() const override + std::unique_ptr