Revert "BUG-410 -- update RIFT platform"
[osm/UI.git] / skyquake / framework / core / modules / api / schemaAPI.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 // DescriptorModelMeta API (NSD + VNFD)
19
20
21 var Schema = {};
22 var rp = require('request-promise');
23 var Promise = require('promise');
24 var constants = require('../../api_utils/constants');
25 var utils = require('../../api_utils/utils');
26 var _ = require('lodash');
27 var cors = require('cors');
28 var bodyParser = require('body-parser');
29 var utils = require('../../api_utils/utils');
30 var configuration = require('./configuration');
31
32 var router = require('express').Router();
33
34
35 router.use(bodyParser.json());
36 router.use(cors());
37 router.use(bodyParser.urlencoded({
38 extended: true
39 }));
40
41 router.get('/schema', cors(), function (req, res) {
42 getSchema(req).then(function (response) {
43 utils.sendSuccessResponse(response, res);
44 }, function (error) {
45 utils.sendErrorResponse(error, res);
46 });
47 });
48
49 module.exports = {
50 getRouter: function () {
51 return router;
52 },
53 init: function () {}
54 };
55
56 getSchema = function (req) {
57 var schemaURI = configuration.getBackendURL() + '/api/schema/';
58 var schemaPaths = req.query['request'];
59 var paths = schemaPaths.split(',');
60
61 function getSchemaRequest(path) {
62 return rp({
63 uri: schemaURI + path,
64 method: 'GET',
65 headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, {
66 'Authorization': req.session && req.session.authorization
67 }),
68 forever: constants.FOREVER_ON,
69 rejectUnauthorized: false,
70 resolveWithFullResponse: true
71 })
72 }
73
74 var requests = _.map(paths, getSchemaRequest);
75
76 return new Promise(function (resolve, reject) {
77 Promise.all(requests).then(
78 function (results) {
79 var data = {
80 schema: {}
81 }
82 _.forEach(results, function (result, index) {
83 data.schema[paths[index]] = JSON.parse(result.body);
84 });
85 resolve({
86 data: data,
87 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK
88 });
89 }).catch(
90 function (error) {
91 var response = {};
92 console.log('Problem with schema.get', error);
93 response.statusCode = error.statusCode || 500;
94 response.errorMessage = {
95 error: 'Failed to get schema' + error
96 };
97 reject(response);
98 });
99 });
100 };