diff --git a/include/AddressRange.h b/include/AddressRange.h index 46ddf9bca0..bb2951fdec 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -146,6 +146,14 @@ public: */ void free_addr(const string& mac); + /** + * Return the id for this address range + */ + unsigned int ar_id() + { + return id; + } + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h new file mode 100644 index 0000000000..84d8c452d4 --- /dev/null +++ b/include/AddressRangePool.h @@ -0,0 +1,62 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), 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 */ +/* */ +/* 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 ADDRESS_RANGE_POOL_H_ +#define ADDRESS_RANGE_POOL_H_ + +#include +#include +#include + +#include + +#include "Template.h" +#include "AddressRange.h" + +using namespace std; + +class AddressRangePool +{ + AddressRangePool(); + + virtual ~AddressRangePool(); + + /** + * Builds the address range set from an array of VectorAttributes. This + * function is used to create address ranges. + * @param ars the vector of address ranges + * @param error_msg describing the error + * @return 0 on success + */ + int from_vattr(vector ars, string& error_msg); + + /** + * Builds the address range set from its XML representation. This function + * is used to rebuild the address ranges from the DB. + * @param node xmlNode for the template + * @return 0 on success + */ + int from_xml_node(const xmlNodePtr node); + +private: + Template ar_template; + + unsigned int next_ar; + + map ar_pool; +}; + +#endif diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 3878b1a3e7..36bbdaddad 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -775,10 +775,6 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList, } } - - - - // ------------------------------------------------------------------------ // Add a new history record and deploy the VM // ------------------------------------------------------------------------ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc new file mode 100644 index 0000000000..1612b1472e --- /dev/null +++ b/src/vnm/AddressRangePool.cc @@ -0,0 +1,113 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), 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 */ +/* */ +/* 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. */ +/* -------------------------------------------------------------------------- */ + +#include "AddressRangePool.h" + +using namespace std; + +AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"),next_ar(0) +{}; + +AddressRangePool::~AddressRangePool() +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + delete it->second; + } +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::from_vattr(vector ars, string& error_msg) +{ + vector::iterator it; + + /* -------------------- Init the AR pool ---------------------------- */ + + for (it = ars.begin(); it != ars.end(); it++) + { + VectorAttribute * va = static_cast(*it); + + if (va == 0) + { + error_msg = "Wrong AR format"; + return -1; + } + + AddressRange * ar = new AddressRange(next_ar); + + if (ar->from_vattr(va, error_msg) != 0) + { + delete ar; + return -1; + } + + ar_pool.insert(make_pair(next_ar++, ar)); + } + + /* ---------------------- Template ---------------------------------- */ + + for (it = ars.begin(); it != ars.end(); it++) + { + ar_template.set(*it); + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::from_xml_node(const xmlNodePtr node) +{ + int rc = ar_template.from_xml_node(node); + + if (rc != 0) + { + return -1; + } + + vector var; + + int num_ar = ar_template.get("AR", var); + + for (int i = 0; i < num_ar; i++) + { + VectorAttribute * va = static_cast(var[i]); + + AddressRange * ar = new AddressRange(0); + + if (ar->from_vattr_db(va)!= 0) + { + return -1; + } + + ar_pool.insert(make_pair(ar->ar_id(), ar)); + + ar_template.set(va); + + if (ar->ar_id() > next_ar) + { + next_ar = ar->ar_id() + 1; + } + } + + return 0; +} + diff --git a/src/vnm/SConstruct b/src/vnm/SConstruct index f3466b8e82..eb586a69df 100644 --- a/src/vnm/SConstruct +++ b/src/vnm/SConstruct @@ -27,7 +27,8 @@ source_files=[ 'RangedLeases.cc', 'VirtualNetwork.cc', 'VirtualNetworkPool.cc', - 'AddressRange.cc' + 'AddressRange.cc', + 'AddressRangePool.cc' ] # Build library