Package Mananger
[osm/UI.git] / skyquake / plugins / composer / src / src / sources / CatalogPackageManagerSource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import $ from 'jquery'
22 import alt from '../alt'
23 import utils from '../libraries/utils'
24 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions'
25 let Utils = require('utils/utils.js');
26 function getApiServerOrigin() {
27 return utils.getSearchParams(window.location).upload_server + ':4567';
28 }
29
30 function ajaxRequest(path, catalogPackage, resolve, reject, method = 'GET', input, urlOverride) {
31 let options = {
32 url: getApiServerOrigin() + path,
33 type: method,
34 beforeSend: Utils.addAuthorizationStub,
35 dataType: 'json',
36 success: function(data) {
37 if (typeof data == 'string') {
38 data = JSON.parse(data);
39 }
40 resolve({
41 data: data,
42 state: catalogPackage
43 });
44 },
45 error: function(error) {
46 if (typeof error == 'string') {
47 error = JSON.parse(error);
48 }
49 reject({
50 data: error,
51 state: catalogPackage
52 });
53 }
54 };
55 if(input) {
56 options.data = input;
57 }
58 if (urlOverride) {
59 options.url = window.location.origin + path;
60 }
61 $.ajax(options).fail(function(xhr){
62 //Authentication and the handling of fail states should be wrapped up into a connection class.
63 Utils.checkAuthentication(xhr.status);
64 });
65 }
66
67
68
69 const CatalogPackageManagerSource = {
70
71 requestCatalogPackageDownload: function () {
72 return {
73 remote: function (state, download, format, grammar, schema) {
74 return new Promise((resolve, reject) => {
75 // the server does not add a status in the payload
76 // so we add one so that the success handler will
77 // be able to follow the flow of this download
78 const setStatusBeforeResolve = (response = {}) => {
79 response.data.status = 'download-requested';
80 resolve(response);
81 };
82 // RIFT-13485 requires to send type (nsd/vnfd) as a path element.
83 // Backend no longer supports mixed multi-package download.
84 // Probably does not even support multi-package download of same type.
85 // Hence, pick the type from the first element.
86 const data = {
87 "package-type": download['catalogItems'][0]['uiState']['type'].toUpperCase(),
88 "package-id": download.ids,
89 "export-format": format && format.toUpperCase() || 'YAML',
90 "export-grammar": grammar && grammar.toUpperCase() || 'OSM',
91 "export-schema": schema && schema.toUpperCase() || "RIFT"
92 }
93 const path = "/composer/api/package-export?api_server=" + utils.getSearchParams(window.location).api_server;
94 ajaxRequest(path, download, setStatusBeforeResolve, reject, 'POST', data, true);
95 })
96 //.then(function(data) {
97 // let filename = data.data.output.filename;
98 // window.open(getApiServerOrigin() + "/api/export/" + filename, "_blank")
99 //});
100 },
101 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
102 error: CatalogPackageManagerActions.downloadCatalogPackageError
103 };
104 },
105
106 requestCatalogPackageDownloadStatus: function() {
107 return {
108 remote: function(state, download) {
109 const transactionId = download.transactionId;
110 return new Promise(function(resolve, reject) {
111 const path = '/api/export/' + transactionId + '/state';
112 ajaxRequest(path, download, resolve, reject);
113 });
114 },
115 success: CatalogPackageManagerActions.downloadCatalogPackageStatusUpdated,
116 error: CatalogPackageManagerActions.downloadCatalogPackageError
117 }
118 },
119
120 requestCatalogPackageUploadStatus: function () {
121 return {
122 remote: function (state, upload) {
123 const transactionId = upload.transactionId;
124 return new Promise(function (resolve, reject) {
125 const action = upload.riftAction === 'onboard' ? 'upload' : 'update';
126 const path = '/api/' + action + '/' + transactionId + '/state';
127 ajaxRequest(path, upload, resolve, reject);
128 });
129 },
130 success: CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated,
131 error: CatalogPackageManagerActions.uploadCatalogPackageError
132 };
133 }
134
135 };
136
137 export default CatalogPackageManagerSource;