From 19bf8ab8c80075a083ebea362ea7339448acfb85 Mon Sep 17 00:00:00 2001
From: juanmont <jjmontiel@opennebula.systems>
Date: Thu, 25 Oct 2018 11:33:55 +0200
Subject: [PATCH] F #2427: Added AUTOMATIC_NIC_REQUIREMENTS to speed_up NIC
 filtering

---
 src/scheduler/src/pool/VirtualMachineXML.cc | 12 ++++++++++++
 src/vm/VirtualMachine.cc                    | 13 ++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/scheduler/src/pool/VirtualMachineXML.cc b/src/scheduler/src/pool/VirtualMachineXML.cc
index 0cffa1b497..ddb535de44 100644
--- a/src/scheduler/src/pool/VirtualMachineXML.cc
+++ b/src/scheduler/src/pool/VirtualMachineXML.cc
@@ -33,6 +33,7 @@ void VirtualMachineXML::init_attributes()
 
     string automatic_requirements;
     string automatic_ds_requirements;
+    string automatic_nic_requirements;
 
     xpath(oid, "/VM/ID", -1);
     xpath(uid, "/VM/UID", -1);
@@ -101,6 +102,8 @@ void VirtualMachineXML::init_attributes()
 
     // ------------------- NIC REQUIREMENTS -------------------------------------
 
+    xpath(automatic_nic_requirements, "/VM/TEMPLATE/AUTOMATIC_NIC_REQUIREMENTS", "");
+
     if (get_nodes("/VM/TEMPLATE/NIC", nodes) > 0)
     {
         std::string net_mode;
@@ -131,6 +134,15 @@ void VirtualMachineXML::init_attributes()
 
                 if ( nic_template->get("SCHED_REQUIREMENTS", requirements) )
                 {
+                    if ( !automatic_nic_requirements.empty() )
+                    {
+                        ostringstream oss;
+
+                        oss << automatic_nic_requirements << " & ( " << requirements << " )";
+
+                        requirements = oss.str();
+                    }
+
                     the_nic->set_requirements(requirements);
                 }
 
diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc
index fd4f1fafda..8d8f2587bb 100644
--- a/src/vm/VirtualMachine.cc
+++ b/src/vm/VirtualMachine.cc
@@ -1553,6 +1553,7 @@ int VirtualMachine::automatic_requirements(set<int>& cluster_ids,
 
     obj_template->erase("AUTOMATIC_REQUIREMENTS");
     obj_template->erase("AUTOMATIC_DS_REQUIREMENTS");
+    obj_template->erase("AUTOMATIC_NIC_REQUIREMENTS");
 
     int rc = get_cluster_requirements(obj_template, cluster_ids, error_str);
 
@@ -1625,6 +1626,8 @@ int VirtualMachine::automatic_requirements(set<int>& cluster_ids,
 
             oss << ")";
 
+            obj_template->add("AUTOMATIC_NIC_REQUIREMENTS", oss.str());
+
             if ( !datastore_ids.empty() )
             {
                 oss << " & ";
@@ -2216,12 +2219,13 @@ string& VirtualMachine::to_xml_short(string& xml)
 {
     string disks_xml, monitoring_xml, user_template_xml, history_xml, nics_xml;
     ostringstream   oss;
-    string cpu_tmpl, mem_tmpl, auto_reqs, auto_ds_reqs;
+    string cpu_tmpl, mem_tmpl, auto_reqs, auto_ds_reqs, auto_nic_reqs;
 
     obj_template->get("CPU", cpu_tmpl);
     obj_template->get("MEMORY", mem_tmpl);
     obj_template->get("AUTOMATIC_REQUIREMENTS", auto_reqs);
     obj_template->get("AUTOMATIC_DS_REQUIREMENTS", auto_ds_reqs);
+    obj_template->get("AUTOMATIC_NIC_REQUIREMENTS", auto_nic_reqs);
 
     oss << "<VM>"
         << "<ID>"        << oid       << "</ID>"
@@ -2265,6 +2269,13 @@ string& VirtualMachine::to_xml_short(string& xml)
         oss << "</AUTOMATIC_DS_REQUIREMENTS>";
     }
 
+    if (!auto_nic_reqs.empty())
+    {
+        oss << "<AUTOMATIC_NIC_REQUIREMENTS>";
+        oss << one_util::escape_xml(auto_nic_reqs);
+        oss << "</AUTOMATIC_NIC_REQUIREMENTS>";
+    }
+
     oss << "</TEMPLATE>"
         << monitoring.to_xml_short(monitoring_xml)
         << user_obj_template->to_xml_short(user_template_xml);