NS/NSI instantiation with capability to open files for config and for SSH keys 43/7443/1
authorlombardofr <lombardo@everyup.it>
Tue, 30 Apr 2019 15:28:22 +0000 (17:28 +0200)
committerlombardofr <lombardo@everyup.it>
Tue, 30 Apr 2019 15:28:22 +0000 (17:28 +0200)
Change-Id: Ib5ebe162ac5e5f00fd02ce3c31be8783361e7b88
Signed-off-by: lombardofr <lombardo@everyup.it>
instancehandler/template/modal/instance_create.html
instancehandler/template/modal/instance_create_nsi.html
instancehandler/views.py
projecthandler/template/project/projectlist.html
static/src/descriptorhandler/composer.js

index 8b4d40c..456579d 100644 (file)
                         </div>
                     </div>
                     <div class="form-group">
-                        <label for="ssh_key" class="col-sm-3 control-label">SSH Key </label>
+                        <label for="ssh_keys" class="col-sm-3 control-label">SSH Key </label>
                         <div class="col-sm-6">
-                            <textarea class="form-control" id="ssh_key" name="ssh_key"
-                                      placeholder="Paste your key here..." rows="4"></textarea>
+                                <textarea class="form-control" id="ssh_key" name="ssh_key"
+                                placeholder="Paste your key here..." rows="3"></textarea> 
+                                <p class="help-block">Or load from file</p>
+                                <input type="file" id="ssh_key_files" name="ssh_key_files" multiple>
+                               
+                             
                         </div>
                     </div>
                     <div class="form-group">
                         <label for="config" class="col-sm-3 control-label">Config </label>
                         <div class="col-sm-6">
                             <textarea class="form-control" id="config" name="config" placeholder="Yaml config"
-                                      rows="4"></textarea>
+                                      rows="3"></textarea>
+                            <p class="help-block">Or load from file</p>
+                            <input type="file" id="config_file" name="config_file">
                         </div>
                     </div>
 
index bad979f..21b410a 100644 (file)
                         </div>
                     </div>
                     <div class="form-group">
-                        <label for="ssh_key_nsi" class="col-sm-3 control-label">SSH Key </label>
+                        <label for="ssh_keys" class="col-sm-3 control-label">SSH Key </label>
                         <div class="col-sm-6">
-                            <textarea class="form-control" id="ssh_key_nsi" name="ssh_key"
-                                      placeholder="Paste your key here..." rows="4"></textarea>
+                                <textarea class="form-control" id="ssh_key" name="ssh_key"
+                                placeholder="Paste your key here..." rows="3"></textarea> 
+                                <p class="help-block">Or load from file</p>
+                                <input type="file" id="ssh_key_files" name="ssh_key_files" multiple>
+                               
+                             
                         </div>
                     </div>
                     <div class="form-group">
-                        <label for="config_nsi" class="col-sm-3 control-label">Config </label>
+                        <label for="config" class="col-sm-3 control-label">Config </label>
                         <div class="col-sm-6">
-                            <textarea class="form-control" id="config_nsi" name="config" placeholder="Yaml config"
-                                      rows="4"></textarea>
+                            <textarea class="form-control" id="config" name="config" placeholder="Yaml config"
+                                      rows="3"></textarea>
+                            <p class="help-block">Or load from file</p>
+                            <input type="file" id="config_file" name="config_file">
                         </div>
-                    </div> 
-
+                    </div>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
index 9dcb037..bdc85a7 100644 (file)
@@ -58,9 +58,36 @@ def list(request, type=None):
 def create(request, type=None):
     result = {}
     config_vim_account_id = {}
+    config_wim_account_id = {}
     user = osmutils.get_user(request)
     client = Client()
 
+    def get_vim_account_id(vim_account):
+        if config_vim_account_id.get(vim_account):
+            return config_vim_account_id[vim_account]
+        result_client = client.vim_list(user.get_token())
+        vim_list = result_client['data'] if result_client and result_client['error'] is False else []
+        if vim_list is None or len(vim_list) == 0:
+            raise ValueError("cannot find vim account '{}'".format(vim_account))
+        for vim in vim_list:
+            if vim_account == vim['name']:
+                config_vim_account_id[vim_account] = vim['uuid']
+                return vim['uuid']
+    
+    def get_wim_account_id(wim_account):
+
+        if config_wim_account_id.get(wim_account):
+            return config_wim_account_id[wim_account]
+        result_client = client.wim_list(user.get_token())
+        wim_list = result_client['data'] if result_client and result_client['error'] is False else []
+        if wim_list is None or len(wim_list) == 0:
+            raise ValueError("cannot find wim account '{}'".format(wim_account))
+        for wim in wim_list:
+            if wim_account == wim['name']:
+                config_wim_account_id[wim_account] = wim['uuid']
+                return wim['uuid']
+
+
     if type == 'ns':
         try:
 
@@ -70,36 +97,72 @@ def create(request, type=None):
                 "nsdId": request.POST.get('nsdId', ''),
                 "vimAccountId": request.POST.get('vimAccountId', ''),
             }
+            ns_data["ssh_keys"] = []
             if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
-                ns_data["ssh_keys"] = [request.POST.get('ssh_key')]
+                ns_data["ssh_keys"].append(request.POST.get('ssh_key'))
+            ssh_key_files = request.FILES.getlist('ssh_key_files')
+            for ssh_key_file in ssh_key_files:
+                ssh_key = ''
+                for line in ssh_key_file:
+                    ssh_key = ssh_key + line.decode()
+                ns_data["ssh_keys"].append(ssh_key)
+            
 
-            if 'config' in request.POST:
+            config_file = request.FILES.get('config_file')
+            
+            if config_file is not None:
+                config = ''
+                for line in config_file:
+                    config = config + line.decode()
+                ns_config = yaml.load(config)
+            elif 'config' in request.POST and request.POST.get('config') != '':
                 ns_config = yaml.load(request.POST.get('config'))
+            else:
+                ns_config = None
+            
+           
+            if ns_config is not None:
                 if isinstance(ns_config, dict):
                     if "vim-network-name" in ns_config:
                         ns_config["vld"] = ns_config.pop("vim-network-name")
                     if "vld" in ns_config:
-                        print ns_config
                         for vld in ns_config["vld"]:
                             if vld.get("vim-network-name"):
                                 if isinstance(vld["vim-network-name"], dict):
                                     vim_network_name_dict = {}
-                                    for vim_account, vim_net in vld["vim-network-name"].items():
-                                        vim_network_name_dict[ns_data["vimAccountId"]] = vim_net
+                                    for vim_account, vim_net in list(vld["vim-network-name"].items()):
+                                        vim_network_name_dict[get_vim_account_id(vim_account)] = vim_net
                                     vld["vim-network-name"] = vim_network_name_dict
+                            if "wim_account" in vld and vld["wim_account"] is not None:
+                                vld["wimAccountId"] = get_wim_account_id(vld.pop("wim_account"))
                         ns_data["vld"] = ns_config["vld"]
                     if "vnf" in ns_config:
                         for vnf in ns_config["vnf"]:
                             if vnf.get("vim_account"):
-                                vnf["vimAccountId"] = ns_data["vimAccountId"]
-
+                                vnf["vimAccountId"] = get_vim_account_id(vnf.pop("vim_account"))
                         ns_data["vnf"] = ns_config["vnf"]
+
                     if "additionalParamsForNs" in ns_config:
-                        ns_data["additionalParamsForNs"] = ns_config["additionalParamsForNs"]
+                        ns_data["additionalParamsForNs"] = ns_config.pop("additionalParamsForNs")
+                        if not isinstance(ns_data["additionalParamsForNs"], dict):
+                            raise ValueError("Error  'additionalParamsForNs' must be a dictionary")
                     if "additionalParamsForVnf" in ns_config:
-                        ns_data["additionalParamsForVnf"] = ns_config["additionalParamsForVnf"]
-                    if "wimAccountId" in ns_config:
-                        ns_data["wimAccountId"] = ns_config["wimAccountId"]
+                        ns_data["additionalParamsForVnf"] = ns_config.pop("additionalParamsForVnf")
+                        if not isinstance(ns_data["additionalParamsForVnf"], list):
+                            raise ValueError("Error  'additionalParamsForVnf' must be a list")
+                        for additional_param_vnf in ns_data["additionalParamsForVnf"]:
+                            if not isinstance(additional_param_vnf, dict):
+                                raise ValueError("Error  'additionalParamsForVnf' items must be dictionaries")
+                            if not additional_param_vnf.get("member-vnf-index"):
+                                raise ValueError("Error  'additionalParamsForVnf' items must contain "
+                                                "'member-vnf-index'")
+                            if not additional_param_vnf.get("additionalParams"):
+                                raise ValueError("Error  'additionalParamsForVnf' items must contain "
+                                                "'additionalParams'")
+                    if "wim_account" in ns_config:
+                        wim_account = ns_config.pop("wim_account")
+                        if wim_account is not None:
+                            ns_data['wimAccountId'] = get_wim_account_id(wim_account)
 
         except Exception as e:
             request.session["OSM_ERROR"] = "Error creating the NS; Invalid parameters provided."
@@ -116,11 +179,75 @@ def create(request, type=None):
                 "vimAccountId": request.POST.get('vimAccountId', ''),
             }
             
+            nsi_data["ssh_keys"] = []
             if 'ssh_key' in request.POST and request.POST.get('ssh_key') != '':
-                nsi_data["ssh_keys"] = [request.POST.get('ssh_key')]
+                nsi_data["ssh_keys"].append(request.POST.get('ssh_key'))
+            ssh_key_files = request.FILES.getlist('ssh_key_files')
+            for ssh_key_file in ssh_key_files:
+                ssh_key = ''
+                for line in ssh_key_file:
+                    ssh_key = ssh_key + line.decode()
+                nsi_data["ssh_keys"].append(ssh_key)
+            nsi_data["ssh_keys"] = ','.join(nsi_data["ssh_keys"])
+          
+            config_file = request.FILES.get('config_file')
+            
+            if config_file is not None:
+                config = ''
+                for line in config_file:
+                    config = config + line.decode()
+                nsi_config = yaml.load(config)
+            elif 'config' in request.POST and request.POST.get('config') != '':
+                nsi_config = yaml.load(request.POST.get('config'))
+            else:
+                nsi_config = None
+            
+            if nsi_config is not None:
+                if "netslice-vld" in nsi_config:
+                    for vld in nsi_config["netslice-vld"]:
+                        if vld.get("vim-network-name"):
+                            if isinstance(vld["vim-network-name"], dict):
+                                vim_network_name_dict = {}
+                                for vim_account, vim_net in list(vld["vim-network-name"].items()):
+                                    vim_network_name_dict[get_vim_account_id(vim_account)] = vim_net
+                                vld["vim-network-name"] = vim_network_name_dict
+                    nsi_data["netslice-vld"] = nsi_config["netslice-vld"]
+                if "netslice-subnet" in nsi_config:
+                    for nssubnet in nsi_config["netslice-subnet"]:
+                        if "vld" in nssubnet:
+                            for vld in nssubnet["vld"]:
+                                if vld.get("vim-network-name"):
+                                    if isinstance(vld["vim-network-name"], dict):
+                                        vim_network_name_dict = {}
+                                        for vim_account, vim_net in list(vld["vim-network-name"].items()):
+                                            vim_network_name_dict[get_vim_account_id(vim_account)] = vim_net
+                                        vld["vim-network-name"] = vim_network_name_dict
+                        if "vnf" in nssubnet:
+                            for vnf in nsi_config["vnf"]:
+                                if vnf.get("vim_account"):
+                                    vnf["vimAccountId"] = get_vim_account_id(vnf.pop("vim_account"))
+                    nsi_data["netslice-subnet"] = nsi_config["netslice-subnet"]
+                if "additionalParamsForNsi" in nsi_config:
+                    nsi_data["additionalParamsForNsi"] = nsi_config.pop("additionalParamsForNsi")
+                    if not isinstance(nsi_data["additionalParamsForNsi"], dict):
+                        raise ValueError("Error at 'additionalParamsForNsi' must be a dictionary")
+                if "additionalParamsForSubnet" in nsi_config:
+                    nsi_data["additionalParamsForSubnet"] = nsi_config.pop("additionalParamsForSubnet")
+                    if not isinstance(nsi_data["additionalParamsForSubnet"], list):
+                        raise ValueError("Error 'additionalParamsForSubnet' must be a list")
+                    for additional_param_subnet in nsi_data["additionalParamsForSubnet"]:
+                        if not isinstance(additional_param_subnet, dict):
+                            raise ValueError("Error 'additionalParamsForSubnet' items must be dictionaries")
+                        if not additional_param_subnet.get("id"):
+                            raise ValueError("Error 'additionalParamsForSubnet' items must contain subnet 'id'")
+                        if not additional_param_subnet.get("additionalParamsForNs") and\
+                                not additional_param_subnet.get("additionalParamsForVnf"):
+                            raise ValueError("Error 'additionalParamsForSubnet' items must contain "
+                                            "'additionalParamsForNs' and/or 'additionalParamsForVnf'")
         except Exception as e:
             request.session["OSM_ERROR"] = "Error creating the NSI; Invalid parameters provided."
             return __response_handler(request, {}, 'instances:list', to_redirect=True, type=type)
+
         result = client.nsi_create(user.get_token(), nsi_data)
         return __response_handler(request, result, 'instances:list', to_redirect=True, type=type)
 
@@ -191,7 +318,6 @@ def action(request, instance_id=None, type=None):
     }
 
     result = client.ns_action(user.get_token(), instance_id, action_payload)
-    print result
     if result['error']:
         return __response_handler(request, result['data'], url=None,
                                   status=result['data']['status'] if 'status' in result['data'] else 500)
@@ -262,7 +388,7 @@ def show(request, instance_id=None, type=None):
         result = client.pdu_get(user.get_token(), instance_id)
     elif type == 'nsi':
         result = client.nsi_get(user.get_token(), instance_id)
-    print result
+
     return __response_handler(request, result)
 
 
@@ -283,7 +409,6 @@ def export_metric(request, instance_id=None, type=None):
     result = client.ns_metric_export(user.get_token(), instance_id, metric_data)
 
     if result['error']:
-        print result
         return __response_handler(request, result['data'], url=None,
                                   status=result['data']['status'] if 'status' in result['data'] else 500)
     else:
@@ -293,7 +418,6 @@ def export_metric(request, instance_id=None, type=None):
 @login_required
 def create_alarm(request, instance_id=None, type=None):
     metric_data = request.POST.dict()
-    print metric_data
     user = osmutils.get_user(request)
     project_id = user.project_id
     client = Client()
@@ -311,7 +435,6 @@ def create_alarm(request, instance_id=None, type=None):
 
     result = client.ns_alarm_create(user.get_token(), instance_id, metric_data)
     if result['error']:
-        print result
         return __response_handler(request, result['data'], url=None,
                                   status=result['data']['status'] if 'status' in result['data'] else 500)
     else:
index e8cc77a..28888fc 100644 (file)
@@ -28,7 +28,6 @@
  {% csrf_token %}
 <div class="row">
 <div class="col-md-12">
-
     <div class="box">
         <div class="box-header with-border">
                   <h3 class="box-title">Projects</h3>
@@ -43,9 +42,7 @@
                        <table id="projects_table" class="table table-bordered table-striped">
                                <thead>
                                        <tr>
-
                                                <th>Name</th>
-
                                                <th>Modification Date</th>
                                                <th>Creation Date</th>
                                                <th>Actions</th>
 
     function deleteProject(project_id) {
         var url = "/projects/" + project_id+"/delete";
-    bootbox.confirm("Are you sure want to delete?", function (result) {
-        if (result) {
-            $.ajax({
-            url: url,
-            type: 'GET',
-            headers: {
-                "Accept": 'application/json'
-            },
-            contentType: false,
-            processData: false
-        }).done(function (response,textStatus, jqXHR) {
-
-            bootbox.alert({
-                title: "Result",
-                message: "Project deleted.",
-                callback: function () {
-                    location.reload();
-                }
-            });
-        }).fail(function(result){
-            var data  = result.responseJSON;
-            var title = "Error " + (data.code ? data.code: 'unknown');
-                var message = data.detail ? data.detail: 'No detail available.';
-                bootbox.alert({
-                    title: title,
-                    message: message
+        bootbox.confirm("Are you sure want to delete?", function (result) {
+            if (result) {
+                $.ajax({
+                    url: url,
+                    type: 'GET',
+                    headers: {
+                        "Accept": 'application/json'
+                    },
+                    contentType: false,
+                    processData: false
+                }).done(function (response,textStatus, jqXHR) {
+                    bootbox.alert({
+                        title: "Result",
+                        message: "Project deleted.",
+                        callback: function () {
+                            location.reload();
+                        }
+                    });
+                }).fail(function(result){
+                    var data  = result.responseJSON;
+                    var title = "Error " + (data.code ? data.code: 'unknown');
+                    var message = data.detail ? data.detail: 'No detail available.';
+                    bootbox.alert({
+                        title: title,
+                        message: message
+                    });
                 });
-        });
-        }
-    })
+            }
+        })
     }
 
     $(document).ready(function () {
-    $("#formNewProject").submit(function (event) {
-        event.preventDefault(); //prevent default action
-        var post_url = $(this).attr("action"); //get form action url
-        var request_method = $(this).attr("method"); //get form GET/POST method
-        var form_data = new FormData(this); //Encode form elements for submission
-        console.log(post_url);
-        $.ajax({
-            url: post_url,
-            type: request_method,
-            data: form_data,
-            headers: {
-                "Accept": 'application/json'
-            },
-            contentType: false,
-            processData: false
-        }).done(function (response,textStatus, jqXHR) {
-            bootbox.alert({
-                    title: "Result",
-                    message: "Project created.",
-                callback: function () {
-                    location.reload();
-                }
-                });
-        }).fail(function(result){
-            var data  = result.responseJSON;
-            var title = "Error " + (data.code ? data.code: 'unknown');
-                var message = data.detail ? data.detail: 'No detail available.';
+        $("#formNewProject").submit(function (event) {
+            event.preventDefault(); //prevent default action
+            var post_url = $(this).attr("action"); //get form action url
+            var request_method = $(this).attr("method"); //get form GET/POST method
+            var form_data = new FormData(this); //Encode form elements for submission
+            console.log(post_url);
+            $.ajax({
+                url: post_url,
+                type: request_method,
+                data: form_data,
+                headers: {
+                    "Accept": 'application/json'
+                },
+                contentType: false,
+                processData: false
+            }).done(function (response,textStatus, jqXHR) {
                 bootbox.alert({
-                    title: title,
-                    message: message
-                });
+                        title: "Result",
+                        message: "Project created.",
+                    callback: function () {
+                        location.reload();
+                    }
+                    });
+            }).fail(function(result){
+                var data  = result.responseJSON;
+                var title = "Error " + (data.code ? data.code: 'unknown');
+                    var message = data.detail ? data.detail: 'No detail available.';
+                    bootbox.alert({
+                        title: title,
+                        message: message
+                    });
+            });
         });
     });
-    });
 
 
 
index 8f5e870..aef680f 100644 (file)
@@ -266,7 +266,7 @@ function updatePalette(view) {
         var nodes_properties = osm_gui_properties['nodes'];
         for (var n in nodes) {
             var node = nodes[n];
-        if (nodes_properties[node] && (nodes_properties[node].draggable != false)) {
+            if (nodes_properties[node] && (nodes_properties[node].draggable != false)) {
                 palette_template += '<div id="drag_' + n + '" class="node ui-draggable"' +
                     'type-name="' + node + '" draggable="true" ondragstart="nodeDragStart(event)">' +
                     '<div class="icon" style="background-color:' + nodes_properties[node].color + '"></div>' +