diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 8a041e5d4f..4166ed3879 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -210,6 +210,13 @@ public: int vid, const vector& inherit_attrs); + /** + * Replace the template of the virtual network it also updates the BRIDGE, + * PHY_DEV, VLAN_ID and VLAN attributes. + * @param tmpl string representation of the template + */ + int replace_template(const string& tmpl_str, string& error); + private: // ------------------------------------------------------------------------- diff --git a/src/pool/PoolObjectSQL.cc b/src/pool/PoolObjectSQL.cc index 0e7aa84603..95d197fe66 100644 --- a/src/pool/PoolObjectSQL.cc +++ b/src/pool/PoolObjectSQL.cc @@ -183,10 +183,7 @@ int PoolObjectSQL::replace_template(const string& tmpl_str, string& error) return -1; } - if ( obj_template != 0 ) - { - delete obj_template; - } + delete obj_template; obj_template = new_tmpl; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 07389e9317..9adcc4d7fb 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -184,7 +184,7 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) ostringstream ose; int rc; - string vlan_attr; + bool b_vlan; string s_type; string ranged_error_str; @@ -227,19 +227,30 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) // ------------ PHYDEV -------------------- - erase_template_attribute("PHYDEV",phydev); + erase_template_attribute("PHYDEV", phydev); + + add_template_attribute("PHYDEV", phydev); // ------------ VLAN_ID ------------------- - erase_template_attribute("VLAN_ID",vlan_id); + erase_template_attribute("VLAN_ID", vlan_id); + + add_template_attribute("VLAN_ID", vlan_id); // ------------ VLAN ---------------------- - erase_template_attribute("VLAN", vlan_attr); + erase_template_attribute("VLAN", b_vlan); - TO_UPPER(vlan_attr); - - vlan = (vlan_attr == "YES") || (vlan_attr.empty() && !phydev.empty()); + if (b_vlan || !phydev.empty()) + { + vlan = 1; + add_template_attribute("VLAN", "YES"); + } + else + { + vlan = 0; + add_template_attribute("VLAN", "NO"); + } // ------------ BRIDGE -------------------- @@ -270,6 +281,8 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) } } + add_template_attribute("BRIDGE", bridge); + // ------------ IP6 PREFIX --------------- erase_template_attribute("GLOBAL_PREFIX", global); @@ -389,6 +402,76 @@ error_common: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VirtualNetwork::replace_template(const string& tmpl_str, string& error_str) +{ + string new_bridge; + bool b_vlan; + + /* ---------------------------------------------------------------------- */ + /* Parse & Update VirtualNetwork Template */ + /* ---------------------------------------------------------------------- */ + + Template * new_tmpl = new VirtualNetworkTemplate; + + if ( new_tmpl == 0 ) + { + error_str = "Cannot allocate a new template"; + return -1; + } + + if ( new_tmpl->parse_str_or_xml(tmpl_str, error_str) != 0 ) + { + delete new_tmpl; + return -1; + } + + delete obj_template; + + obj_template = new_tmpl; + + /* ---------------------------------------------------------------------- */ + /* Update Configuration Attributes (class & template) */ + /* - PHYDEV */ + /* - VLAN_ID */ + /* - VLAN */ + /* - BRIDGE */ + /* ---------------------------------------------------------------------- */ + erase_template_attribute("PHYDEV", phydev); + + add_template_attribute("PHYDEV", phydev); + + erase_template_attribute("VLAN_ID", vlan_id); + + add_template_attribute("VLAN_ID", vlan_id); + + erase_template_attribute("VLAN", b_vlan); + + if (b_vlan || !phydev.empty()) + { + vlan = 1; + add_template_attribute("VLAN", "YES"); + } + else + { + vlan = 0; + add_template_attribute("VLAN", "NO"); + } + + erase_template_attribute("BRIDGE",new_bridge); + + if (!new_bridge.empty()) + { + bridge = new_bridge; + } + + add_template_attribute("BRIDGE", bridge); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VirtualNetwork::insert_replace(SqlDB *db, bool replace, string& error_str) { ostringstream oss;