X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=modules%2Fyangman-resources%2Fsrc%2Fmain%2Fresources%2Fyangman%2Fservices%2Fyangman.services.js;h=5be603d013403015c53251342deaf7b8181ef6b4;hb=c0a35610ccb95a45a5074a325e1307ff71d933d7;hp=a13ca474f1e8b0a6dca660d1f06958027813db7c;hpb=24745532bf03bbc42bf3e16d876c562000e33c84;p=dlux.git diff --git a/modules/yangman-resources/src/main/resources/yangman/services/yangman.services.js b/modules/yangman-resources/src/main/resources/yangman/services/yangman.services.js index a13ca474..5be603d0 100644 --- a/modules/yangman-resources/src/main/resources/yangman/services/yangman.services.js +++ b/modules/yangman-resources/src/main/resources/yangman/services/yangman.services.js @@ -10,7 +10,8 @@ define([], function () { 'ENV', 'ParsingJsonService', 'RequestsService', - + 'PathUtilsService', + 'constants', ]; function YangmanService( @@ -19,22 +20,50 @@ define([], function () { YangUtilsRestangularService, ENV, ParsingJsonService, - RequestsService + RequestsService, + PathUtilsService, + constants ){ var service = { + cutUrl: cutUrl, checkRpcReceivedData: checkRpcReceivedData, executeRequestOperation: executeRequestOperation, fillNodeFromResponse: fillNodeFromResponse, getDataStoreIndex: getDataStoreIndex, + handleNodeIdentifier: handleNodeIdentifier, prepareAllRequestData: prepareAllRequestData, prepareReceivedData: prepareReceivedData, + putIntoObj: putIntoObj, + setSrcDataByDataType: setSrcDataByDataType, validateFile: validateFile, }; return service; + /** + * Handle param continuum between header path array and node data + * @param parametersList + * @param selectedSubApi + * @param node + */ + function handleNodeIdentifier(parametersList, selectedSubApi, node){ + var identifier = RequestsService.findIdentifierByParam( + parametersList, selectedSubApi.pathArray[selectedSubApi.pathArray.length - 1] + ); + + if ( identifier ){ + PathUtilsService.fillListNode(node, identifier.label, identifier.value); + } + } + + /** + * Put data to output container if root node is rpc + * @param data + * @param node + * @returns {*} + */ function checkRpcReceivedData(data, node){ - return node.type === 'rpc' ? cutData(data) : data; + return node.type === constants.NODE_RPC ? cutData(data) : data; function cutData(data){ return { @@ -43,6 +72,20 @@ define([], function () { } } + /** + * Put source object into destination object by source properties + * @param sourceObj + * @param destinationObj + */ + function putIntoObj(sourceObj, destinationObj, containter){ + if ( sourceObj ) { + Object.keys(sourceObj).forEach(function(prop){ + destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {}; + destinationObj[containter][prop] = sourceObj[prop]; + }); + } + } + /** * Prepare request date before filling into node depends on method and node type * @param node @@ -54,49 +97,44 @@ define([], function () { */ function prepareReceivedData(node, method, rData, sData, outputType){ var prepareType = { - rpc: function(){ + rpc: function (){ - if ( outputType === 'form' ){ + if ( outputType === constants.DISPLAY_TYPE_FORM ){ var dObj = {}; + + if ( !sData ) { + sData = {}; + sData[node.label] = {}; + } + putIntoObj(rData, dObj, node.label); putIntoObj(sData[node.label] ? sData[node.label] : sData, dObj, node.label); return dObj; } else { return rData; } - - /** - * Put source object into destination object by source properties - * @param sourceObj - * @param destinationObj - */ - function putIntoObj(sourceObj, destinationObj, containter){ - Object.keys(sourceObj).forEach(function(prop){ - destinationObj[containter] = destinationObj[containter] ? destinationObj[containter] : {}; - destinationObj[containter][prop] = sourceObj[prop]; - }); - } }, default: function (){ var methodType = { GET: function () { - node.clear(); + if ( node ){ + node.clear(); + } return rData; }, DELETE: function () { - node.clear(); + if ( node ) { + node.clear(); + } return {}; }, - PUT: function () { + DEFAULT: function () { return rData; }, - DEFAULT: function () { - return outputType === 'form' ? sData : rData; - } }; return (methodType[method] || methodType.DEFAULT)(); - } + }, }; return (prepareType[node ? node.type : 'default'] || prepareType.default)(); @@ -138,6 +176,45 @@ define([], function () { return result ? rIndex : null; } + /** + * Apply all parametrized values into request (data, url, pathArray) + * @param allPreparedData + * @param params + * @param selSubApiCopy + * @param requestUrl + */ + function setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl){ + allPreparedData.reqFullUrl = RequestsService.applyParamsToStr(params, requestUrl); + + // apply parametrized value into request data in string form + allPreparedData.reqString = + selSubApiCopy ? RequestsService.applyParamsToStr(params, selSubApiCopy.buildApiRequestString()) : ''; + + if ( !angular.equals(allPreparedData.reqFullUrl, requestUrl) && selSubApiCopy ){ + // fill parametrized data into path array + PathUtilsService.fillPath(selSubApiCopy.pathArray, allPreparedData.reqFullUrl); + } + + allPreparedData.reqData = RequestsService.applyParamsToObj(params, allPreparedData.srcData); + } + + /** + * Set source data into request object based on shown data type + * @param allPreparedData + * @param node + * @param requestData + * @param dataType + */ + function setSrcDataByDataType(allPreparedData, node, requestData, dataType){ + if ( dataType === constants.DISPLAY_TYPE_FORM && node){ + node.buildRequest(RequestBuilderService, requestData, node.module); + allPreparedData.srcData = angular.copy(requestData); + } + else { + allPreparedData.srcData = requestData; + } + } + /** * Prepare all necessary data for executing or saving request * @param selectedApi @@ -148,47 +225,56 @@ define([], function () { * @param requestUrl * @param requestData * @param params - * @returns {{customRestangular: null, headers: {}, operation: string, reqString: string, reqHeaders: {}, - * reqData: {}}} + * @returns { + * { + * customRestangular: null, + * headers: {}, + * operation: string, + * reqString: string, + * reqHeaders: {}, + * reqData: string, + * srcData: string, + * reqFullUrl: + * string + * } + * } */ function prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData, params) { var allPreparedData = { - customRestangular: null, - headers: {}, - operation: '', - reqString: selectedSubApi ? selectedSubApi.buildApiRequestString() : '', - reqHeaders: {}, - reqData: '', - srcData: '', - }; - - if ( dataType === 'form' && node){ - node.buildRequest(RequestBuilderService, requestData, node.module); - allPreparedData.srcData = angular.copy(requestData); - } - else { - allPreparedData.srcData = requestData; - } + customRestangular: null, + headers: {}, + operation: '', + reqString: '', + reqHeaders: {}, + reqData: '', + srcData: '', + reqFullUrl: '', + }, + selSubApiCopy = angular.copy(selectedSubApi); - allPreparedData.reqData = RequestsService.applyParams(params, allPreparedData.srcData); + setSrcDataByDataType(allPreparedData, node, requestData, dataType); + setParametrizedData(allPreparedData, params, selSubApiCopy, requestUrl); // prepare req data - if (operation === 'GET' || operation === 'DELETE'){ + if (operation === constants.OPERATION_GET){ allPreparedData.srcData = null; allPreparedData.reqData = null; } - else if (operation === 'POST'){ - allPreparedData.reqData = YangUtilsService.postRequestData( - allPreparedData.reqData, - allPreparedData.reqString, - selectedSubApi - ); + else if ( operation === constants.OPERATION_POST ){ + + if ( selSubApiCopy ) { + allPreparedData.reqData = YangUtilsService.postRequestData( + allPreparedData.reqData, + allPreparedData.reqString, + selSubApiCopy + ); + } } // set correct host into restangular based on shown data type and prepare data - if ( dataType === 'req-data' ){ - var parser = locationHelper(requestUrl, ['pathname', 'origin']), + if ( dataType === constants.DISPLAY_TYPE_REQ_DATA ){ + var parser = locationHelper(allPreparedData.reqFullUrl, ['pathname', 'origin']), raParam = ''; YangUtilsRestangularService.setBaseUrl(parser.origin); @@ -212,6 +298,10 @@ define([], function () { return allPreparedData; } + function cutUrl(url){ + return url.indexOf('restconf') > -1 ? url.split('restconf')[1].substring(1) : url; + } + /** * Execute request built from this data * @param selectedApi @@ -227,19 +317,12 @@ define([], function () { */ function executeRequestOperation(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData, params, successCbk, errorCbk) { - var time = { - started: 0, - finished: 0, - }; YangUtilsRestangularService.setFullResponse(true); - // prepare all necessary data - var allPreparedData = prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, - requestUrl, requestData, params); - - // start track time response - time.started = new Date().getMilliseconds(); + var allPreparedData = prepareAllRequestData( + selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData, params + ); // executing operation allPreparedData.customRestangular.customOperation( @@ -259,14 +342,11 @@ define([], function () { ); function finishExecuting(response){ - // finish track time response - time.finished = new Date().getMilliseconds(); - var spentRequestTime = time.finished - time.started; return { status: response.status, statusText: response.statusText, - time: spentRequestTime < 0 ? -(spentRequestTime) : spentRequestTime, + time: null, requestData: allPreparedData.reqData, requestSrcData: allPreparedData.srcData, };