Yangman - changed event strings to constants
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / services / plugins / ymDisplayMountPoints.services.js
1 define([
2     'app/yangman/services/mount-points-connector.services',
3 ], function (yangman) {
4     'use strict';
5
6     angular.module('app.yangman').service('ymDisplayMountPoints', DisplayMountPoints);
7
8     DisplayMountPoints.$inject =
9         ['MountPointsConnectorService', '$timeout', 'YangUtilsService', '$filter', 'ApiBuilderService', 'constants'];
10
11     function DisplayMountPoints(
12         MountPointsConnectorService, $timeout, YangUtilsService, $filter, ApiBuilderService, constants) {
13         var loadId = 0;
14
15         return {
16             module: ['network-topology', 'opendaylight-inventory', 'network-topology', 'opendaylight-inventory'],
17             revision: null,
18             pathString: [
19                 'operational/network-topology:network-topology/topology/{topology-id}/node/{node-id}/',
20                 'operational/opendaylight-inventory:nodes/node/{id}/',
21             ],
22             label: 'YANGMAN_CUST_MOUNT_POINTS',
23             hideButtonOnSelect: true,
24             getCallback: displayMountPointsCallback,
25         };
26
27         function displayMountPointsCallback(args) {
28             var controller = args.controller,
29                 scope = args.scope,
30                 path = scope.selectedSubApi.buildApiRequestString();
31
32             scope.rootBroadcast(constants.YANGMAN_SET_LOADING_BOX, true, function () {
33                 scope.setLeftPanel(0);
34                 $timeout(function () {
35                     MountPointsConnectorService.discoverMountPoints(path, getNodesMPData, createMPStructure);
36                 }, 1000);
37             });
38
39             /**
40              * Get Mount point data from received raw data
41              * @param data
42              * @returns {*}
43              */
44             function getNodesMPData(data) {
45                 var node = data.node[0];
46                 return node && node['netconf-node-inventory:initial-capability'] ?
47                     node['netconf-node-inventory:initial-capability'].map(function (c) {
48                         return c.slice(c.lastIndexOf(')') + 1);
49                     }) : [];
50             }
51
52             // TODO :: description
53             function findFirstSubApiIndex(subApis) {
54                 var firstConfigSubApiIndex = 0;
55
56                 subApis.some(function (sa, index) {
57                     var condition = sa.storage === constants.DATA_STORE_CONFIG;
58                     if (condition) {
59                         firstConfigSubApiIndex = index;
60                     }
61                     return condition;
62                 });
63
64                 return firstConfigSubApiIndex;
65             }
66
67             /**
68              * Create base params for setup mount points in app
69              * @param mpNodes
70              * @param mpAugments
71              * @param reqObj
72              */
73             function createMPStructure(mpNodes, mpAugments, reqObj) {
74                 if (mpNodes.length){
75                     var mpRootNode = MountPointsConnectorService.createMPRootNode(mpNodes),
76                         mountPointApis = ApiBuilderService.processAllRootNodes([mpRootNode]),
77                     // root node has isConfigStm undefined, we need to create root config SubApi by hand
78                     // if we set the variable isConfigStm to true and then generate the subApis it will do it
79                     // incorrectly because, the variable is inherited to children and we would malform the data
80                     // we need just to get operational root subApi...
81                         rootApi = mountPointApis[0],
82                         rootOperSubApi = rootApi.subApis.filter(function (sa) {
83                             return sa.pathTemplateString === 'yang-ext:mount/' && sa.storage === constants.DATA_STORE_OPERATIONAL;
84                         })[0];
85
86                     if (rootOperSubApi) {
87                         var rootConfigSubApi =  rootOperSubApi.clone(), // clone it and...
88                             firstConfigSubApiIndex = findFirstSubApiIndex(rootApi.subApis);
89                         // we need to find first index of config
90                         // subApi - because generating treeApis depends on order
91
92                         // set storage to config
93                         rootConfigSubApi.storage = constants.DATA_STORE_CONFIG;
94                         rootConfigSubApi.pathArray[0].name = constants.DATA_STORE_CONFIG;
95
96                         // and add it to rest of the apis
97                         rootApi.subApis.splice(firstConfigSubApiIndex, 0, rootConfigSubApi);
98                         rootConfigSubApi.parent = rootApi;
99                     }
100
101                     var mountPointTreeApis = YangUtilsService.generateApiTreeData(mountPointApis),
102                         pathItems = path.split('/');
103
104                     MountPointsConnectorService.updateMountPointApis(scope.selectedSubApi.pathArray, mountPointApis);
105
106                     // call initialization after necessary things are loaded
107                     controller.initMountPoint(mountPointTreeApis, mountPointApis, mpAugments, reqObj);
108
109                     scope.rootBroadcast(
110                         constants.YANGMAN_SET_MODULE_LIST_TITLE,
111                         pathItems[pathItems.length - 1] + ' [ ' + $filter('translate')(constants.YANGMAN_MOUNT_POINT) + ' ]'
112                     );
113
114                     controller.selectedPluginsButtons.push(
115                         MountPointsConnectorService.createCustomButton(constants.YANGMAN_CANCEL_MP, function (){
116                             return controller.selectedPlugin.label === constants.YANGMAN_CUST_MOUNT_POINTS;
117                         },
118                         function (){
119                             controller.unsetPluginFunctionality();
120                         })
121                     );
122
123                 } else {
124                     $timeout(function (){
125                         controller.selectedPlugin = null;
126                         scope.rootBroadcast(constants.YANGMAN_SET_LOADING_BOX, false);
127                         scope.rootBroadcast(constants.YANGMAN_SHOW_TOAST, constants.YANGMAN_NO_MOUNT_POINT);
128                     }, 100);
129                 }
130             }
131
132         }
133     }
134 });