Network Slice Templates
Change-Id: I9453c518cc717e514598e40e2b1ce5ff60b76108
Signed-off-by: lombardofr <lombardo@everyup.it>
diff --git a/netslicehandler/__init__.py b/netslicehandler/__init__.py
new file mode 100644
index 0000000..00de7ab
--- /dev/null
+++ b/netslicehandler/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
\ No newline at end of file
diff --git a/netslicehandler/apps.py b/netslicehandler/apps.py
new file mode 100644
index 0000000..e887243
--- /dev/null
+++ b/netslicehandler/apps.py
@@ -0,0 +1,21 @@
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import unicode_literals
+
+from django.apps import AppConfig
+
+
+class NetslicehandlerConfig(AppConfig):
+ name = 'netslicehandler'
diff --git a/netslicehandler/migrations/__init__.py b/netslicehandler/migrations/__init__.py
new file mode 100644
index 0000000..00de7ab
--- /dev/null
+++ b/netslicehandler/migrations/__init__.py
@@ -0,0 +1,13 @@
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
\ No newline at end of file
diff --git a/netslicehandler/template/modal/nst_details.html b/netslicehandler/template/modal/nst_details.html
new file mode 100644
index 0000000..6537008
--- /dev/null
+++ b/netslicehandler/template/modal/nst_details.html
@@ -0,0 +1,23 @@
+<div class="modal" id="modal_show_nst" xmlns="http://www.w3.org/1999/html">
+ <div class="modal-dialog">
+ <div class="modal-content">
+ <div class="modal-header">
+ <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+ <span aria-hidden="true">×</span></button>
+ <h4 class="modal-title" >Network Slices Template details</h4>
+ </div>
+
+ <div class="modal-body" id="modal_show_nst_body" >
+ <textarea id="nst_view_json">
+ </textarea>
+ </div>
+ <div class="modal-footer">
+ <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
+
+ </div>
+
+ </div>
+ <!-- /.modal-content -->
+ </div>
+ <!-- /.modal-dialog -->
+</div>
diff --git a/netslicehandler/template/nst_edit.html b/netslicehandler/template/nst_edit.html
new file mode 100644
index 0000000..3cce0a8
--- /dev/null
+++ b/netslicehandler/template/nst_edit.html
@@ -0,0 +1,177 @@
+{% extends "base.html" %}
+{% load get %}
+{% load staticfiles %}
+
+
+{% block head_block %}
+{{ block.super }}
+<link rel="stylesheet" href="/static/bower_components/codemirror/lib/codemirror.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/fold/foldgutter.css" />
+<link rel="stylesheet" href="/static/bower_components/codemirror/theme/neat.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/dialog/dialog.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/display/fullscreen.css">
+{% endblock %}
+{% block title_header_big %}
+{{ block.super }}
+Network Slices Templates
+{% endblock %}
+{% block left_sidebar %}
+
+{% include 'osm/osm_project_left_sidebar.html' %}
+
+{% endblock %}
+
+
+{% block breadcrumb_body %}
+{{ block.super }}
+<li><a href="{% url "netslices:list_templates" %}">NetSlice Templates</a></li>
+{% endblock %}
+
+{% block content_body %}
+{{ block.super }}
+
+{% csrf_token %}
+<div class="row">
+ <div class="col-md-12">
+ <div class="nav-tabs-custom" style="position: relative;">
+ <ul class="nav nav-tabs" >
+ <li class="active" id="yaml_li"><a href="#yaml" data-toggle="tab"><i class="fa fa-file-code-o"></i> YAML</a></li>
+ <li class="pull-right"><button id="save" type="button" class="btn btn-block btn-primary btn-sm" onclick="update()" ><i class="fa fa-save"></i> Update</button></li>
+
+ </ul>
+ <div class="alert alert-success" id="success-alert" style="position: absolute; z-index: 100; top: 44px; left: 0; width: 100%;
+ border-radius: 1px;
+ background-color: rgba(0, 166, 90, 0.8) !important;
+ text-align: center;
+ border-color: rgba(0, 141, 76, 0.4);" hidden>
+ <button type="button" class="close" onclick="$('#success-alert').toggle()">x</button>
+ <strong>Success! </strong>
+ Template updated!
+ </div>
+ <div class="tab-content">
+ <div class="active tab-pane" id="yaml">
+ <textarea id="code_editor_yaml">
+ </textarea>
+ </div>
+ </div>
+ </div>
+
+ <!-- <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title"></h3>
+ <div class="box-tools"></div>
+ </div>
+ <div class="box-body">
+ <textarea id="code_editor_yaml">
+ </textarea>
+ </div>
+ </div> -->
+ </div>
+</div>
+
+{% endblock %}
+
+{% block resource_block %}
+{{ block.super }}
+<script src="/static/bower_components/codemirror/lib/codemirror.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/fold/foldcode.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/fold/foldgutter.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/fold/brace-fold.js" ></script>
+ <script src="/static/bower_components/codemirror/mode/javascript/javascript.js" ></script>
+ <script src="/static/bower_components/codemirror/mode/yaml/yaml.js" ></script>
+ <script src="/static/bower_components/codemirror/mode/markdown/markdown.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/search/searchcursor.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/search/search.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/dialog/dialog.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/display/autorefresh.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/edit/matchbrackets.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/edit/closebrackets.js" ></script>
+ <script src="/static/bower_components/codemirror/addon/display/fullscreen.js" ></script>
+ <script src="/static/bower_components/codemirror/keymap/sublime.js" ></script>
+<script>
+ var csrf_token = '{{csrf_token}}';
+
+ var table;
+ var yaml_editor_settings = {
+ mode: "yaml",
+ showCursorWhenSelecting: true,
+ autofocus: true,
+ autoRefresh: true,
+ lineNumbers: true,
+ lineWrapping: true,
+ foldGutter: true,
+ gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
+ autoCloseBrackets: true,
+ matchBrackets: true,
+ extraKeys: {
+ "F11": function (cm) {
+ cm.setOption("fullScreen", !cm.getOption("fullScreen"));
+ },
+ "Esc": function (cm) {
+ if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
+ },
+ "Ctrl-Q": function (cm) {
+ cm.foldCode(cm.getCursor());
+ }
+ },
+ theme: "neat",
+ keyMap: "sublime"
+ };
+
+
+ var myYamlTextArea = document.getElementById("code_editor_yaml");
+ var editorYaml = CodeMirror(function (elt) {
+ myYamlTextArea.parentNode.replaceChild(elt, myYamlTextArea);
+ }, yaml_editor_settings);
+
+ var template = {{ template | safe}};
+
+ $(document).ready(function () {
+
+ editorYaml.setValue(template.data);
+ editorYaml.setSize("auto", "auto");
+
+ });
+
+ function update() {
+ var dialog = bootbox.dialog({
+ message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
+ closeButton: false
+ });
+
+
+ text = editorYaml.getValue();
+
+ $.ajax({
+ url: window.location.href,
+ type: 'POST',
+ dataType: 'json',
+ data: {
+ 'csrfmiddlewaretoken': '{{csrf_token}}',
+ 'text': text
+ },
+ success: function (result) {
+
+ dialog.modal('hide');
+ $("#success-alert").fadeTo(2000, 500).slideDown(500, function(){
+ setTimeout(function () {
+ $("#success-alert").slideUp(500);
+ }, 2000);
+
+ });
+ },
+ error: function (result) {
+ dialog.modal('hide');
+ var data = result.responseJSON;
+ var title = "Error " + (data && data.code ? data.code : 'unknown');
+ var message = data && data.detail ? data.detail : 'No detail available.';
+ bootbox.alert({
+ title: title,
+ message: message
+ });
+ }
+ });
+ }
+</script>
+
+{% endblock %}
\ No newline at end of file
diff --git a/netslicehandler/template/nst_list.html b/netslicehandler/template/nst_list.html
new file mode 100644
index 0000000..41525ef
--- /dev/null
+++ b/netslicehandler/template/nst_list.html
@@ -0,0 +1,211 @@
+{% extends "base.html" %}
+{% load get %}
+{% load staticfiles %}
+
+
+{% block head_block %}
+{{ block.super }}
+<link rel="stylesheet" href="/static/bower_components/codemirror/lib/codemirror.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/fold/foldgutter.css" />
+<link rel="stylesheet" href="/static/bower_components/codemirror/theme/neat.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/dialog/dialog.css">
+<link rel="stylesheet" href="/static/bower_components/codemirror/addon/display/fullscreen.css">
+<link rel="stylesheet" href="/static/bower_components/select2/dist/css/select2.min.css">
+<link rel="stylesheet" href="/static/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
+{% endblock %}
+{% block title_header_big %}
+{{ block.super }}
+Network Slices Templates
+{% endblock %}
+{% block left_sidebar %}
+
+{% include 'osm/osm_project_left_sidebar.html' %}
+
+{% endblock %}
+
+
+{% block breadcrumb_body %}
+{{ block.super }}
+<li><a href="{% url "netslices:list_templates" %}">NetSlice Templates</a></li>
+{% endblock %}
+
+{% block content_body %}
+{{ block.super }}
+{% include 'modal/nst_details.html' %}
+{% csrf_token %}
+<div class="row">
+ <div class="col-md-12">
+
+ <div class="box">
+ <div class="box-header with-border">
+ <h3 class="box-title"></h3>
+ <div class="box-tools"></div>
+ </div>
+ <div class="box-body">
+ <table id="templates_table" class="table table-bordered table-striped dataTable" role="grid">
+ <thead>
+ <tr role="row">
+ <th>Name</th>
+ <th>Identifier</th>
+ <th>Usage State</th>
+ <th>Actions</th>
+ </tr>
+ </thead>
+ <tbody>
+
+ </tbody>
+
+ </table>
+ </div>
+ </div>
+ </div>
+</div>
+<div class="row">
+ <div class="col-sm-12">
+ <div class="box box-solid">
+ <div class="box-body">
+
+ <div class="upload-drop-zone" id="drop-zone">
+ <i class="fa fa-upload"></i> Just drag and drop files here
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
+
+{% endblock %}
+
+{% block resource_block %}
+{{ block.super }}
+<script src="/static/bower_components/select2/dist/js/select2.js"></script>
+<script src="/static/bower_components/codemirror/lib/codemirror.js"></script>
+<script src="/static/bower_components/codemirror/addon/fold/foldcode.js"></script>
+<script src="/static/bower_components/codemirror/addon/fold/foldgutter.js"></script>
+<script src="/static/bower_components/codemirror/addon/fold/brace-fold.js"></script>
+<script src="/static/bower_components/codemirror/mode/javascript/javascript.js"></script>
+<script src="/static/bower_components/codemirror/addon/search/searchcursor.js"></script>
+<script src="/static/bower_components/codemirror/addon/search/search.js"></script>
+<script src="/static/bower_components/codemirror/addon/dialog/dialog.js"></script>
+<script src="/static/bower_components/codemirror/addon/display/autorefresh.js"></script>
+<script src="/static/bower_components/codemirror/addon/edit/matchbrackets.js"></script>
+<script src="/static/bower_components/codemirror/addon/edit/closebrackets.js"></script>
+<script src="/static/bower_components/codemirror/addon/display/fullscreen.js"></script>
+<script src="/static/bower_components/codemirror/keymap/sublime.js"></script>
+<script src="/static/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
+<script src="/static/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
+<script src="/static/src/drop_zone.js"></script>
+<script src="/static/src/netslicehandler/onboard_template.js"></script>
+<script src="/static/src/netslicehandler/templates_list.js"></script>
+<script>
+ var csrf_token = '{{csrf_token}}';
+ var table;
+
+ var editorJSON;
+
+
+
+ $(document).ready(function () {
+ table = $('#templates_table').DataTable({
+ responsive: true,
+ "ajax": {
+ "url": "netslices/templates/list",
+ "dataSrc": function (json) {
+ return json['templates'];
+ },
+ statusCode: {
+ 401: function () {
+ console.log("no auth");
+ moveToLogin(window.location.pathname);
+ }
+ },
+ "error": function (hxr, error, thrown) {
+ console.log(hxr)
+ console.log(thrown)
+ console.log(error);
+ }
+
+ },
+ "columns": [
+ {
+ "render": function (data, type, row) {
+ return row['name'] || '';
+ },
+ "targets": 0
+ },
+ {
+ "render": function (data, type, row) {
+ return row['_id'];
+ },
+ "targets": 1
+ },
+ {
+ "render": function (data, type, row) {
+ return row['_admin']['usageState'];
+ },
+ "targets": 2
+ },
+ {
+ "render": function (data, type, row) {
+ return '<div class="btn-group">' +
+ ' <button type="button" class="btn btn-default"' +
+ ' onclick="javascript:showNstDetails(\'' + row["_id"] + '\')"' +
+ ' data-toggle="tooltip" data-placement="top" data-container="body" title="Open">' +
+ ' <i class="fa fa-info"></i>' +
+ ' </button>' +
+ ' <button type="button" class="btn btn-default"' +
+ ' onclick="window.location.href=\'/netslices/templates/'+row["_id"]+'/edit\'"' +
+ ' data-toggle="tooltip" data-placement="top" data-container="body" title="Edit">' +
+ ' <i class="fa fa-edit"></i>' +
+ ' </button>' +
+ '<button type="button" class="btn btn-default"' +
+ ' onclick="javascript:deleteTemplate(\'' + row["name"] + '\', \'' + row["_id"] + '\')"' +
+ ' data-toggle="tooltip" data-placement="top" data-container="body" title="Delete"><i' +
+ ' class="far fa-trash-alt"></i></button>' +
+ ' </div>';
+ },
+ "targets": 3,
+ "orderable": false
+ }
+ ]
+ });
+
+ setInterval(function () {
+ refreshTable();
+ }, 10000);
+
+ var json_editor_settings = {
+ mode: "javascript",
+ showCursorWhenSelecting: true,
+ autofocus: true,
+ lineNumbers: true,
+ lineWrapping: true,
+ foldGutter: true,
+ gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
+ autoCloseBrackets: true,
+ matchBrackets: true,
+ extraKeys: {
+ "F11": function (cm) {
+ cm.setOption("fullScreen", !cm.getOption("fullScreen"));
+ },
+ "Esc": function (cm) {
+ if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
+ },
+ "Ctrl-Q": function (cm) {
+ cm.foldCode(cm.getCursor());
+ }
+ },
+ theme: "neat",
+ keyMap: "sublime"
+ };
+ var myJsonTextArea = document.getElementById("nst_view_json");
+ editorJSON = CodeMirror(function (elt) {
+ myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
+ }, json_editor_settings);
+ });
+
+ function refreshTable() {
+ table.ajax.reload();
+ }
+</script>
+
+{% endblock %}
\ No newline at end of file
diff --git a/netslicehandler/urls.py b/netslicehandler/urls.py
new file mode 100644
index 0000000..097fba8
--- /dev/null
+++ b/netslicehandler/urls.py
@@ -0,0 +1,26 @@
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from django.conf.urls import url
+from netslicehandler import views
+
+urlpatterns = [
+ url(r'templates/list$', views.list, name='list_templates'),
+ url(r'templates/create', views.create_template, name='create_template'),
+ url(r'templates/onboard', views.onboard_template, name='onboard_template'),
+ url(r'templates/(?P<template_id>[-\w]+)/details', views.details, name='details'),
+ url(r'templates/(?P<template_id>[-\w]+)/edit', views.edit, name='edit'),
+ url(r'templates/(?P<template_id>[-\w]+)/delete', views.delete_template, name='delete_template'),
+ url(r'templates/(?P<template_id>[-\w]+)/download', views.download_template, name='download_template'),
+]
\ No newline at end of file
diff --git a/netslicehandler/views.py b/netslicehandler/views.py
new file mode 100644
index 0000000..4385cc0
--- /dev/null
+++ b/netslicehandler/views.py
@@ -0,0 +1,119 @@
+# Copyright 2018 EveryUP Srl
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import yaml
+import json
+import logging
+from sf_t3d.decorators import login_required
+from django.http import HttpResponse
+from django.shortcuts import render, redirect
+
+from lib.util import Util
+import authosm.utils as osmutils
+from lib.osm.osmclient.clientv2 import Client
+
+logging.basicConfig(level=logging.DEBUG)
+log = logging.getLogger('instancehandler/view.py')
+
+@login_required
+def list(request):
+ user = osmutils.get_user(request)
+ client = Client()
+ result = {}
+ result_client = client.nst_list(user.get_token())
+
+ result['templates'] = result_client['data'] if result_client and result_client['error'] is False else []
+
+ return __response_handler(request, result, 'nst_list.html')
+
+@login_required
+def create_template(request, template_id=None):
+ return
+
+@login_required
+def edit(request, template_id=None):
+ user = osmutils.get_user(request)
+ client = Client()
+ if request.method == 'GET':
+ page = 'nst_edit.html'
+ result = client.nst_content(user.get_token(), template_id)
+ if result['error']:
+ return __response_handler(request, result, url=page, status=500)
+ else:
+ return __response_handler(request, {'template': {'template_id': str(template_id), 'data': result['data']}}, url=page, status=200)
+ elif request.method == 'POST':
+ result = client.nst_content_update(user.get_token(), template_id, request.POST.get('text'))
+ if result['error'] == True:
+ return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
+ else:
+ return __response_handler(request, {}, url=None, status=200)
+
+
+@login_required
+def details(request, template_id=None):
+ user = osmutils.get_user(request)
+ client = Client()
+ result = client.nst_details(user.get_token(), template_id)
+ if result['error']:
+ return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
+ else:
+ return __response_handler(request, result, url=None, status=200)
+
+@login_required
+def delete_template(request, template_id=None):
+ user = osmutils.get_user(request)
+
+ client = Client()
+ result = client.nst_delete(user.get_token(), template_id)
+
+ if result['error']:
+ return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
+ else:
+ return __response_handler(request, {}, url=None, status=200)
+
+
+@login_required
+def download_template(request, package_id=None):
+ return
+
+@login_required
+def onboard_template(request):
+ user = osmutils.get_user(request)
+ if request.method == 'POST':
+ data_type = request.POST.get('type')
+ if data_type == "file":
+ file_uploaded = request.FILES['file']
+ try:
+ client = Client()
+ result = client.nst_onboard(user.get_token(), file_uploaded)
+ except Exception as e:
+ log.exception(e)
+ result = {'error': True, 'data': str(e)}
+ else:
+ result = {'error': True, 'data': 'Create descriptor: Unknown data type'}
+
+ if result['error']:
+ return __response_handler(request, result['data'], url=None, status=result['data']['status'] if 'status' in result['data'] else 500)
+ else:
+ return __response_handler(request, {}, url=None, status=200)
+
+
+def __response_handler(request, data_res, url=None, to_redirect=None, *args, **kwargs):
+ raw_content_types = request.META.get('HTTP_ACCEPT', '*/*').split(',')
+ if not to_redirect and ('application/json' in raw_content_types or url is None):
+ return HttpResponse(json.dumps(data_res), content_type="application/json", *args, **kwargs)
+ elif to_redirect:
+ return redirect(url, *args, **kwargs)
+ else:
+ return render(request, url, data_res)
\ No newline at end of file