update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / framework / core / modules / api / restconf.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
19 /**
20 * restconf api module. Provides API functions to interact with RESTCONF
21 * @module framework/core/modules/api/restconf
22 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
23 */
24
25 var Promise = require('bluebird');
26 var constants = require('../../api_utils/constants');
27 var utils = require('../../api_utils/utils');
28 var request = utils.request;
29 var restconfAPI = {};
30 var _ = require('lodash');
31 restconfAPI['streams'] = {};
32
33 /**
34 * Get the RESTCONF/Netconf streams from the RESTCONF endpoint
35 * @param {Object} req - the Express request object with or without a plugin_id
36 * @return {Function} Promise that resolves with success object or rejects
37 * with error
38 */
39 restconfAPI['streams'].get = function(req) {
40 var api_server = req.query["api_server"];
41 var uri = utils.confdPort(api_server);
42 var url = req.path;
43 return new Promise(function(resolve, reject) {
44 request({
45 url: uri + url + '?deep',
46 method: 'GET',
47 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
48 'Authorization': req.session && req.session.authorization
49 }),
50 forever: constants.FOREVER_ON,
51 rejectUnauthorized: false,
52 }, function(error, response, body) {
53 if (utils.validateResponse('restconfAPI.streams', error, response, body, resolve, reject)) {
54 // resolve(JSON.parse(response.body))
55 resolve({
56 statusCode: response.statusCode,
57 data: JSON.parse(response.body)
58 })
59 };
60 })
61 })
62 };
63
64
65 module.exports = restconfAPI;