new API v3 that consumes OSM IM models
[osm/RO.git] / openmano
index ec588a9..26ef9b9 100755 (executable)
--- a/openmano
+++ b/openmano
@@ -28,8 +28,8 @@ openmano client used to interact with openmano-server (openmanod)
 """
 __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes"
 __date__ = "$09-oct-2014 09:09:48$"
-__version__ = "0.4.15-r525"
-version_date = "Jul 2017"
+__version__ = "0.4.20-r530"
+version_date = "Sep 2017"
 
 from argcomplete.completers import FilesCompleter
 import os
@@ -226,6 +226,9 @@ def _get_item_uuid(item, item_name_id, tenant=None):
         if i["name"] == item_name_id:
             uuid = i["uuid"]
             found += 1
+        if item_name_id.startswith("osm_id=") and i.get("osm_id") == item_name_id[7:]:
+            uuid = i["uuid"]
+            found += 1
     if found == 0:
         raise OpenmanoCLIError("No %s found with name/uuid '%s'" %(item[:-1], item_name_id))
     elif found > 1:
@@ -259,45 +262,90 @@ def vnf_create(args):
     headers_req = {'Accept': 'application/json', 'content-type': 'application/json'}
     tenant = _get_tenant()
     myvnf = _load_file_or_yaml(args.file)
+    api_version = ""
+    if "vnfd:vnfd-catalog" in myvnf or "vnfd-catalog" in myvnf:
+        api_version = "/v3"
+        token = "vnfd"
+        vnfd_catalog = myvnf.get("vnfd:vnfd-catalog")
+        if not vnfd_catalog:
+            vnfd_catalog = myvnf.get("vnfd-catalog")
+        vnfds = vnfd_catalog.get("vnfd:vnfd")
+        if not vnfds:
+            vnfds = vnfd_catalog.get("vnfd")
+        vnfd = vnfds[0]
+        vdu_list = vnfd["vdu"]
+
+    else:  # old API
+        api_version = ""
+        token = "vnfs"
+        vnfd = myvnf['vnf']
+        vdu_list = vnfd["VNFC"]
 
     if args.name or args.description or args.image_path or args.image_name or args.image_checksum:
-        #print args.name
+        # TODO, change this for API v3
+        # print args.name
         try:
             if args.name:
-                myvnf['vnf']['name'] = args.name
+                vnfd['name'] = args.name
             if args.description:
-                myvnf['vnf']['description'] = args.description
+                vnfd['description'] = args.description
             if args.image_path:
-                index=0
+                index = 0
                 for image_path_ in args.image_path.split(","):
-                    #print "image-path", image_path_
-                    myvnf['vnf']['VNFC'][index]['VNFC image']=image_path_
-                    if "image name" in myvnf['vnf']['VNFC'][index]:
-                        del myvnf['vnf']['VNFC'][index]["image name"]
-                    if "image checksum" in myvnf['vnf']['VNFC'][index]:
-                        del myvnf['vnf']['VNFC'][index]["image checksum"]
-                    index=index+1
-            if args.image_name: #image name precedes if both are supplied
-                index=0
+                    # print "image-path", image_path_
+                    if api_version == "/v3":
+                        if vdu_list[index].get("image"):
+                            vdu_list[index]['image'] = image_path_
+                            if "image-checksum" in vdu_list[index]:
+                                del vdu_list[index]["image-checksum"]
+                        else:  # image name in volumes
+                            vdu_list[index]["volumes"][0]["image"] = image_path_
+                            if "image-checksum" in vdu_list[index]["volumes"][0]:
+                                del vdu_list[index]["volumes"][0]["image-checksum"]
+                    else:
+                        vdu_list[index]['VNFC image'] = image_path_
+                        if "image name" in vdu_list[index]:
+                            del vdu_list[index]["image name"]
+                        if "image checksum" in vdu_list[index]:
+                            del vdu_list[index]["image checksum"]
+                    index += 1
+            if args.image_name:  # image name precedes if both are supplied
+                index = 0
                 for image_name_ in args.image_name.split(","):
-                    myvnf['vnf']['VNFC'][index]['image name']=image_name_
-                    if "VNFC image" in myvnf['vnf']['VNFC'][index]:
-                        del myvnf['vnf']['VNFC'][index]["VNFC image"]
-                    index=index+1
+                    if api_version == "/v3":
+                        if vdu_list[index].get("image"):
+                            vdu_list[index]['image'] = image_name_
+                            if "image-checksum" in vdu_list[index]:
+                                del vdu_list[index]["image-checksum"]
+                        else:  # image name in volumes
+                            vdu_list[index]["volumes"][0]["image"] = image_name_
+                            if "image-checksum" in vdu_list[index]["volumes"][0]:
+                                del vdu_list[index]["volumes"][0]["image-checksum"]
+                    else:
+                        vdu_list[index]['image name'] = image_name_
+                        if "VNFC image" in vdu_list[index]:
+                            del vdu_list[index]["VNFC image"]
+                    index += 1
             if args.image_checksum:
-                index=0
+                index = 0
                 for image_checksum_ in args.image_checksum.split(","):
-                    myvnf['vnf']['VNFC'][index]['image checksum']=image_checksum_
-                    index=index+1
+                    if api_version == "/v3":
+                        if vdu_list[index].get("image"):
+                            vdu_list[index]['image-checksum'] = image_checksum_
+                        else:  # image name in volumes
+                            vdu_list[index]["volumes"][0]["image-checksum"] = image_checksum_
+                    else:
+                        vdu_list[index]['image checksum'] = image_checksum_
+                    index += 1
         except (KeyError, TypeError), e:
-            if str(e)=='vnf':           error_pos= "missing field 'vnf'"
-            elif str(e)=='name':        error_pos= "missing field  'vnf':'name'"
-            elif str(e)=='description': error_pos= "missing field  'vnf':'description'"
-            elif str(e)=='VNFC':        error_pos= "missing field  'vnf':'VNFC'"
-            elif str(e)==str(index):    error_pos= "field  'vnf':'VNFC' must be an array"
-            elif str(e)=='VNFC image':  error_pos= "missing field 'vnf':'VNFC'['VNFC image']"
-            elif str(e)=='image name':  error_pos= "missing field 'vnf':'VNFC'['image name']"
-            elif str(e)=='image checksum':  error_pos= "missing field 'vnf':'VNFC'['image checksum']"
+            if str(e) == 'vnf':           error_pos= "missing field 'vnf'"
+            elif str(e) == 'name':        error_pos= "missing field  'vnf':'name'"
+            elif str(e) == 'description': error_pos= "missing field  'vnf':'description'"
+            elif str(e) == 'VNFC':        error_pos= "missing field  'vnf':'VNFC'"
+            elif str(e) == str(index):    error_pos= "field  'vnf':'VNFC' must be an array"
+            elif str(e) == 'VNFC image':  error_pos= "missing field 'vnf':'VNFC'['VNFC image']"
+            elif str(e) == 'image name':  error_pos= "missing field 'vnf':'VNFC'['image name']"
+            elif str(e) == 'image checksum':  error_pos= "missing field 'vnf':'VNFC'['image checksum']"
             else:                       error_pos="wrong format"
             print "Wrong VNF descriptor: " + error_pos
             return -1 
@@ -305,7 +353,7 @@ def vnf_create(args):
         
     #print payload_req
         
-    URLrequest = "http://%s:%s/openmano/%s/vnfs" %(mano_host, mano_port, tenant)
+    URLrequest = "http://{}:{}/openmano{}/{}/{token}".format(mano_host, mano_port, api_version, tenant, token=token)
     logger.debug("openmano request: %s", payload_req)
     mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
     logger.debug("openmano response: %s", mano_response.text )
@@ -326,7 +374,7 @@ def vnf_list(args):
     mano_response = requests.get(URLrequest)
     logger.debug("openmano response: %s", mano_response.text )
     content = mano_response.json()
-    #print json.dumps(content, indent=4)
+    # print json.dumps(content, indent=4)
     if args.verbose==None:
         args.verbose=0
     result = 0 if mano_response.status_code==200 else mano_response.status_code
@@ -337,36 +385,40 @@ def vnf_list(args):
                 return result
             if len(content['vnfs']) == 0:
                 print "No VNFs were found."
-                return 404 #HTTP_Not_Found
+                return 404   # HTTP_Not_Found
             for vnf in content['vnfs']:
-                myoutput = "%s %s" %(vnf['uuid'].ljust(38),vnf['name'].ljust(20))
-                if args.verbose >=1:
-                    myoutput = "%s %s" %(myoutput, vnf['created_at'].ljust(20))
-                print myoutput
-                if args.verbose >=2:
-                    print "  Description: %s" %vnf['description']
-                    print "  VNF descriptor file: %s" %vnf['path']
+                myoutput = "{:38} {:20}".format(vnf['uuid'], vnf['name'])
+                if vnf.get('osm_id') or args.verbose >= 1:
+                    myoutput += " osm_id={:20}".format(vnf.get('osm_id'))
+                if args.verbose >= 1:
+                    myoutput += " {}".format(vnf['created_at'])
+                print (myoutput)
+                if args.verbose >= 2:
+                    print ("  Description: {}".format(vnf['description']))
+                    # print ("  VNF descriptor file: {}".format(vnf['path']))
         else:
             if args.verbose:
                 print yaml.safe_dump(content, indent=4, default_flow_style=False)
                 return result
             vnf = content['vnf']
-            print "%s %s %s" %(vnf['uuid'].ljust(38),vnf['name'].ljust(20), vnf['created_at'].ljust(20))
-            print "  Description: %s" %vnf['description']
-            #print "  VNF descriptor file: %s" %vnf['path']
-            print "    VMs:"
+            print ("{:38} {:20} osm_id={:20} {:20}".format(vnf['uuid'], vnf['name'], vnf.get('osm_id'),
+                                                           vnf['created_at']))
+            print ("  Description: {}".format(vnf['description']))
+            # print "  VNF descriptor file: %s" %vnf['path']
+            print ("  VMs:")
             for vm in vnf['VNFC']:
-                #print "    %s %s %s" %(vm['name'].ljust(20), vm['uuid'].ljust(38), vm['description'].ljust(30))
-                print "        %s %s" %(vm['name'].ljust(20), vm['description'])
-            if len(vnf['nets'])>0:
-                print "    Internal nets:"
+                print ("    {:20} osm_id={:20} {}".format(vm['name'], vm.get('osm_id'), vm['description']))
+            if len(vnf['nets']) > 0:
+                print ("  Internal nets:")
                 for net in vnf['nets']:
-                    print "        %s %s" %(net['name'].ljust(20), net['description'])
-            if len(vnf['external-connections'])>0:
-                print "    External interfaces:"
+                    print ("    {:20} {}".format(net['name'], net['description']))
+            if len(vnf['external-connections']) > 0:
+                print ("  External interfaces:")
                 for interface in vnf['external-connections']:
-                    print "        %s %s %s %s" %(interface['external_name'].ljust(20), interface['vm_name'].ljust(20), interface['internal_name'].ljust(20), \
-                                                  interface.get('vpci',"").ljust(14))
+                    print ("    {:20} {:20} {:20} {:14}".format(
+                        interface['external_name'], interface['vm_name'],
+                        interface['internal_name'],
+                        interface.get('vpci') if interface.get('vpci') else ""))
     else:
         print content['error']['description']
         if args.verbose:
@@ -397,20 +449,38 @@ def vnf_delete(args):
     return result
 
 def scenario_create(args):
-    #print "scenario-create",args
+    # print "scenario-create",args
     tenant = _get_tenant()
     headers_req = {'content-type': 'application/yaml'}
     myscenario = _load_file_or_yaml(args.file)
-
+    if "nsd:nsd-catalog" in myscenario or "nsd-catalog" in myscenario:
+        api_version = "/v3"
+        token = "nsd"
+        nsd_catalog = myscenario.get("nsd:nsd-catalog")
+        if not nsd_catalog:
+            nsd_catalog = myscenario.get("nsd-catalog")
+        nsds = nsd_catalog.get("nsd:nsd")
+        if not nsds:
+            nsds = nsd_catalog.get("nsd")
+        nsd = nsds[0]
+    else:  # API<v3
+        api_version = ""
+        token = "scenarios"
+        if "scenario" in myscenario:
+            nsd = myscenario["scenario"]
+        else:
+            nsd = myscenario
+    # TODO modify for API v3
     if args.name:
-        myscenario['name'] = args.name
+        nsd['name'] = args.name
     if args.description:
-        myscenario['description'] = args.description
-    payload_req = yaml.safe_dump(myscenario, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True)
+        nsd['description'] = args.description
+    payload_req = yaml.safe_dump(myscenario, explicit_start=True, indent=4, default_flow_style=False, tags=False,
+                                 encoding='utf-8', allow_unicode=True)
     
-    #print payload_req
-        
-    URLrequest = "http://%s:%s/openmano/%s/scenarios" %(mano_host, mano_port, tenant)
+    # print payload_req
+    URLrequest = "http://{host}:{port}/openmano{api}/{tenant}/{token}".format(
+        host=mano_host, port=mano_port, api=api_version, tenant=tenant, token=token)
     logger.debug("openmano request: %s", payload_req)
     mano_response = requests.post(URLrequest, headers = headers_req, data=payload_req)
     logger.debug("openmano response: %s", mano_response.text )
@@ -444,36 +514,41 @@ def scenario_list(args):
                 print "No scenarios were found."
                 return 404 #HTTP_Not_Found
             for scenario in content['scenarios']:
-                myoutput = "%s %s" %(scenario['uuid'].ljust(38),scenario['name'].ljust(20))
-                if args.verbose >=1:
-                    myoutput = "%s %s" %(myoutput, scenario['created_at'].ljust(20))
-                print myoutput
+                myoutput = "{:38} {:20}".format(scenario['uuid'], scenario['name'])
+                if scenario.get('osm_id') or args.verbose >= 1:
+                    myoutput += " osm_id={:20}".format(scenario.get('osm_id'))
+                if args.verbose >= 1:
+                    myoutput += " {}".format(scenario['created_at'])
+                print (myoutput)
                 if args.verbose >=2:
-                    print "  Description: %s" %scenario['description']
+                    print ("  Description: {}".format(scenario['description']))
         else:
             if args.verbose:
                 print yaml.safe_dump(content, indent=4, default_flow_style=False)
                 return result
             scenario = content['scenario']
-            myoutput = "%s %s %s" %(scenario['uuid'].ljust(38),scenario['name'].ljust(20), scenario['created_at'].ljust(20))
-            print myoutput
-            print "  Description: %s" %scenario['description']
-            print "    VNFs:"
+            print ("{:38} {:20} osm_id={:20} {:20}".format(scenario['uuid'], scenario['name'], scenario.get('osm_id'),
+                                                           scenario['created_at']))
+            print ("  Description: {}".format(scenario['description']))
+            print ("  VNFs:")
             for vnf in scenario['vnfs']:
-                print "        %s %s %s" %(vnf['name'].ljust(20), vnf['vnf_id'].ljust(38), vnf['description'])
-            if len(scenario['nets'])>0:
-                print "    Internal nets:"
+                print ("    {:38} {:20} vnf_index={} {}".format(vnf['vnf_id'], vnf['name'], vnf.get("member_vnf_index"),
+                                                                vnf['description']))
+            if len(scenario['nets']) > 0:
+                print ("  nets:")
                 for net in scenario['nets']:
-                    if net['description'] is None:   #if description does not exist, description is "-". Valid for external and internal nets.
-                        net['description'] = '-' 
-                    if not net['external']:
-                        print "        %s %s %s" %(net['name'].ljust(20), net['uuid'].ljust(38), net['description'].ljust(30))
-                print "    External nets:"
-                for net in scenario['nets']:
-                    if net['external']:
-                        print "        %s %s %s vim-id:%s" %(net['name'].ljust(20), net['uuid'].ljust(38), net['description'].ljust(30), net['vim_id'])
+                    description = net['description']
+                    if not description:   # if description does not exist, description is "-". Valid for external and internal nets.
+                        description = '-'
+                    vim_id = ""
+                    if net.get('vim_id'):
+                        vim_id = " vim_id=" + net["vim_id"]
+                    external = ""
+                    if net["external"]:
+                        external = " external"
+                    print ("    {:20} {:38} {:30}{}{}".format(net['name'], net['uuid'], description, vim_id, external))
     else:
-        print content['error']['description']
+        print (content['error']['description'])
         if args.verbose:
             print yaml.safe_dump(content, indent=4, default_flow_style=False)
     return result