[odl-sfc] Add function get_vnf and fix endless loop in get_vnf_id
authorJuan Vidal <juan.vidal.allende@ericsson.com>
Tue, 21 Feb 2017 10:26:28 +0000 (10:26 +0000)
committerJuan Vidal <juan.vidal.allende@ericsson.com>
Tue, 21 Feb 2017 10:27:13 +0000 (10:27 +0000)
Change-Id: I1828242cec6449b36d09be21a62074b3c131af17
Signed-off-by: Juan Vidal <juan.vidal.allende@ericsson.com>
functest/utils/openstack_tacker.py

index 1c02e04..07acc8b 100644 (file)
@@ -48,9 +48,8 @@ def get_vnfd_id(tacker_client, vnfd_name):
 def get_vnf_id(tacker_client, vnf_name, timeout=5):
     vnf_id = None
     while vnf_id is None and timeout >= 0:
-        try:
-            vnf_id = get_id_from_name(tacker_client, 'vnf', vnf_name)
-        except:
+        vnf_id = get_id_from_name(tacker_client, 'vnf', vnf_name)
+        if vnf_id is None:
             logger.info("Could not retrieve ID for vnf with name [%s]."
                         " Retrying." % vnf_name)
             time.sleep(1)
@@ -145,27 +144,39 @@ def create_vnf(tacker_client, vnf_name, vnfd_id=None,
         return None
 
 
-def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None, timeout=60):
+def get_vnf(tacker_client, vnf_id=None, vnf_name=None):
     try:
         if vnf_id is None and vnf_name is None:
             raise Exception('You must specify vnf_id or vnf_name')
+
         _id = get_vnf_id(tacker_client, vnf_name) if vnf_id is None else vnf_id
 
-        vnf = next((v for v in list_vnfs(tacker_client, verbose=True)['vnfs']
-                   if v['id'] == _id), None)
+        if _id is not None:
+            all_vnfs = list_vnfs(tacker_client, verbose=True)['vnfs']
+            return next((vnf for vnf in all_vnfs if vnf['id'] == _id), None)
+        else:
+            raise Exception('Could not retrieve ID from name [%s]' % vnf_name)
+
+    except Exception, e:
+        logger.error("Could not retrieve VNF [vnf_id=%s, vnf_name=%s] - %s"
+                     % (vnf_id, vnf_name, e))
+        return None
+
+
+def wait_for_vnf(tacker_client, vnf_id=None, vnf_name=None, timeout=60):
+    try:
+        vnf = get_vnf(tacker_client, vnf_id, vnf_name)
         if vnf is None:
-            raise Exception("Could not retrieve VNF with ID [%s]" % _id)
+            raise Exception("Could not retrieve VNF - id='%s', name='%s'"
+                            % vnf_id, vnf_name)
         logger.info('Waiting for vnf {0}'.format(str(vnf)))
-        while True and timeout >= 0:
+        while vnf['status'] != 'ACTIVE' and timeout >= 0:
             if vnf['status'] == 'ERROR':
-                raise Exception('Error when booting vnf %s' % _id)
+                raise Exception('Error when booting vnf %s' % vnf['id'])
             elif vnf['status'] == 'PENDING_CREATE':
                 time.sleep(3)
                 timeout -= 3
-                continue
-            else:
-                break
-        return _id
+        return vnf['id']
     except Exception, e:
         logger.error("error [wait_for_vnf(tacker_client, '%s', '%s')]: %s"
                      % (vnf_id, vnf_name, e))