diff --git a/include/NebulaUtil.h b/include/NebulaUtil.h index 441e24df4a..5dac3c1906 100644 --- a/include/NebulaUtil.h +++ b/include/NebulaUtil.h @@ -174,6 +174,29 @@ namespace one_util * @return trimed string */ std::string trim(const std::string& str); + + /** + * Returns a copy of st with the all occurrences of "find" substituted + * for "replacement" + * @param st string input + * @param find string to search for + * @param replacement string to replace occurrences with + * @return a string copy + */ + std::string gsub(const std::string& st, + const char *find, const char *replacement); + + + /** + * Returns a copy of st with the all occurrences of "find" substituted + * for "replacement" + * @param st string input + * @param find string to search for + * @param replacement string to replace occurrences with + * @return a string copy + */ + std::string gsub(const std::string& st, + const char *find, const std::string& replacement); }; #endif /* _NEBULA_UTIL_H_ */ diff --git a/src/common/NebulaUtil.cc b/src/common/NebulaUtil.cc index b172da2b51..01c681edad 100644 --- a/src/common/NebulaUtil.cc +++ b/src/common/NebulaUtil.cc @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -306,3 +307,36 @@ std::string one_util::trim(const std::string& str) return tstr; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string one_util::gsub(const string& st, + const char *find, const string& replacement) +{ + return gsub(st, find, replacement.c_str()); +} + +/* -------------------------------------------------------------------------- */ + +string one_util::gsub(const string& st, + const char *find, const char *replacement) +{ + string result = st; + + string::size_type pos = 0; + size_t find_len = strlen(find); + size_t replacement_len = strlen(replacement); + + pos = result.find(find, pos); + + while(pos != std::string::npos) + { + result.replace(pos, find_len, replacement); + pos += replacement_len; + + pos = result.find(find, pos); + } + + return result; +} diff --git a/src/rm/RequestManagerVMTemplate.cc b/src/rm/RequestManagerVMTemplate.cc index 1d7d4924e0..4dc55ffbe2 100644 --- a/src/rm/RequestManagerVMTemplate.cc +++ b/src/rm/RequestManagerVMTemplate.cc @@ -68,6 +68,8 @@ void VirtualRouterInstantiate::request_execute( Template* extra_attrs; bool has_vmids; string errorstr; + string vr_name; + ostringstream oss; vector vms; vector::iterator vmid; @@ -93,6 +95,8 @@ void VirtualRouterInstantiate::request_execute( has_vmids = vr->has_vmids(); + vr_name = vr->get_name(); + vr->unlock(); if ( att.uid != 0 ) @@ -120,9 +124,21 @@ void VirtualRouterInstantiate::request_execute( return; } + if (name.empty()) + { + oss.str(""); + oss << "vr-" << vr_name << "-%i"; + name = oss.str(); + } + for (int i=0; i