RIFT-14562
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rwcal_openstack.py
index e5dcb67..b430886 100644 (file)
@@ -431,8 +431,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         with self._use_driver(account) as drv:
             vm_id = drv.nova_server_create(**kwargs)
             if floating_ip:
-                self.prepare_vdu_on_boot(account, vm_id, floating_ip)
-
+                self.prepare_vdu_on_boot(account, vm_id, floating_ip, mgmt_network = None)
         return vm_id
 
     @rwstatus
@@ -1228,7 +1227,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         return link
 
     @staticmethod
-    def _fill_vdu_info(vm_info, flavor_info, mgmt_network, port_list, server_group, volume_list = None):
+    def _fill_vdu_info(vm_info, flavor_info, mgmt_network, port_list, server_group, volume_list=None):
         """Create a GI object for VDUInfoParams
 
         Converts VM information dictionary object returned by openstack
@@ -1249,11 +1248,12 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         for network_name, network_info in vm_info['addresses'].items():
             if network_info and network_name == mgmt_network:
                 for interface in network_info:
-                    if 'OS-EXT-IPS:type' in interface:
-                        if interface['OS-EXT-IPS:type'] == 'fixed':
-                            vdu.management_ip = interface['addr']
-                        elif interface['OS-EXT-IPS:type'] == 'floating':
-                            vdu.public_ip = interface['addr']
+                    for interface in network_info:
+                        if 'OS-EXT-IPS:type' in interface:
+                            if interface['OS-EXT-IPS:type'] == 'fixed':
+                                vdu.management_ip = interface['addr']
+                            elif interface['OS-EXT-IPS:type'] == 'floating':
+                                vdu.public_ip = interface['addr']
 
         # Look for any metadata
         for key, value in vm_info['metadata'].items():
@@ -1460,6 +1460,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
            account  - a cloud account
            c_point  - connection_points
         """
+
         kwargs = {}
         kwargs['name'] = c_point.name
         kwargs['network_id'] = c_point.virtual_link_id
@@ -1472,11 +1473,16 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         else:
             raise NotImplementedError("Port Type: %s not supported" %(c_point.type_yang))
 
+        if c_point.static_ip_address:
+            kwargs["ip_address"] = c_point.static_ip_address
+
         with self._use_driver(account) as drv:
             if c_point.has_field('security_group'):
                 group = drv.neutron_security_group_by_name(c_point.security_group)
                 if group is not None:
                     kwargs['security_groups'] = [group['id']]
+            self.log.debug("Create connection point port : {}".
+                           format(kwargs))
             return drv.neutron_port_create(**kwargs)
 
     def _allocate_floating_ip(self, drv, pool_name):
@@ -1868,7 +1874,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
                 raise OpenstackCALOperationFailure("Create-flavor operation failed for cloud account: %s" %(account.name))
             return flavor_id
 
-    def _create_vm(self, account, vduinfo, pci_assignement=None, server_group=None, port_list=None, network_list=None, imageinfo_list=None):
+    def _create_vm(self, account, vduinfo, pci_assignement=None, server_group=None, port_list=None, network_list=None, imageinfo_list=None, mgmt_network = None):
         """Create a new virtual machine.
 
         Arguments:
@@ -1963,7 +1969,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         with self._use_driver(account) as drv:
             vm_id = drv.nova_server_create(**kwargs)
             if floating_ip:
-                self.prepare_vdu_on_boot(account, vm_id, floating_ip)
+                self.prepare_vdu_on_boot(account, vm_id, floating_ip, mgmt_network)
 
         return vm_id
 
@@ -2010,7 +2016,11 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
             The vdu_id
         """
         ### First create required number of ports aka connection points
+        # Add the mgmt_ntwk by default.
+
+        mgmt_network_id = None
         with self._use_driver(account) as drv:
+            mgmt_network_id = drv._mgmt_network_id
             ### If floating_ip is required and we don't have one, better fail before any further allocation
             if vdu_init.has_field('allocate_public_address') and vdu_init.allocate_public_address:
                 if account.openstack.has_field('floating_ip_pool'):
@@ -2020,11 +2030,31 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
                 floating_ip = self._allocate_floating_ip(drv, pool_name)
             else:
                 floating_ip = None
-
         port_list = []
         network_list = []
         imageinfo_list = []
-        for c_point in vdu_init.connection_points:
+        explicit_mgmt_network = None
+        vdu_connection_points = []
+        #This check is to identify the c_point of overriding management network first
+        if vdu_init.get_mgmt_network() is not None:
+            explicit_mgmt_network = vdu_init.get_mgmt_network()
+            with self._use_driver(account) as drv:
+                overridding_mgmt_network = drv.neutron_network_by_name(explicit_mgmt_network)
+                mgmt_network_conn_point_names = []
+                for c_point in vdu_init.connection_points:
+                    if overridding_mgmt_network["id"] == c_point.virtual_link_id:
+                        vdu_connection_points.append(c_point)
+                        mgmt_network_conn_point_names.append(c_point.name)
+
+                for c_point in vdu_init.connection_points:
+                    if c_point.name not in mgmt_network_conn_point_names:
+                        vdu_connection_points.append(c_point)
+        else:
+            vdu_connection_points = vdu_init.connection_points
+       
+        for c_point in vdu_connection_points:
+            # if the user has specified explicit mgmt_network connection point
+            # then remove the mgmt_network from the VM list
             if c_point.virtual_link_id in network_list:
                 assert False, "Only one port per network supported. Refer: http://specs.openstack.org/openstack/nova-specs/specs/juno/implemented/nfv-multiple-if-1-net.html"
             else:
@@ -2086,7 +2116,8 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         with self._use_driver(account) as drv:
             ### Now Create VM
             vm_network_list = []
-            vm_network_list.append(drv._mgmt_network_id)
+            if explicit_mgmt_network is None:
+                vm_network_list.append(drv._mgmt_network_id)
   
             if vdu_init.has_field('volumes'):
                   # Only combination supported: Image->Volume
@@ -2122,8 +2153,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
             if pci_assignement != '':
                 vm.user_tags.pci_assignement = pci_assignement
 
-            vm_id = self._create_vm(account, vdu_init, pci_assignement=pci_assignement, server_group=server_group, port_list=port_list, network_list=vm_network_list, imageinfo_list = imageinfo_list)
-            self.prepare_vdu_on_boot(account, vm_id, floating_ip)
+            vm_id = self._create_vm(account, vdu_init, pci_assignement=pci_assignement, server_group=server_group, port_list=port_list, network_list=vm_network_list, imageinfo_list = imageinfo_list, mgmt_network = explicit_mgmt_network)           
             return vm_id
 
     def prepare_vpci_metadata(self, drv, vdu_init):
@@ -2169,13 +2199,22 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
 
 
 
-    def prepare_vdu_on_boot(self, account, server_id, floating_ip):
-        cmd = PREPARE_VM_CMD.format(auth_url     = account.openstack.auth_url,
+    def prepare_vdu_on_boot(self, account, server_id, floating_ip, mgmt_network = None):
+
+        if(mgmt_network is None):
+            cmd = PREPARE_VM_CMD.format(auth_url = account.openstack.auth_url,
                                     username     = account.openstack.key,
                                     password     = account.openstack.secret,
                                     tenant_name  = account.openstack.tenant,
                                     mgmt_network = account.openstack.mgmt_network,
                                     server_id    = server_id)
+        else:
+            cmd = PREPARE_VM_CMD.format(auth_url = account.openstack.auth_url,
+                                    username     = account.openstack.key,
+                                    password     = account.openstack.secret,
+                                    tenant_name  = account.openstack.tenant,
+                                    mgmt_network = mgmt_network,
+                                    server_id    = server_id)
 
         if floating_ip is not None:
             cmd += (" --floating_ip "+ floating_ip.ip)
@@ -2250,7 +2289,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
 
 
     @rwstatus(ret_on_failure=[None])
-    def do_get_vdu(self, account, vdu_id):
+    def do_get_vdu(self, account, vdu_id, mgmt_network=None):
         """Get information about a virtual deployment unit.
 
         Arguments:
@@ -2261,9 +2300,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
             Object of type RwcalYang.VDUInfoParams
         """
         with self._use_driver(account) as drv:
-
-            ### Get list of ports excluding the one for management network
-            port_list = [p for p in drv.neutron_port_list(**{'device_id': vdu_id}) if p['network_id'] != drv.get_mgmt_network_id()]
+            port_list = drv.neutron_port_list(**{'device_id': vdu_id})
 
             vm = drv.nova_server_get(vdu_id)
 
@@ -2277,9 +2314,12 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
             openstack_group_list = drv.nova_server_group_list()
             server_group = [ i['name'] for i in openstack_group_list if vm['id'] in i['members']]
             openstack_srv_volume_list = drv.nova_volume_list(vm['id'])
+
+            if mgmt_network is None:
+                mgmt_network = account.openstack.mgmt_network
             vdu_info = RwcalOpenstackPlugin._fill_vdu_info(vm,
                                                            flavor_info,
-                                                           account.openstack.mgmt_network,
+                                                           mgmt_network,
                                                            port_list,
                                                            server_group,
                                                            volume_list = openstack_srv_volume_list)
@@ -2309,8 +2349,7 @@ class RwcalOpenstackPlugin(GObject.Object, RwCal.Cloud):
         with self._use_driver(account) as drv:
             vms = drv.nova_server_list()
             for vm in vms:
-                ### Get list of ports excluding one for management network
-                port_list = [p for p in drv.neutron_port_list(**{'device_id': vm['id']}) if p['network_id'] != drv.get_mgmt_network_id()]
+                port_list = drv.neutron_port_list(**{'device_id': vm['id']})
 
                 flavor_info = None