Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / test / uploadServer / server.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
20 /*Define dependencies.*/
21
22 var express = require('express');
23 var multer = require('multer');
24 var cors = require('cors');
25 var launchpadServer = express();
26 var apiServer = express();
27 var done = false;
28 var path = require('path');
29
30 /*Configure the multer.*/
31
32 //apiServer.use(function(req, res, next) {
33 // res.header('Access-Control-Allow-Origin', '*');
34 // res.header('Access-Control-Allow-Headers', '*');
35 // next();
36 //});
37
38 var uploadPayload = {
39 "events": [
40 {
41 "timestamp": 1445452293.688965,
42 "value": "onboard-started",
43 "text": "onboarding process started"
44 },
45 {
46 "timestamp": 1445452293.7836523,
47 "value": "onboard-pkg-upload",
48 "text": "uploading package"
49 },
50 {
51 "timestamp": 1445452293.8315961,
52 "value": "onboard-img-upload",
53 "text": "uploading image"
54 },
55 {
56 "timestamp": 1445452293.8316283,
57 "value": "onboard-dsc-validation",
58 "text": "descriptor validation"
59 },
60 {
61 "timestamp": 1445452294.00448,
62 "value": "onboard-success",
63 "text": "onboarding process completed"
64 }
65 ],
66 "warnings": [],
67 "status": "success",
68 "errors": [
69 {
70 "value": "onboard vnfd/ping_vnfd.xml",
71 "timestamp": 1445452294.0032463
72 }
73 ]
74 };
75
76 var updatePayload = {
77 "events": [
78 {
79 "timestamp": 1445452293.688965,
80 "value": "update-started",
81 "text": "updating process started"
82 },
83 {
84 "timestamp": 1445452293.7836523,
85 "value": "update-pkg-upload",
86 "text": "uploading package"
87 },
88 {
89 "timestamp": 1445452293.8315961,
90 "value": "update-img-upload",
91 "text": "uploading image"
92 },
93 {
94 "timestamp": 1445452293.8316283,
95 "value": "update-dsc-validation",
96 "text": "descriptor validation"
97 },
98 {
99 "timestamp": 1445452294.00448,
100 "value": "update-success",
101 "text": "updating process completed"
102 }
103 ],
104 "warnings": [],
105 "status": "success",
106 "errors": [
107 {
108 "value": "update vnfd/ping_vnfd.xml",
109 "timestamp": 1445452294.0032463
110 }
111 ]
112 };
113
114 var exportPayload = {
115 "events": [
116 {
117 "timestamp": 1445452293.688965,
118 "value": "update-started",
119 "text": "download process started"
120 },
121 {
122 "timestamp": 1445452293.7836523,
123 "value": "update-pkg-upload",
124 "text": "building package"
125 },
126 {
127 "timestamp": 1445452293.8315961,
128 "value": "update-img-upload",
129 "text": "building image"
130 },
131 {
132 "timestamp": 1445452293.8316283,
133 "value": "update-dsc-validation",
134 "text": "descriptor validation"
135 },
136 {
137 "timestamp": 1445452294.00448,
138 "value": "download-success",
139 "text": "download process completed"
140 }
141 ],
142 "warnings": [],
143 "status": "success",
144 "errors": [
145 {
146 "value": "download nsd/ping-pong-nsd.xml",
147 "timestamp": 1445452294.0032463
148 }
149 ]
150 };
151
152 apiServer.use(cors());
153 launchpadServer.use(cors());
154
155 apiServer.use(multer({
156 dest: './uploads/',
157 rename: function (fieldname, filename) {
158 return filename + '-' + Date.now();
159 },
160 onFileUploadStart: function (file) {
161 console.log(file.originalname + ' is starting ...')
162 },
163 onFileUploadComplete: function (file) {
164 console.log(file.fieldname + ' uploaded to ' + file.path)
165 done = true;
166 }
167 }));
168
169 /*Handling routes.*/
170
171 launchpadServer.get('/launchpad/catalog', function (req, res) {
172 res.sendFile(path.join(__dirname, '../../src/assets/ping-pong-catalog.json'));
173 });
174
175 launchpadServer.delete('/launchpad/catalog/nsd/:id', function (req, res) {
176 res.end();
177 });
178
179 launchpadServer.put('/launchpad/catalog/nsd/:id', function (req, res) {
180 res.end();
181 });
182
183 apiServer.post('/api/upload', function (req, res) {
184 if (done == true) {
185 res.end('{"transaction_id": "' + Date.now() + '"}');
186 }
187 });
188
189 apiServer.get('/api/upload/:transaction_id/state', function (req, res) {
190 if (done == true) {
191 console.log('transaction id state request', req.params.transaction_id);
192 res.end(JSON.stringify(uploadPayload));
193 }
194 });
195
196 apiServer.post('/api/update', function (req, res) {
197 if (done == true) {
198 res.end('{"transaction_id": "' + Date.now() + '"}');
199 }
200 });
201
202 apiServer.get('/api/update/:transaction_id/state', function (req, res) {
203 if (done == true) {
204 console.log('transaction id state request', req.params.transaction_id);
205 res.end(JSON.stringify(updatePayload));
206 }
207 });
208
209 apiServer.get('/api/export', function (req, res) {
210 res.end('{"transaction_id": "' + Date.now() + '"}');
211 });
212
213 apiServer.get('/api/export/:transaction_id/state', function (req, res) {
214 console.log('transaction id state request', req.params.transaction_id);
215 res.end(JSON.stringify(exportPayload));
216 });
217
218 /*Run the servers.*/
219 launchpadServer.listen(3000, function () {
220 console.log('LaunchPad Server listening on port 3000');
221 });
222
223 apiServer.listen(4567, function () {
224 console.log('API Server listening on port 4567');
225 });