[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / controllers / policyManager.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 app.controller('PolicyManagerController', [
21         '$scope', '$q', '$window', '$cookies', 'policyManagerConfig', 'item', 'policyNavigator', 'policyUploader', 'Notification','PolicyAppService',
22         function($scope, $q, $Window, $cookies, policyManagerConfig, Item, PolicyNavigator, PolicyUploader, Notification, PolicyAppService ) {
23                 
24         $scope.isDisabled = true;
25         $scope.superAdminId = false;
26         $scope.exportPolicyId = false;
27         $scope.importPolicyId = false;
28         $scope.createScopeId = false;
29         $scope.deleteScopeId = false;
30         $scope.renameId = false;
31         $scope.createPolicyId = false;
32         $scope.cloneId = false;
33         $scope.editPolicyId = false;
34         $scope.switchVersionId = false;
35         $scope.describePolicyId = false;
36         $scope.viewPolicyId = false;
37         $scope.deletePolicyId = false;
38         PolicyAppService.getData('get_LockDownData').then(function(data){
39                 var j = data;
40                 $scope.data = JSON.parse(j.data);
41                 $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
42                 if($scope.lockdowndata[0].lockdown == true){
43                         $scope.isDisabled = true;
44                 }else{
45                         $scope.isDisabled = false;
46                 }
47                 console.log($scope.data);
48         },function(error){
49                 console.log("failed");
50         });
51         
52         PolicyAppService.getData('getDictionary/get_DescriptiveScopeByName').then(function(data){
53                 var j = data;
54                 $scope.data = JSON.parse(j.data);
55                 console.log($scope.data);
56                 $scope.descriptiveScopeDictionaryDatas = JSON.parse($scope.data.descriptiveScopeDictionaryDatas);       
57         }, function (error) {
58                 console.log("failed");
59         });
60
61         PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data){
62                 var j = data;
63                 $scope.data = JSON.parse(j.data);
64                 console.log($scope.data);
65                 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);       
66         }, function (error) {
67                 console.log("failed");
68         });
69
70         PolicyAppService.getData('getDictionary/get_VSCLActionDataByName').then(function(data){
71                 var j = data;
72                 $scope.data = JSON.parse(j.data);
73                 console.log($scope.data);
74                 $scope.vsclActionDictionaryDatas = JSON.parse($scope.data.vsclActionDictionaryDatas);   
75         }, function (error) {
76                 console.log("failed");
77         });
78
79         PolicyAppService.getData('getDictionary/get_VNFTypeDataByName').then(function(data){
80                 var j = data;
81                 $scope.data = JSON.parse(j.data);
82                 console.log($scope.data);
83                 $scope.vnfTypeDictionaryDatas = JSON.parse($scope.data.vnfTypeDictionaryDatas); 
84         }, function (error) {
85                 console.log("failed");
86         });
87
88         
89         PolicyAppService.getData('get_UserRolesData').then(function (data) {
90            var j = data;
91            $scope.data = JSON.parse(j.data);
92            console.log($scope.data);
93            $scope.userRolesDatas = JSON.parse($scope.data.userRolesDatas);
94            console.log($scope.userRolesDatas);
95            if($scope.userRolesDatas[0] == 'super-admin'){
96                   $scope.superAdminId = true;
97                   $scope.exportPolicyId = true;
98               $scope.importPolicyId = true;
99                   $scope.createScopeId = true;
100               $scope.deleteScopeId = true;
101               $scope.renameId = true;
102               $scope.createPolicyId = true;
103               $scope.cloneId = true;
104               $scope.editPolicyId = true;
105               $scope.switchVersionId = true;
106               $scope.describePolicyId = true;
107               $scope.viewPolicyId = true;
108               $scope.deletePolicyId = true; 
109            }else if($scope.userRolesDatas[0] == 'super-editor' || $scope.userRolesDatas[0] == 'editor'){
110                   $scope.exportPolicyId = true;
111               $scope.importPolicyId = true; 
112                   $scope.cloneId = true;
113                   $scope.editPolicyId = true;
114                   $scope.createPolicyId = true;
115                   $scope.cloneId = true;
116                   $scope.editPolicyId = true;
117                   $scope.switchVersionId = true;
118                   $scope.describePolicyId = true;
119                   $scope.viewPolicyId = true;
120                   $scope.deletePolicyId = true; 
121            }else if($scope.userRolesDatas[0] == 'super-guest' || $scope.userRolesDatas[0] == 'guest'){
122                   $scope.describePolicyId = true;
123                   $scope.viewPolicyId = true;
124            }else if($scope.userRolesDatas[0] == 'admin'){
125                   $scope.exportPolicyId = true;
126               $scope.importPolicyId = true;
127                   $scope.createScopeId = true;
128                   $scope.renameId = true;
129                   $scope.createPolicyId = true;
130                   $scope.cloneId = true;
131                   $scope.editPolicyId = true;
132                   $scope.switchVersionId = true;
133                   $scope.describePolicyId = true;
134                   $scope.viewPolicyId = true;
135                   $scope.deletePolicyId = true;  
136            }
137            }, function (error) {
138               console.log("failed");
139         });
140         
141         $scope.config = policyManagerConfig;
142         $scope.reverse = false;
143         $scope.predicate = ['model.type', 'model.name'];
144         $scope.order = function(predicate) {
145             $scope.reverse = ($scope.predicate[1] === predicate) ? !$scope.reverse : false;
146             $scope.predicate[1] = predicate;
147         };
148
149         $scope.query = '';
150         $scope.temp = new Item();
151         $scope.policyNavigator = new PolicyNavigator();
152         $scope.policyUploader = PolicyUploader;
153         $scope.uploadFileList = [];
154
155         $scope.setTemplate = function(name) {
156             $scope.viewTemplate = $cookies.viewTemplate = name;
157         };
158
159         $scope.touch = function(item) {
160             item = item instanceof Item ? item : new Item();
161             item.revert();
162             $scope.temp = item;
163         };
164
165         $scope.smartClick = function(item) {
166             if (item.isFolder()) {
167                 return $scope.policyNavigator.folderClick(item);
168             }
169             if (item.isEditable()) {
170                 return $scope.openEditItem(item);
171             }
172         };
173
174         $scope.openEditItem = function(item) {
175             item.getContent();
176             $scope.modal('createNewPolicy');
177             return $scope.touch(item);
178         };
179
180         $scope.modal = function(id, hide) {
181             return $('#' + id).modal(hide ? 'hide' : 'show');
182         };
183
184         $scope.isInThisPath = function(path) {
185             var currentPath = $scope.policyNavigator.currentPath.join('/');
186             return currentPath.indexOf(path) !== -1;
187         };
188          
189        $scope.watch = function(item){
190            var uuu = "watchPolicy";
191            var data = {name : item.model.name,
192                            path : item.model.path};
193            var postData={watchData: data};
194            $.ajax({
195                type : 'POST',
196                url : uuu,
197                dataType: 'json',
198                contentType: 'application/json',
199                data: JSON.stringify(postData),
200                success : function(data){
201                    $scope.$apply(function(){
202                        $scope.watchData=data.watchData;});
203                    Notification.success($scope.watchData);
204                    console.log($scope.watchData);
205                },
206                error : function(data){
207                    alert("Error while saving.");
208                }
209            });
210        };
211      
212          
213        $scope.refresh = function(){
214            $scope.policyNavigator.refresh();
215        };
216        
217          $scope.switchVersion = function(item){
218                  if ($scope.policyNavigator.fileNameExists(item.tempModel.content.activeVersion)) {
219                  item.error = 'Invalid filename or already exists, specify another name';
220                  return false;
221              }
222                  item.getSwitchVersionContent().then(function(){
223                          $scope.policyNavigator.refresh();
224                          $scope.modal('switchVersion', true);
225                  });
226          };
227
228         $scope.copy = function(item) {
229             var samePath = item.tempModel.path.join() === item.model.path.join();
230             if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
231                 item.error = 'Invalid filename or already exists, specify another name';
232                 return false;
233             }
234             item.copy().then(function() {
235                 $scope.policyNavigator.refresh();
236                 $scope.modal('copy', true);
237             });
238         };
239
240         $scope.remove = function(item) {
241             item.remove().then(function() {
242                 $scope.policyNavigator.refresh();
243                 $scope.modal('delete', true);
244             });
245         };
246
247         $scope.removePolicy = function(item) {
248             item.removePolicy().then(function() {
249                 $scope.policyNavigator.refresh();
250                 $scope.modal('deletePolicy', true);
251             });
252         };
253         
254         $scope.rename = function(item) {
255             var samePath = item.tempModel.path.join() === item.model.path.join();
256             if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
257                 item.error = 'Invalid filename or already exists, specify another name';
258                 return false;
259             }
260             item.rename().then(function() {
261                 $scope.policyNavigator.refresh();
262                 $scope.modal('rename', true);
263             });
264         };
265         
266         $scope.move = function(item) {
267             var samePath = item.tempModel.path.join() === item.model.path.join();
268             if (samePath && $scope.policyNavigator.fileNameExists(item.tempModel.name)) {
269                 item.error = 'Invalid filename or already exists, specify another name';
270                 return false;
271             }
272             item.move().then(function() {
273                 $scope.policyNavigator.refresh();
274                 $scope.modal('move', true);
275             });
276         };
277
278         $scope.createFolder = function(item) {
279             var name = item.tempModel.name && item.tempModel.name.trim();
280             item.tempModel.type = 'dir';
281             item.tempModel.path = $scope.policyNavigator.currentPath;
282             if (name && !$scope.policyNavigator.fileNameExists(name)) {
283                 item.createFolder().then(function() {
284                     $scope.policyNavigator.refresh();
285                     $scope.modal('newfolder', true);
286                 });
287             } else {
288                 item.error = 'Invalid filename or already exists, specify another name';
289                 return false;
290             }
291         };
292
293         $scope.subScopeFolder = function(item) {
294                 var name = item.tempModel.name +"\\" + item.tempModel.subScopename && item.tempModel.name.trim() + "\\"+item.tempModel.subScopename.trim() ;
295                 item.tempModel.type = 'dir';
296                 item.tempModel.path = $scope.policyNavigator.currentPath;
297                 if (name && !$scope.policyNavigator.fileNameExists(name)) {
298                         item.getScopeContent().then(function() {
299                                 $scope.policyNavigator.refresh();
300                                 $scope.modal('addSubScope', true);
301                         });
302                 } else {
303                         item.error = 'Invalid filename or already exists, specify another name';
304                         return false;
305                 }
306         };
307         
308         $scope.closefunction = function(fianlPath){
309                 $scope.policyNavigator.policyrefresh(fianlPath);
310         };
311
312         $scope.uploadFiles = function() {
313             $scope.policyUploader.upload($scope.uploadFileList, $scope.policyNavigator.currentPath).then(function() {
314                 $scope.policyNavigator.refresh();
315                 $scope.modal('uploadfile', true);
316             }, function(data) {
317                 var errorMsg = data.result && data.result.error || 'Error Occured while Uploading....';
318                 $scope.temp.error = errorMsg;
319             });
320         };
321
322         $scope.getQueryParam = function(param) {
323             var found;
324             window.location.search.substr(1).split('&').forEach(function(item) {
325                 if (param ===  item.split('=')[0]) {
326                     found = item.split('=')[1];
327                     return false;
328                 }
329             });
330             return found;
331         };
332
333         $scope.isWindows = $scope.getQueryParam('server') === 'Windows';
334         $scope.policyNavigator.refresh();
335     }]);