'ENV',
'ParsingJsonService',
'RequestsService',
-
+ 'PathUtilsService',
+ 'constants',
];
function YangmanService(
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 {
}
}
+ /**
+ * 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
*/
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(){
+ default: function (){
var methodType = {
GET: function () {
+ if ( node ){
+ node.clear();
+ }
return rData;
},
DELETE: function () {
- node.clear();
-
+ if ( node ) {
+ node.clear();
+ }
return {};
},
DEFAULT: function () {
- return outputType === 'form' ? sData : rData;
- }
+ return rData;
+ },
};
return (methodType[method] || methodType.DEFAULT)();
- }
+ },
};
return (prepareType[node ? node.type : 'default'] || prepareType.default)();
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
function prepareAllRequestData(selectedApi, selectedSubApi, operation, node, dataType, requestUrl, requestData,
params) {
var allPreparedData = {
- customRestangular: null,
- headers: {},
- operation: '',
- reqString: selectedSubApi ? selectedSubApi.buildApiRequestString() : '',
- reqHeaders: {},
- reqData: '',
- srcData: '',
- };
+ customRestangular: null,
+ headers: {},
+ operation: '',
+ reqString: '',
+ reqHeaders: {},
+ reqData: '',
+ srcData: '',
+ reqFullUrl: '',
+ },
+ selSubApiCopy = angular.copy(selectedSubApi);
- if ( dataType === 'form' && node){
- node.buildRequest(RequestBuilderService, requestData, node.module);
- allPreparedData.srcData = angular.copy(requestData);
- }
- else {
- allPreparedData.srcData = requestData;
- }
- 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 || operation === constants.OPERATION_DELETE){
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);
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
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: (time.finished - time.started),
+ time: spentRequestTime < 0 ? -(spentRequestTime) : spentRequestTime,
requestData: allPreparedData.reqData,
requestSrcData: allPreparedData.srcData,
};