1 define([], function () {
4 function ApiBuilderService(ArrayUtilsService, PathUtilsService, NodeUtilsService, YangUtilsRestangularService,
10 processAllRootNodes: processAllRootNodes,
11 processSingleRootNode: processSingleRootNode,
14 config: ['GET', 'PUT', 'DELETE'],
18 nodePathStringCreator = {
19 list: function (node, pathstr) {
20 return pathstr + addNodePathStr(node) + '/' + (node.refKey.length ?
21 (getKeyIndentifiers(node.refKey).join('/') + '/') : '');
23 container: function (node, pathstr) {
24 return pathstr + addNodePathStr(node) + '/';
26 rpc: function (node, pathstr) {
27 return pathstr + addNodePathStr(node) + '/';
41 function Api(basePath, module, revision, subApis) {
42 this.basePath = basePath;
44 this.revision = revision;
45 this.subApis = subApis || [];
47 this.addSubApis = function (subApis) {
49 subApis.forEach(function (sa) {
51 self.subApis.push(sa);
58 * @param pathTemplateString
65 function SubApi(pathTemplateString, operations, node, storage, parent) {
67 this.pathTemplateString = pathTemplateString;
68 this.operations = operations;
69 this.storage = storage;
71 this.parent = parent ? parent : null;
73 this.pathArray = (function (st, path) {
74 var pathString = (st ? st + '/' : '') + path;
75 return PathUtilsService.translate(pathString);
76 })(this.storage, this.pathTemplateString);
78 this.equals = function (pathArray, compareIdentifierValues) {
79 return this.pathArray.every(function (pa, i) {
80 pa.equals(pathArray[i], compareIdentifierValues);
84 this.buildApiRequestString = function () {
85 return PathUtilsService.translatePathArray(this.pathArray).join('/');
88 this.addCustomFunctionality = function (label, callback, viewStr, hideButtonOnSelect) {
89 var funct = CustomFuncService.createNewFunctionality(label, this.node, callback, viewStr, hideButtonOnSelect);
92 this.custFunct.push(funct);
96 this.clone = function (options) {
97 var getOption = function (optName) {
100 res = options[optName] || null;
104 clone = new SubApi(getOption('pathTemplateString') || this.pathTemplateString,
105 getOption('operations') || this.operations,
106 getOption('withoutNode') ? null : this.node,
107 getOption('storage') || this.storage,
108 getOption('parent') || this.parent);
110 if (getOption('clonePathArray')) {
111 clone.pathArray = this.pathArray.map(function (pe) {
120 // TODO: add function's description
121 function removeDuplicatedApis(apis) {
123 sortApisByRevision = function (a, b) {
124 var dateA = new Date(a.revision + 'Z'),
125 dateB = new Date(b.revision + 'Z');
127 return dateB - dateA;
130 apis.forEach(function (a) {
131 if (toRemove.indexOf(a) === -1) {
132 var sortedApis = apis.filter(function (af) {
133 return a.module === af.module;
134 }).sort(sortApisByRevision);
136 toRemove = toRemove.concat(sortedApis.slice(1));
140 toRemove.forEach(function (a) {
141 apis.splice(apis.indexOf(a), 1);
147 // TODO: add function's description
148 function isConfigNode(node) {
151 if (node.hasOwnProperty('isConfigStm')) {
152 result = node.isConfigStm;
153 } else if (node.parent) {
154 result = isConfigNode(node.parent);
160 // TODO: add function's description
161 function addNodePathStr(node) {
162 return (!node.parent || (node.parent.module !== node.module) ? node.module + ':' : '') + node.label;
165 // TODO: add function's description
166 function getBasePath() {
167 return YangUtilsRestangularService.configuration.baseUrl + '/restconf/';
170 // TODO: add function's description
171 function getApiByModuleRevision(apis, module, revision) {
172 return apis.filter(function (a) {
173 return a.module === module && a.revision === revision;
177 // TODO: add function's description
178 function getKeyIndentifiers(keys) {
179 return keys.map(function (k) {
180 return '{' + k.label + '}';
184 // TODO: add function's description
185 function getStoragesByNodeType(node) {
187 if (NodeUtilsService.isRootNode(node.type)) {
188 if (node.type === 'rpc') {
189 storages.push('operations');
191 storages.push('operational');
192 if (isConfigNode(node)) {
193 storages.push('config');
201 // TODO: add function's description
202 function getOperationsByStorage(storage) {
204 if (storageOperations.hasOwnProperty(storage)) {
205 operations = storageOperations[storage];
211 // TODO: add function's description
212 function createSubApis(node, pathstr) {
213 var storages = getStoragesByNodeType(node);
215 return storages.map(function (storage) {
216 var subApi = new SubApi(pathstr, getOperationsByStorage(storage), node, storage);
221 // TODO: add function's description
222 function nodeChildrenProcessor(node, pathstr, subApis) {
223 if (NodeUtilsService.isRootNode(node.type) && nodePathStringCreator.hasOwnProperty(node.type)) {
224 var templateStr = nodePathStringCreator[node.type](node, pathstr),
225 newSubApis = createSubApis(node, templateStr);
227 ArrayUtilsService.pushElementsToList(subApis, newSubApis);
229 node.children.forEach(function (ch) {
230 nodeChildrenProcessor(ch, templateStr, subApis);
236 * Function for showing available apis in web browser's console
239 function printApis(apis) {
241 apis.forEach(function (a) {
242 a.subApis.forEach(function (sa) {
243 co += (sa.storage + '/' + sa.pathTemplateString + '\n');
250 // TODO: add service's description
251 function processAllRootNodes(nodes) {
254 nodes.forEach(function (node) {
255 var api = getApiByModuleRevision(apis, node.module, node.moduleRevision),
259 api = new Api(getBasePath(), node.module, node.moduleRevision);
263 api.addSubApis(processSingleRootNode(node));
270 apis = removeDuplicatedApis(apis);
277 // TODO: add service's description
278 function processSingleRootNode(node) {
279 var templateStr = nodePathStringCreator[node.type](node, ''),
280 subApis = createSubApis(node, templateStr);
282 node.children.forEach(function (ch) {
283 nodeChildrenProcessor(ch, templateStr, subApis);
291 ApiBuilderService.$inject = ['ArrayUtilsService', 'PathUtilsService', 'NodeUtilsService',
292 'YangUtilsRestangularService', 'CustomFuncService'];
294 return ApiBuilderService;