YangUI - removed POST method for config datastore
[dlux.git] / modules / common-yangutils-resources / src / main / resources / yangutils / services / api-builder.services.js
1 define([], function () {
2     'use strict';
3
4     function ApiBuilderService(ArrayUtilsService, PathUtilsService, NodeUtilsService, YangUtilsRestangularService,
5                                CustomFuncService){
6
7         var service = {
8                 Api: Api,
9                 SubApi: SubApi,
10                 processAllRootNodes: processAllRootNodes,
11                 processSingleRootNode: processSingleRootNode,
12             },
13             storageOperations = {
14                 config: ['GET', 'PUT', 'DELETE'],
15                 operational: ['GET'],
16                 operations: ['POST'],
17             },
18             nodePathStringCreator = {
19                 list: function (node, pathstr) {
20                     return pathstr + addNodePathStr(node) + '/' + (node.refKey.length ?
21                             (getKeyIndentifiers(node.refKey).join('/') + '/') : '');
22                 },
23                 container: function (node, pathstr) {
24                     return pathstr + addNodePathStr(node) + '/';
25                 },
26                 rpc: function (node, pathstr) {
27                     return pathstr + addNodePathStr(node) + '/';
28                 },
29             };
30
31         return service;
32
33         /**
34          * Base Api object
35          * @param basePath
36          * @param module
37          * @param revision
38          * @param subApis
39          * @constructor
40          */
41         function Api(basePath, module, revision, subApis) {
42             this.basePath = basePath;
43             this.module = module;
44             this.revision = revision;
45             this.subApis = subApis || [];
46
47             this.addSubApis = function (subApis) {
48                 var self = this;
49                 subApis.forEach(function (sa) {
50                     sa.parent = self;
51                     self.subApis.push(sa);
52                 });
53             };
54         }
55
56         /**
57          * Base SubApi object
58          * @param pathTemplateString
59          * @param operations
60          * @param node
61          * @param storage
62          * @param parent
63          * @constructor
64          */
65         function SubApi(pathTemplateString, operations, node, storage, parent) {
66             this.node = node;
67             this.pathTemplateString = pathTemplateString;
68             this.operations = operations;
69             this.storage = storage;
70             this.custFunct = [];
71             this.parent = parent ? parent : null;
72
73             this.pathArray = (function (st, path) {
74                 var pathString = (st ? st + '/' : '') + path;
75                 return PathUtilsService.translate(pathString);
76             })(this.storage, this.pathTemplateString);
77
78             this.equals = function (pathArray, compareIdentifierValues) {
79                 return this.pathArray.every(function (pa, i) {
80                     pa.equals(pathArray[i], compareIdentifierValues);
81                 });
82             };
83
84             this.buildApiRequestString = function () {
85                 return PathUtilsService.translatePathArray(this.pathArray).join('/');
86             };
87
88             this.addCustomFunctionality = function (label, callback, viewStr, hideButtonOnSelect) {
89                 var funct = CustomFuncService.createNewFunctionality(label, this.node, callback, viewStr, hideButtonOnSelect);
90
91                 if (funct) {
92                     this.custFunct.push(funct);
93                 }
94             };
95
96             this.clone = function (options) {
97                 var getOption = function (optName) {
98                         var res = null;
99                         if (options) {
100                             res = options[optName] || null;
101                         }
102                         return  res;
103                     },
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);
109
110                 if (getOption('clonePathArray')) {
111                     clone.pathArray = this.pathArray.map(function (pe) {
112                         return pe.clone();
113                     });
114                 }
115
116                 return clone;
117             };
118         }
119
120         // TODO: add function's description
121         function removeDuplicatedApis(apis) {
122             var toRemove = [],
123                 sortApisByRevision = function (a, b) {
124                     var dateA = new Date(a.revision + 'Z'),
125                         dateB = new Date(b.revision + 'Z');
126
127                     return dateB - dateA;
128                 };
129
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);
135
136                     toRemove = toRemove.concat(sortedApis.slice(1));
137                 }
138             });
139
140             toRemove.forEach(function (a) {
141                 apis.splice(apis.indexOf(a), 1);
142             });
143
144             return apis;
145         }
146
147         // TODO: add function's description
148         function isConfigNode(node) {
149             var result = false;
150
151             if (node.hasOwnProperty('isConfigStm')) {
152                 result = node.isConfigStm;
153             } else if (node.parent) {
154                 result = isConfigNode(node.parent);
155             }
156
157             return result;
158         }
159
160         // TODO: add function's description
161         function addNodePathStr(node) {
162             return (!node.parent || (node.parent.module !== node.module) ? node.module + ':' : '') + node.label;
163         }
164
165         // TODO: add function's description
166         function getBasePath() {
167             return YangUtilsRestangularService.configuration.baseUrl + '/restconf/';
168         }
169
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;
174             })[0];
175         }
176
177         // TODO: add function's description
178         function getKeyIndentifiers(keys) {
179             return keys.map(function (k) {
180                 return '{' + k.label + '}';
181             });
182         }
183
184         // TODO: add function's description
185         function getStoragesByNodeType(node) {
186             var storages = [];
187             if (NodeUtilsService.isRootNode(node.type)) {
188                 if (node.type === 'rpc') {
189                     storages.push('operations');
190                 } else {
191                     storages.push('operational');
192                     if (isConfigNode(node)) {
193                         storages.push('config');
194                     }
195                 }
196             }
197
198             return storages;
199         }
200
201         // TODO: add function's description
202         function getOperationsByStorage(storage) {
203             var operations =  [];
204             if (storageOperations.hasOwnProperty(storage)) {
205                 operations = storageOperations[storage];
206             }
207
208             return operations;
209         }
210
211         // TODO: add function's description
212         function createSubApis(node, pathstr) {
213             var storages = getStoragesByNodeType(node);
214
215             return storages.map(function (storage) {
216                 var subApi = new SubApi(pathstr, getOperationsByStorage(storage), node, storage);
217                 return subApi;
218             });
219         }
220
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);
226
227                 ArrayUtilsService.pushElementsToList(subApis, newSubApis);
228
229                 node.children.forEach(function (ch) {
230                     nodeChildrenProcessor(ch, templateStr, subApis);
231                 });
232             }
233         }
234
235         /**
236          * Function for showing available apis in web browser's console
237          * @param apis
238          */
239         function printApis(apis) {
240             var co = '';
241             apis.forEach(function (a) {
242                 a.subApis.forEach(function (sa) {
243                     co += (sa.storage + '/' + sa.pathTemplateString + '\n');
244                 });
245             });
246
247             // console.info(co);
248         }
249
250         // TODO: add service's description
251         function processAllRootNodes(nodes) {
252             var apis = [];
253
254             nodes.forEach(function (node) {
255                 var api = getApiByModuleRevision(apis, node.module, node.moduleRevision),
256                     newApi = false;
257
258                 if (!api) {
259                     api = new Api(getBasePath(), node.module, node.moduleRevision);
260                     newApi = true;
261                 }
262
263                 api.addSubApis(processSingleRootNode(node));
264
265                 if (newApi) {
266                     apis.push(api);
267                 }
268             });
269
270             apis = removeDuplicatedApis(apis);
271
272             printApis(apis);
273
274             return apis;
275         }
276
277         // TODO: add service's description
278         function processSingleRootNode(node) {
279             var templateStr = nodePathStringCreator[node.type](node, ''),
280                 subApis = createSubApis(node, templateStr);
281
282             node.children.forEach(function (ch) {
283                 nodeChildrenProcessor(ch, templateStr, subApis);
284             });
285
286             return subApis;
287         }
288
289     }
290
291     ApiBuilderService.$inject = ['ArrayUtilsService', 'PathUtilsService', 'NodeUtilsService',
292                                 'YangUtilsRestangularService', 'CustomFuncService'];
293
294     return ApiBuilderService;
295
296 });