Allow idempotency for sdn assist network/port deletion
[osm/openvim.git] / osm_openvim / ovim.py
index 5650cd5..e3581b7 100755 (executable)
@@ -43,9 +43,9 @@ import openflow_conn
 
 __author__ = "Alfonso Tierno, Leonardo Mirabal"
 __date__ = "$06-Feb-2017 12:07:15$"
-__version__ = "0.5.18-r534"
-version_date = "Jun 2017"
-database_version = 21      #needed database schema version
+__version__ = "0.5.28-r548"
+version_date = "Jul 2018"
+database_version = 23      #needed database schema version
 
 HTTP_Bad_Request =          400
 HTTP_Unauthorized =         401
@@ -199,8 +199,8 @@ class ovim():
         host_develop_bridge_iface = self.config.get('development_bridge', None)
 
         # get host list from data base before starting threads
-        r, hosts = self.db.get_table(SELECT=('name', 'ip_name', 'user', 'uuid', 'password', 'keyfile'),
-                                     FROM='hosts', WHERE={'status': 'ok'})
+        r, hosts = self.db.get_table(SELECT=('name', 'ip_name', 'user', 'uuid', 'hypervisors', 'password', 'keyfile'),
+                                     FROM='hosts', WHERE={'status': 'ok'}) #Unikernels extension
         if r < 0:
             raise ovimException("Cannot get hosts from database {}".format(hosts))
 
@@ -215,6 +215,7 @@ class ovim():
                                     version=self.config['version'], host_id=host['uuid'],
                                     develop_mode=host_develop_mode,
                                     develop_bridge_iface=host_develop_bridge_iface,
+                                    hypervisors=host['hypervisors'],  #Unikernels extension
                                     logger_name=self.logger_name + ".host." + host['name'],
                                     debug=self.config.get('log_level_host'))
 
@@ -301,13 +302,14 @@ class ovim():
                     links = net.get('links')
                     if links:
                         links = yaml.safe_load(net.get('links'))
-                    self.launch_dhcp_server(net.get('vlan'),
-                                            net.get('dhcp_first_ip'),
-                                            net.get('dhcp_last_ip'),
-                                            net.get('cidr'),
-                                            net.get('gateway_ip'),
-                                            dns,
-                                            routes)
+                    if net.get('enable_dhcp'):
+                        self.launch_dhcp_server(net.get('vlan'),
+                                                net.get('dhcp_first_ip'),
+                                                net.get('dhcp_last_ip'),
+                                                net.get('cidr'),
+                                                net.get('gateway_ip'),
+                                                dns,
+                                                routes)
                     self.launch_link_bridge_to_ovs(net['vlan'], net.get('gateway_ip'), net.get('cidr'), links, routes)
                     if net["status"] == "ERROR":
                         self.db.update_rows("nets", UPDATE={"status": "ACTIVE", "last_error": None},
@@ -499,7 +501,7 @@ class ovim():
 
         return content
 
-    def show_network(self, network_id, db_filter={}):
+    def show_network(self, network_id, db_filter={}, skip_on_not_found=False):
         """
         Get network from DB by id
         :param network_id: net Id
@@ -516,7 +518,9 @@ class ovim():
         if result < 0:
             raise ovimException(str(content), -result)
         elif result == 0:
-            raise ovimException("show_network network '%s' not found" % network_id, -result)
+            if skip_on_not_found:
+                return
+            raise ovimException("show_network network '%s' not found" % network_id, HTTP_Not_Found)
         else:
             convert_boolean(content, ('shared', 'admin_state_up', 'enable_dhcp'))
             # get ports from DB
@@ -524,8 +528,12 @@ class ovim():
                                               WHERE={'net_id': network_id}, LIMIT=100)
             if len(ports) > 0:
                 content[0]['ports'] = ports
-
             convert_boolean(content, ('shared', 'admin_state_up', 'enable_dhcp'))
+
+            result, flows = self.db.get_table(FROM='of_flows', SELECT=('priority', 'vlan_id', 'ingress_port', 'src_mac', 'dst_mac', 'actions'),
+                                              WHERE={'net_id': network_id}, LIMIT=100)
+            if len(flows) > 0:
+                content[0]['flows'] = flows
             return content[0]
 
     def new_network(self, network):
@@ -660,7 +668,7 @@ class ovim():
         network['vlan'] = net_vlan
         network['region'] = net_region
         dhcp_integrity = True
-        if 'enable_dhcp' in network and network['enable_dhcp']:
+        if network.get('enable_dhcp'):
             dhcp_integrity = self._check_dhcp_data_integrity(network)
         
         if network.get('links'):
@@ -671,8 +679,7 @@ class ovim():
             network['routes'] = yaml.safe_dump(network['routes'], default_flow_style=True, width=256)
 
         result, content = self.db.new_row('nets', network, True, True)
-
-        if result >= 0 and dhcp_integrity:
+        if result >= 0:  # and dhcp_integrity:
             if bridge_net:
                 bridge_net[3] = content
             if self.config.get("dhcp_server") and self.config['network_type'] == 'bridge':
@@ -684,7 +691,8 @@ class ovim():
                     self.logger.debug("dhcp_server: add new net", content, content)
             return content
         else:
-            raise ovimException("Error posting network", HTTP_Internal_Server_Error)
+            raise ovimException("Error creating network: {}".format(content), -result)
+
 # TODO kei change update->edit
 
     def edit_network(self, network_id, network):
@@ -778,12 +786,15 @@ class ovim():
         else:
             raise ovimException(content, -result)
 
-    def delete_network(self, network_id):
+    def delete_network(self, network_id, idempotent=True):
         """
         Delete network by network id
         :param network_id:  network id
         :return:
         """
+        net_data = self.show_network(network_id, skip_on_not_found=idempotent)
+        if not net_data:  # network does not exist
+            return
 
         # delete from the data base
         result, content = self.db.delete_row('nets', network_id)
@@ -797,7 +808,18 @@ class ovim():
                     break
             if self.config.get("dhcp_server") and network_id in self.config["dhcp_nets"]:
                 self.config["dhcp_nets"].remove(network_id)
-            return content
+
+            if net_data.get('enable_dhcp'):
+                dhcp_path = self.config['ovs_controller_file_path']
+                dhcp_controller = self.get_dhcp_controller()
+                dhcp_controller.delete_dhcp_server(net_data['vlan'], network_id, dhcp_path)
+                dhcp_controller.delete_dhcp_port(net_data['vlan'], network_id, dhcp_path)
+                links = yaml.load(net_data.get('links'))
+                if links:
+                    links = yaml.load(net_data.get('links'))
+                    self.delete_link_bridge_to_ovs(net_data['vlan'], links)
+
+                return content
         else:
             raise ovimException("Error deleting network '{}': {}".format(network_id, content), -result)
 
@@ -985,6 +1007,8 @@ class ovim():
             del port_data['compute_node']
 
         result, uuid = self.db.new_row('ports', port_data, True, True)
+        # set net status to BUILD
+        self.db.update_rows('nets', {"status": "BUILD"}, WHERE={'uuid': port_data['net_id']})
         if result > 0:
             try:
                 self.net_update_ofc_thread(port_data['net_id'], port_data['ofc_id'])
@@ -1041,7 +1065,7 @@ class ovim():
             self.logger.error(message)
             raise ovimException(message, HTTP_Internal_Server_Error)
 
-    def delete_port(self, port_id):
+    def delete_port(self, port_id, idempotent=False):
         # Look for the previous port data
         result, ports = self.db.get_table(WHERE={'uuid': port_id, "type": "external"}, FROM='ports')
         if result < 0:
@@ -1049,21 +1073,25 @@ class ovim():
         # delete from the data base
         result, content = self.db.delete_row('ports', port_id)
         if result == 0:
+            if idempotent:
+                return
             raise ovimException("External port '{}' not found".format(port_id), http_code=HTTP_Not_Found)
         elif result < 0:
             raise ovimException("Cannot delete port from database: {}".format(content), http_code=-result)
         # update network
-        network = ports[0].get('net_id', None)
-        if network:
+        net_id = ports[0].get('net_id', None)
+        if net_id:
             # change of net.
 
+            # set net status to BUILD
+            self.db.update_rows('nets', {"status": "BUILD"}, WHERE={'uuid': net_id})
             try:
-                self.net_update_ofc_thread(network, ofc_id=ports[0]["ofc_id"], switch_dpid=ports[0]["switch_dpid"])
+                self.net_update_ofc_thread(net_id, ofc_id=ports[0]["ofc_id"], switch_dpid=ports[0]["switch_dpid"])
             except ovimException as e:
-                raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)),
+                raise ovimException("Cannot insert a task for delete network '{}' {}".format(net_id, str(e)),
                                     HTTP_Internal_Server_Error)
             except Exception as e:
-                raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)),
+                raise ovimException("Cannot insert a task for delete network '{}' {}".format(net_id, str(e)),
                                     HTTP_Internal_Server_Error)
 
         return content
@@ -1349,6 +1377,8 @@ class ovim():
                 map['switch_dpid'] = switch_dpid
             if region:
                 map['region'] = region
+            if map.get("pci"):
+                map["pci"] = map["pci"].lower()
 
         for of_map in of_maps:
             result, uuid = self.db.new_row('of_port_mappings', of_map, True)