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
228 * @returns {{customRestangular: null, headers: {}, operation: string, reqString: string, reqHeaders: {},
231 function prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData,
233 var allPreparedData = {
234 customRestangular: null,
243 selSubApiCopy = angular.copy(selectedSubApi);
245 setSrcDataByDataType(allPreparedData, node, requestData, dataType);
246 setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl);
249 if (operation === constants.OPERATION_GET || operation === constants.OPERATION_DELETE){
250 allPreparedData.srcData = null;
251 allPreparedData.reqData = null;
253 else if ( operation === constants.OPERATION_POST ){
255 if ( selSubApiCopy ) {
256 allPreparedData.reqData = YangUtilsService.postRequestData(
257 allPreparedData.reqData,
258 allPreparedData.reqString,
264 // set correct host into restangular based on shown data type and prepare data
265 if ( dataType === constants.DISPLAY_TYPE_REQ_DATA ){
266 var parser = locationHelper(allPreparedData.reqFullUrl, ['pathname', 'origin']),
269 YangUtilsRestangularService.setBaseUrl(parser.origin);
270 allPreparedData.reqString = parser.pathname.slice(1).split('/');
271 raParam = allPreparedData.reqString.shift();
272 allPreparedData.reqString = allPreparedData.reqString.join('/');
274 allPreparedData.customRestangular = YangUtilsRestangularService.one(raParam);
278 YangUtilsRestangularService.setBaseUrl(ENV.getBaseURL('MD_SAL'));
279 allPreparedData.customRestangular = YangUtilsRestangularService.one('restconf');
282 allPreparedData.headers = YangUtilsService.prepareHeaders(allPreparedData.reqData);
286 allPreparedData.operation = YangUtilsService.prepareOperation(operation);
287 return allPreparedData;
290 function cutUrl(url){
291 return url.indexOf('restconf') > -1 ? url.split('restconf')[1].substring(1) : url;
295 * Execute request built from this data
297 * @param selectedSubApi
307 function executeRequestOperation(selectedApi, selectedSubApi, operation, node, dataType, requestUrl,
308 requestData, params, successCbk, errorCbk) {
310 YangUtilsRestangularService.setFullResponse(true);
312 // prepare all necessary data
313 var allPreparedData = prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType,
314 requestUrl, requestData, params);
316 // executing operation
317 allPreparedData.customRestangular.customOperation(
318 allPreparedData.operation.toLowerCase(),
319 allPreparedData.reqString,
321 allPreparedData.headers,
322 allPreparedData.reqData
325 function (response) {
326 (successCbk || angular.noop)(finishExecuting(response), response);
328 function (response) {
329 (errorCbk || angular.noop)(finishExecuting(response), response);
333 function finishExecuting(response){
336 status: response.status,
337 statusText: response.statusText,
339 requestData: allPreparedData.reqData,
340 requestSrcData: allPreparedData.srcData,
346 * Method for parsing path to additional properties based on JS LOCATION
351 function locationHelper(path, properties){
352 var parser = document.createElement('a'),
357 properties.forEach(function (prop) {
358 obj[prop] = parser[prop];
365 * Fill node values from response
369 function fillNodeFromResponse(node, data){
370 var props = data ? Object.getOwnPropertyNames(data) : [];
372 // fill each property - needed for root mountpoint node,
373 // in other cases there should be only one property anyway
374 props.forEach(function (p) {
375 node.fill(p, data[p]);