Fixed Sonar issues for Console Logs
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / DecisionPolicyController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2019 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 angular.module('abs').controller('decisionPolicyController', 
21     [ '$scope', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', '$http', 
22     function($scope, PolicyAppService, PolicyNavigator, modalService, $modal, Notification, $http) {
23     $("#dialog").hide();
24
25     $scope.policyNavigator;
26     $scope.savebutton = true;
27     $scope.refreshCheck = false;
28     $scope.disableOnCreate = false;
29     $scope.notRawPolicy = true;
30
31     if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
32     $scope.disableOnCreate = true;
33     $scope.temp.policy = {
34         policyType : "Decision"
35     }
36     }
37
38     $scope.refresh = function() {
39     if ($scope.refreshCheck) {
40         $scope.policyNavigator.refresh();
41     }
42     $scope.modal('createNewPolicy', true);
43     $scope.temp.policy = "";
44     };
45
46     $scope.modal = function(id, hide) {
47     return $('#' + id).modal(hide ? 'hide' : 'show');
48     };
49
50     if ($scope.temp.policy.ruleProvider == undefined) {
51     $scope.temp.policy.ruleProvider = "Custom";
52     }
53
54     if ($scope.temp.policy.blackListEntryType == undefined) {
55     $scope.temp.policy.blackListEntryType = "Use Manual Entry";
56     }
57
58     PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function(data) {
59     var j = data;
60     $scope.data = JSON.parse(j.data);
61     $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
62     });
63
64     PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function(data) {
65     var j = data;
66     $scope.data = JSON.parse(j.data);
67     $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
68     });
69
70     PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function(data) {
71     var j = data;
72     $scope.data = JSON.parse(j.data);
73     $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
74     });
75
76     PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function(data) {
77     var j = data;
78     $scope.data = JSON.parse(j.data);
79     $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
80     });
81
82     PolicyAppService.getData('getDictionary/get_RainyDayDictionaryDataByName').then(function(data) {
83     var j = data;
84     $scope.data = JSON.parse(j.data);
85     $scope.rainyDayDictionaryDatas = JSON.parse($scope.data.rainyDayDictionaryDatas);
86     });
87
88     PolicyAppService.getData('getDictionary/get_RainyDayDictionaryData').then(function(data) {
89     var j = data;
90     $scope.data = JSON.parse(j.data);
91     $scope.rainyDayDictionaryDataEntity = JSON.parse($scope.data.rainyDayDictionaryDatas);
92     });
93
94     $scope.saveDecisionPolicy = function(policy) {
95     if (policy.itemContent != undefined) {
96         $scope.refreshCheck = true;
97         $scope.policyNavigator = policy.itemContent;
98         policy.itemContent = "";
99     }
100     $scope.savebutton = false;
101     var uuu = "policycreation/save_policy";
102     var postData = {
103         policyData : policy
104     };
105     $.ajax({
106         type : 'POST',
107         url : uuu,
108         dataType : 'json',
109         contentType : 'application/json',
110         data : JSON.stringify(postData),
111         success : function(data) {
112         $scope.$apply(function() {
113             $scope.data = data.policyData;
114             if ($scope.data == 'success') {
115             $scope.temp.policy.readOnly = 'true';
116             Notification.success("Policy Saved Successfully.");
117             } else if ($scope.data == 'PolicyExists') {
118             $scope.savebutton = true;
119             Notification.error("Policy Already Exists with Same Name in Scope.");
120             }
121         });
122
123         },
124         error : function(data) {
125         Notification.error("Error Occured while saving Policy.");
126         }
127     });
128     };
129
130     $scope.validatePolicy = function(policy) {
131     document.getElementById("validate").innerHTML = "";
132     var uuu = "policyController/validate_policy.htm";
133     var postData = {
134         policyData : policy
135     };
136     $.ajax({
137         type : 'POST',
138         url : uuu,
139         dataType : 'json',
140         contentType : 'application/json',
141         data : JSON.stringify(postData),
142         success : function(data) {
143         $scope.$apply(function() {
144             $scope.validateData = data.data.replace(/\"/g, "");
145             $scope.data = data.data;
146             if ($scope.data == '"success"') {
147             Notification.success("Validation Success.");
148             $scope.savebutton = false;
149             } else {
150             Notification.error("Validation Failed.");
151             document.getElementById("validate").innerHTML = $scope.validateData;
152             document.getElementById("validate").style.color = "white";
153             document.getElementById("validate").style.backgroundColor = "red";
154             $scope.savebutton = true;
155             }
156
157         });
158         },
159         error : function(data) {
160         Notification.error("Validation Failed.");
161         $scope.savebutton = true;
162         }
163     });
164     };
165
166     if (!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly) {
167     $scope.temp.policy.attributes = [];
168     $scope.temp.policy.settings = [];
169     $scope.temp.policy.ruleAlgorithmschoices = [];
170     if (!$scope.temp.policy.yamlparams) {
171         $scope.temp.policy.yamlparams = {};
172     }
173     if (!$scope.temp.policy.yamlparams.targets) {
174         $scope.temp.policy.yamlparams.targets = [];
175     }
176     if (!$scope.temp.policy.yamlparams.blackList) {
177         $scope.temp.policy.yamlparams.blackList = [];
178     }
179     if (!$scope.temp.policy.rainyday) {
180         $scope.temp.policy.rainyday = {};
181     }
182     if (!$scope.temp.policy.rainyday.treatmentTableChoices) {
183         $scope.temp.policy.rainyday.treatmentTableChoices = [];
184     }
185
186     } else if ($scope.temp.policy.ruleProvider == "Custom") {
187     if ($scope.temp.policy.attributes.length == 0) {
188         $scope.temp.policy.attributes = [];
189     }
190     if ($scope.temp.policy.settings.length == 0) {
191         $scope.temp.policy.settings = [];
192     }
193     if ($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0) {
194         $scope.temp.policy.ruleAlgorithmschoices = [];
195     }
196     } else if ($scope.temp.policy.ruleProvider == "GUARD_BL_YAML") {
197     if ($scope.temp.policy.yamlparams.blackList == null || $scope.temp.policy.yamlparams.blackList.length == 0) {
198         $scope.temp.policy.yamlparams.blackList = [];
199     }
200     if ($scope.temp.policy.blackListEntries == null || $scope.temp.policy.blackListEntries.length == 0) {
201         $scope.temp.policy.blackListEntries = [];
202     }
203     $scope.blackListEntries = [];
204     $scope.temp.policy.appendBlackListEntries = [];
205     $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
206     
207     }else if($scope.temp.policy.ruleProvider=="GUARD_YAML" || $scope.temp.policy.ruleProvider=="GUARD_MIN_MAX"){
208         if($scope.temp.policy.yamlparams.targets.length==0){
209         $scope.temp.policy.yamlparams.targets = [];
210             }
211     } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
212     if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
213         $scope.temp.policy.rainyday.treatmentTableChoices = [];
214     }
215     }
216     $scope.attributeDatas = [ {
217     "attributes" : $scope.temp.policy.attributes
218     } ];
219     $scope.addNewChoice = function() {
220     var newItemNo = $scope.temp.policy.attributes.length + 1;
221     $scope.temp.policy.attributes.push({
222         'id' : 'choice' + newItemNo
223     });
224     };
225     $scope.removeChoice = function() {
226     var lastItem = $scope.temp.policy.attributes.length - 1;
227     $scope.temp.policy.attributes.splice(lastItem);
228     };
229
230     $scope.settingsDatas = [ {
231     "settings" : $scope.temp.policy.settings
232     } ];
233     $scope.addNewSettingsChoice = function() {
234     var newItemNo = $scope.temp.policy.settings.length + 1;
235     $scope.temp.policy.settings.push({
236         'id' : 'choice' + newItemNo
237     });
238     };
239     $scope.removeSettingsChoice = function() {
240     var lastItem = $scope.temp.policy.settings.length - 1;
241     $scope.temp.policy.settings.splice(lastItem);
242     };
243
244     $scope.addNewTarget = function() {
245     $scope.temp.policy.yamlparams.targets.push('');
246     };
247     $scope.removeTarget = function() {
248     var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
249     $scope.temp.policy.yamlparams.targets.splice(lastItem);
250     };
251
252     $scope.addNewBL = function() {
253     $scope.temp.policy.yamlparams.blackList.push('');
254     };
255     
256     $scope.removeBL = function(id) {
257         $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function (obj){
258         return obj !== id;
259     });
260     };
261
262     $scope.treatmentDatas = [ {
263     "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
264     } ];
265     
266     $scope.addNewTreatment = function() {
267     $scope.temp.policy.rainyday.treatmentTableChoices.push({});
268     };
269     
270     $scope.removeTreatment = function() {
271     var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
272     $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
273     };
274
275     $scope.workstepDictionaryDatas = [];
276     $scope.getWorkstepValues = function(bbidValue) {
277     for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
278         var obj = $scope.rainyDayDictionaryDataEntity[i];
279         if (obj.bbid == bbidValue) {
280         $scope.workstepDictionaryDatas.push(obj.workstep);
281         }
282     }
283     };
284
285     $scope.allowedTreatmentsDatas = [];
286     $scope.getTreatmentValues = function(bbidValue, workstepValue) {
287     for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
288         var obj = $scope.rainyDayDictionaryDataEntity[i];
289         if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
290         var splitAlarm = obj.treatments.split(',');
291         for (var j = 0; j < splitAlarm.length; ++j) {
292             $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
293         }
294         }
295     }
296     };
297
298     $scope.ItemNo = 0;
299     $scope.ruleAlgorithmDatas = [ {
300     "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
301     } ];
302
303     $scope.addNewRuleAlgorithm = function() {
304     if ($scope.temp.policy.ruleAlgorithmschoices != null) {
305         var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
306     } else {
307         var newItemNo = 1;
308     }
309     if (newItemNo > 1) {
310         var value = newItemNo - 1;
311         $scope.attributeDictionaryDatas.push('A' + value);
312     }
313     $scope.temp.policy.ruleAlgorithmschoices.push({
314         'id' : 'A' + newItemNo
315     });
316     };
317
318     $scope.removeRuleAlgorithm = function() {
319     var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
320     $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
321     };
322
323     $scope.providerListener = function(ruleProvider) {
324     if (ruleProvider != "Custom") {
325         $scope.temp.policy.ruleAlgorithmschoices = [];
326         $scope.temp.policy.settings = [];
327         $scope.temp.policy.attributes = [];
328     }
329     if (ruleProvider === "Raw") {
330         $scope.notRawPolicy = false;
331     }
332     };
333
334     $scope.importButton = true;
335     var fd;
336     $scope.uploadBLFile = function(files) {
337     fd = new FormData();
338     fd.append("file", files[0]);
339     var fileExtension = files[0].name.split(".")[1];
340     if (fileExtension == "xls") {
341         $scope.importButton = false;
342         $scope.$apply();
343     } else {
344         Notification.error("Upload the BlackList file which extends with .xls format.");
345     }
346     };
347
348     function arrayUnique(array) {
349     var a = array.concat();
350     for (var i = 0; i < a.length; ++i) {
351         for (var j = i + 1; j < a.length; ++j) {
352         if (a[i] === a[j])
353             a.splice(j--, 1);
354         }
355     }
356     return a;
357     }
358
359     $scope.submitUpload = function() {
360     $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
361         withCredentials : false,
362         headers : {
363         'Content-Type' : undefined
364         },
365         transformRequest : angular.identity
366     }).success(function(data) {
367         $scope.data = JSON.parse(data.data);
368         $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
369         if ($scope.temp.policy.blackListEntries[0] !== "error") {
370         $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
371         $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
372         $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
373             return !$scope.temp.policy.appendBlackListEntries.includes(obj);
374         });
375         if ($scope.blackListEntries.length == 0) {
376             $scope.validateButton = true;
377             Notification.error("Black Lists are empty. Minimum one entry required.");
378         } else {
379             $scope.temp.policy.blackListEntries = $scope.blackListEntries;
380             Notification.success("Blacklist File Uploaded Successfully.");
381             $scope.validateButton = false;
382             $scope.importButton = true;
383         }
384         } else {
385         Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
386         }
387     }).error(function(data) {
388         Notification.error("Blacklist File Upload Failed.");
389     });
390     };
391
392     $scope.initializeBlackList = function() {
393     if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
394         $scope.validateButton = true;
395     } else {
396         $scope.validateButton = false;
397     }
398     $("#importFile").val('');
399     };
400
401     $scope.exportBlackListEntries = function() {
402     var uuu = "policycreation/exportDecisionBlackListEntries";
403     var postData = {
404         policyData : $scope.temp.policy,
405         date : $scope.temp.model.modifiedDate,
406         version : $scope.temp.model.version
407     };
408     $.ajax({
409         type : 'POST',
410         url : uuu,
411         dataType : 'json',
412         contentType : 'application/json',
413         data : JSON.stringify(postData),
414         success : function(data) {
415         $scope.$apply(function() {
416             $scope.data = data.data;
417             var url = '../' + $scope.data;
418             window.location = url;
419             Notification.success("BlackList Entries Exported Successfully.");
420         });
421         },
422         error : function(data) {
423         Notification.error("Error Occured while Exporting BlackList Entries.");
424         }
425     });
426     };
427 } ]);