4 * Copyright 2016 RIFT.IO Inc
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
30 function ajaxRequest(path
, catalogPackage
, resolve
, reject
, method
= 'GET', input
, urlOverride
) {
32 url
: getApiServerOrigin() + path
,
34 beforeSend
: Utils
.addAuthorizationStub
,
36 success: function(data
) {
37 if (typeof data
== 'string') {
38 data
= JSON
.parse(data
);
45 error: function(error
) {
46 if (typeof error
== 'string') {
47 error
= JSON
.parse(error
);
59 options
.url
= window
.location
.origin
+ path
;
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
);
69 const CatalogPackageManagerSource
= {
71 requestCatalogPackageDownload: function () {
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';
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.
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"
93 const path
= "/composer/api/package-export?api_server=" + utils
.getSearchParams(window
.location
).api_server
;
94 ajaxRequest(path
, download
, setStatusBeforeResolve
, reject
, 'POST', data
, true);
96 //.then(function(data) {
97 // let filename = data.data.output.filename;
98 // window.open(getApiServerOrigin() + "/api/export/" + filename, "_blank")
101 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
102 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
106 requestCatalogPackageDownloadStatus: function() {
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
);
115 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
116 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
120 requestCatalogPackageUploadStatus: function () {
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
);
130 success
: CatalogPackageManagerActions
.uploadCatalogPackageStatusUpdated
,
131 error
: CatalogPackageManagerActions
.uploadCatalogPackageError
137 export default CatalogPackageManagerSource
;