Yangman - fix delete operation
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / mount-points-connector.services.js
1 define([
2     'common/yangutils/constants',
3 ], function (constants) {
4     'use strict';
5
6     angular.module('app.yangman').service('MountPointsConnectorService', MountPointsConnectorService);
7
8     MountPointsConnectorService.$inject = [
9         'YangUiApisService', 'NodeWrapperService',
10         'YangUtilsService', 'EventDispatcherService',
11         'YinParserService', 'PathUtilsService', 'YangUtilsRestangularService',
12     ];
13
14     function MountPointsConnectorService(
15         YangUiApisService, NodeWrapperService,
16         YangUtilsService, EventDispatcherService,
17         YinParserService, PathUtilsService, YangUtilsRestangularService){
18
19         var mountPrefix = constants.MPPREFIX,
20             service = {
21                 addPathElemsToPathArray: addPathElemsToPathArray,
22                 alterMpPath: alterMpPath,
23                 createCustomButton: createCustomButton,
24                 createMPRootNode: createMPRootNode,
25                 discoverMountPoints: discoverMountPoints,
26                 getMPModulesAPI: getMPModulesAPI,
27                 updateMountPointApis: updateMountPointApis,
28             };
29
30         return service;
31
32         // TODO: add service's description
33         function createMPRootNode(mpNodes) {
34             var node = null,
35                 yangParser = YinParserService.yangParser;
36
37             yangParser.setCurrentModuleObj(new YinParserService.Module('yang-ext', null, null));
38             node = yangParser.createNewNode('mount', 'container', null, constants.NODE_UI_DISPLAY);
39             NodeWrapperService.wrapAll(node);
40
41             node.buildRequest = function (builder, req, module) {
42                 var added = false,
43                     builderNodes = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
44
45                 if (builderNodes.length) {
46                     builderNodes.forEach(function (child) {
47                         var childAdded = child.buildRequest(builder, req, module);
48                     });
49                 }
50
51                 return added;
52             };
53
54             node.fill = function (name, data) {
55                 var nodesToFill = node.getChildren(null, null, constants.NODE_UI_DISPLAY);
56
57                 nodesToFill.forEach(function (child) {
58                     var childFilled = child.fill(name, data);
59                 });
60             };
61
62
63             mpNodes.forEach(function (mp){
64                 node.addChild(mp);
65             });
66
67             return node;
68         }
69
70         // TODO: add service's description
71         function addPathElemsToPathArray(pathElems, pathArray, index) {
72             var updatedPath = pathArray.slice();
73
74             pathElems.forEach(function (pe, offset) {
75                 // pe.disabled = true; //add disabled flag so user won't be able to change it in the UI
76                 updatedPath.splice(index + offset, 0, pe);
77             });
78
79             return updatedPath;
80         }
81
82         // TODO: add service's description
83         function alterMpPath(path) {
84             var pathParts = path.split('/'),
85                 restconfIndex = PathUtilsService.findIndexOfStrInPathStr(pathParts, 'restconf'),
86                 mpIndex = PathUtilsService.findIndexOfStrInPathStr(pathParts, mountPrefix),
87                 mpPath = path.slice(),
88                 mpPathParts = '';
89
90             if (mpIndex !== -1){
91                 mpPathParts = pathParts.slice(mpIndex);
92
93                 var unshiftIndex = restconfIndex !== -1 ? restconfIndex + 1 : 0;
94
95                 mpPathParts.unshift(pathParts[unshiftIndex]);
96                 mpPath = mpPathParts.join('/');
97             }
98
99             return mpPath;
100         }
101
102         /**
103          * function for adding path to mountpoint + yang:ext-mount to mount point patharray so the request string
104          * will be built correctly
105          * @param basePathArray
106          * @param mpApis
107          */
108         function updateMountPointApis(basePathArray, mpApis) {
109             var actualPath = basePathArray.slice(1); // we don't want to have config/operational storage in path
110             // actualPath.push(PathUtilsService.createPathElement(mountPrefix, null, null, false));
111             // we want to push yang-ext:mount to the path - not if we have yang-ext:mount rootNode
112
113             mpApis.forEach(function (api) {
114                 api.subApis.forEach(function (subApi) {
115                     subApi.pathArray = addPathElemsToPathArray(actualPath, subApi.pathArray, 1);
116                 });
117             });
118         }
119
120         // TODO: add service's description
121         function getMPModulesAPI(api) {
122             var apiArray = api.split('/'),
123                 yangExtMountStr = mountPrefix;
124
125             if (apiArray[apiArray.length - 1] !== yangExtMountStr) {
126                 apiArray.push(yangExtMountStr);
127             }
128
129             return apiArray.slice(1).join('/');
130         }
131
132         // TODO: add service's description
133         function discoverMountPoints(api, getModulesCbk, callback) {
134             var modulesCbk = getModulesCbk || angular.noop,
135                 mpNodes = [],
136                 baseApiPath = getMPModulesAPI(api),
137                 time = {
138                     started: 0,
139                     finished: 0,
140                 };
141
142             YangUtilsRestangularService.setFullResponse(true);
143
144             time.started = new Date().getMilliseconds();
145
146             return YangUiApisService.getCustomModules(baseApiPath).then(
147                 function (response) {
148                     time.finished = new Date().getMilliseconds();
149
150                     var reqObj = {
151                         status: response.status,
152                         statusText: response.statusText,
153                         time: (time.finished - time.started),
154                     };
155
156                     YangUtilsService.processModulesMP(response.data.modules, baseApiPath, function (result, augments) {
157                         EventDispatcherService.dispatch(constants.EV_SRC_MAIN, 'Linking modules to Apis');
158                         var allRootNodes = result.map(function (node) {
159                             var copy = node.deepCopy(['augmentionGroups', 'augmentationId']);
160
161                             NodeWrapperService.wrapAll(copy);
162                             return copy;
163                         });
164
165                         var moduleNames = response.data.modules.module.map(function (m) {
166                             return m.name;
167                         });
168
169                         allRootNodes.forEach(function (n) {
170                             if (moduleNames.indexOf(n.module) > -1 && ['container', 'list'].indexOf(n.type) > -1) {
171                                 mpNodes.push(n);
172                             }
173                         });
174
175                         callback(mpNodes, augments, reqObj);
176                         YangUtilsRestangularService.setFullResponse(false);
177                     });
178                 },
179                 function (response) {
180                     time.finished = new Date().getMilliseconds();
181
182                     var reqObj = {
183                         status: response.status,
184                         statusText: response.statusText,
185                         time: (time.finished - time.started),
186                     };
187
188                     callback([], [], reqObj);
189                     YangUtilsRestangularService.setFullResponse(false);
190                 });
191         }
192
193         // TODO: add service's description
194         function createCustomButton(label, show, click){
195             return {
196                 label: label,
197                 show: show,
198                 onclick: click,
199             };
200         }
201     }
202
203 });