1 define([], function () {
4 angular.module('app.yangman').service('YangmanService', YangmanService);
6 YangmanService.$inject = [
7 'RequestBuilderService',
9 'YangUtilsRestangularService',
17 function YangmanService(
18 RequestBuilderService,
20 YangUtilsRestangularService,
29 checkRpcReceivedData: checkRpcReceivedData,
30 executeRequestOperation: executeRequestOperation,
31 fillNodeFromResponse: fillNodeFromResponse,
32 getDataStoreIndex: getDataStoreIndex,
33 handleNodeIdentifier: handleNodeIdentifier,
34 prepareAllRequestData: prepareAllRequestData,
35 prepareReceivedData: prepareReceivedData,
36 putIntoObj: putIntoObj,
37 setSrcDataByDataType: setSrcDataByDataType,
38 validateFile: validateFile,
44 * Handle param continuum between header path array and node data
45 * @param parametersList
46 * @param selectedSubApi
49 function handleNodeIdentifier(parametersList, selectedSubApi, node){
50 var identifier = RequestsService.findIdentifierByParam(
51 parametersList, selectedSubApi.pathArray[selectedSubApi.pathArray.length - 1]
55 PathUtilsService.fillListNode(node, identifier.label, identifier.value);
60 * Put data to output container if root node is rpc
65 function checkRpcReceivedData(data, node){
66 return node.type === constants.NODE_RPC ? cutData(data) : data;
68 function cutData(data){
70 output: data[node.label].output,
76 * Put source object into destination object by source properties
78 * @param destinationObj
80 function putIntoObj(sourceObj, destinationObj, containter){
82 Object.keys(sourceObj).forEach(function(prop){
83 destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {};
84 destinationObj[containter][prop] = sourceObj[prop];
90 * Prepare request date before filling into node depends on method and node type
98 function prepareReceivedData(node, method, rData, sData, outputType){
102 if ( outputType === constants.DISPLAY_TYPE_FORM ){
107 sData[node.label] = {};
110 putIntoObj(rData, dObj, node.label);
111 putIntoObj(sData[node.label] ? sData[node.label] : sData, dObj, node.label);
117 default: function (){
125 DELETE: function () {
131 DEFAULT: function () {
136 return (methodType[method] || methodType.DEFAULT)();
140 return (prepareType[node ? node.type : 'default'] || prepareType.default)();
144 * Validating collection import file
149 function validateFile(data, checkArray){
151 var obj = ParsingJsonService.parseJson(data);
153 return obj && obj.every(function (el){
154 return checkArray.every(function (arr){
155 return el.hasOwnProperty(arr);
164 * Return index of selected datastore in list
169 function getDataStoreIndex(list, dataStore){
171 result = list.some(function (item, index) {
173 return item.label === dataStore;
176 return result ? rIndex : null;
180 * Apply all parametrized values into request (data, url, pathArray)
181 * @param allPreparedData
183 * @param selSubApiCopy
186 function setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl){
187 allPreparedData.reqFullUrl = RequestsService.applyParamsToStr(params, requestUrl);
189 // apply parametrized value into request data in string form
190 allPreparedData.reqString =
191 selSubApiCopy ? RequestsService.applyParamsToStr(params, selSubApiCopy.buildApiRequestString()) : '';
193 if ( !angular.equals(allPreparedData.reqFullUrl, requestUrl) && selSubApiCopy ){
194 // fill parametrized data into path array
195 PathUtilsService.fillPath(selSubApiCopy.pathArray, allPreparedData.reqFullUrl);
198 allPreparedData.reqData = RequestsService.applyParamsToObj(params, allPreparedData.srcData);
202 * Set source data into request object based on shown data type
203 * @param allPreparedData
208 function setSrcDataByDataType(allPreparedData, node, requestData, dataType){
209 if ( dataType === constants.DISPLAY_TYPE_FORM && node){
210 node.buildRequest(RequestBuilderService, requestData, node.module);
211 allPreparedData.srcData = angular.copy(requestData);
214 allPreparedData.srcData = requestData;
219 * Prepare all necessary data for executing or saving request
221 * @param selectedSubApi
230 * customRestangular: null,
242 function prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData,
244 var allPreparedData = {
245 customRestangular: null,
254 selSubApiCopy = angular.copy(selectedSubApi);
256 setSrcDataByDataType(allPreparedData, node, requestData, dataType);
257 setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl);
260 if (operation === constants.OPERATION_GET){
261 allPreparedData.srcData = null;
262 allPreparedData.reqData = null;
264 else if ( operation === constants.OPERATION_POST ){
266 if ( selSubApiCopy ) {
267 allPreparedData.reqData = YangUtilsService.postRequestData(
268 allPreparedData.reqData,
269 allPreparedData.reqString,
275 // set correct host into restangular based on shown data type and prepare data
276 if ( dataType === constants.DISPLAY_TYPE_REQ_DATA ){
277 var parser = locationHelper(allPreparedData.reqFullUrl, ['pathname', 'origin']),
280 YangUtilsRestangularService.setBaseUrl(parser.origin);
281 allPreparedData.reqString = parser.pathname.slice(1).split('/');
282 raParam = allPreparedData.reqString.shift();
283 allPreparedData.reqString = allPreparedData.reqString.join('/');
285 allPreparedData.customRestangular = YangUtilsRestangularService.one(raParam);
289 YangUtilsRestangularService.setBaseUrl(ENV.getBaseURL('MD_SAL'));
290 allPreparedData.customRestangular = YangUtilsRestangularService.one('restconf');
293 allPreparedData.headers = YangUtilsService.prepareHeaders(allPreparedData.reqData);
297 allPreparedData.operation = YangUtilsService.prepareOperation(operation);
298 return allPreparedData;
301 function cutUrl(url){
302 return url.indexOf('restconf') > -1 ? url.split('restconf')[1].substring(1) : url;
306 * Execute request built from this data
308 * @param selectedSubApi
318 function executeRequestOperation(selectedApi, selectedSubApi, operation, node, dataType, requestUrl,
319 requestData, params, successCbk, errorCbk) {
321 YangUtilsRestangularService.setFullResponse(true);
323 var allPreparedData = prepareAllRequestData(
324 selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData, params
327 // executing operation
328 allPreparedData.customRestangular.customOperation(
329 allPreparedData.operation.toLowerCase(),
330 allPreparedData.reqString,
332 allPreparedData.headers,
333 allPreparedData.reqData
336 function (response) {
337 (successCbk || angular.noop)(finishExecuting(response), response);
339 function (response) {
340 (errorCbk || angular.noop)(finishExecuting(response), response);
344 function finishExecuting(response){
347 status: response.status,
348 statusText: response.statusText,
350 requestData: allPreparedData.reqData,
351 requestSrcData: allPreparedData.srcData,
357 * Method for parsing path to additional properties based on JS LOCATION
362 function locationHelper(path, properties){
363 var parser = document.createElement('a'),
368 properties.forEach(function (prop) {
369 obj[prop] = parser[prop];
376 * Fill node values from response
380 function fillNodeFromResponse(node, data){
381 var props = data ? Object.getOwnPropertyNames(data) : [];
383 // fill each property - needed for root mountpoint node,
384 // in other cases there should be only one property anyway
385 props.forEach(function (p) {
386 node.fill(p, data[p]);