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