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