Merge "AAF integration in Policy SDK"
[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                 
242     }else if($scope.temp.policy.ruleProvider=="GUARD_YAML" || $scope.temp.policy.ruleProvider=="GUARD_MIN_MAX"){
243         if($scope.temp.policy.yamlparams.targets.length==0){
244                    $scope.temp.policy.yamlparams.targets = [];
245                 }
246         } else if ($scope.temp.policy.ruleProvider == "Rainy_Day") {
247                 if ($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0) {
248                         $scope.temp.policy.rainyday.treatmentTableChoices = [];
249                 }
250         }
251         $scope.attributeDatas = [ {
252                 "attributes" : $scope.temp.policy.attributes
253         } ];
254         $scope.addNewChoice = function() {
255                 var newItemNo = $scope.temp.policy.attributes.length + 1;
256                 $scope.temp.policy.attributes.push({
257                         'id' : 'choice' + newItemNo
258                 });
259         };
260         $scope.removeChoice = function() {
261                 var lastItem = $scope.temp.policy.attributes.length - 1;
262                 $scope.temp.policy.attributes.splice(lastItem);
263         };
264
265         $scope.settingsDatas = [ {
266                 "settings" : $scope.temp.policy.settings
267         } ];
268         $scope.addNewSettingsChoice = function() {
269                 var newItemNo = $scope.temp.policy.settings.length + 1;
270                 $scope.temp.policy.settings.push({
271                         'id' : 'choice' + newItemNo
272                 });
273         };
274         $scope.removeSettingsChoice = function() {
275                 var lastItem = $scope.temp.policy.settings.length - 1;
276                 $scope.temp.policy.settings.splice(lastItem);
277         };
278
279         $scope.addNewTarget = function() {
280                 $scope.temp.policy.yamlparams.targets.push('');
281         };
282         $scope.removeTarget = function() {
283                 var lastItem = $scope.temp.policy.yamlparams.targets.length - 1;
284                 $scope.temp.policy.yamlparams.targets.splice(lastItem);
285         };
286
287         $scope.addNewBL = function() {
288                 $scope.temp.policy.yamlparams.blackList.push('');
289         };
290     
291     $scope.removeBL = function(id) {
292         $scope.temp.policy.yamlparams.blackList = $scope.temp.policy.yamlparams.blackList.filter(function (obj){
293                         return obj !== id;
294                 });
295         };
296
297         $scope.treatmentDatas = [ {
298                 "treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices
299         } ];
300         
301         $scope.addNewTreatment = function() {
302                 $scope.temp.policy.rainyday.treatmentTableChoices.push({});
303         };
304         
305         $scope.removeTreatment = function() {
306                 var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length - 1;
307                 $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
308         };
309
310         $scope.workstepDictionaryDatas = [];
311         $scope.getWorkstepValues = function(bbidValue) {
312                 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
313                         var obj = $scope.rainyDayDictionaryDataEntity[i];
314                         if (obj.bbid == bbidValue) {
315                                 $scope.workstepDictionaryDatas.push(obj.workstep);
316                         }
317                 }
318         };
319
320         $scope.allowedTreatmentsDatas = [];
321         $scope.getTreatmentValues = function(bbidValue, workstepValue) {
322                 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
323                         var obj = $scope.rainyDayDictionaryDataEntity[i];
324                         if (obj.bbid == bbidValue && obj.workstep == workstepValue) {
325                                 var splitAlarm = obj.treatments.split(',');
326                                 for (var j = 0; j < splitAlarm.length; ++j) {
327                                         $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
328                                 }
329                         }
330                 }
331         };
332
333         $scope.ItemNo = 0;
334         $scope.ruleAlgorithmDatas = [ {
335                 "ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices
336         } ];
337
338         $scope.addNewRuleAlgorithm = function() {
339                 if ($scope.temp.policy.ruleAlgorithmschoices != null) {
340                         var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length + 1;
341                 } else {
342                         var newItemNo = 1;
343                 }
344                 if (newItemNo > 1) {
345                         var value = newItemNo - 1;
346                         $scope.attributeDictionaryDatas.push('A' + value);
347                 }
348                 $scope.temp.policy.ruleAlgorithmschoices.push({
349                         'id' : 'A' + newItemNo
350                 });
351         };
352
353         $scope.removeRuleAlgorithm = function() {
354                 var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length - 1;
355                 $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
356         };
357
358         $scope.providerListener = function(ruleProvider) {
359                 if (ruleProvider != "Custom") {
360                         $scope.temp.policy.ruleAlgorithmschoices = [];
361                         $scope.temp.policy.settings = [];
362                         $scope.temp.policy.attributes = [];
363                 }
364                 if (ruleProvider === "Raw") {
365                         $scope.notRawPolicy = false;
366                 }
367         };
368
369         $scope.importButton = true;
370         var fd;
371         $scope.uploadBLFile = function(files) {
372                 fd = new FormData();
373                 fd.append("file", files[0]);
374                 var fileExtension = files[0].name.split(".")[1];
375                 if (fileExtension == "xls") {
376                         $scope.importButton = false;
377                         $scope.$apply();
378                 } else {
379                         Notification.error("Upload the BlackList file which extends with .xls format.");
380                 }
381         };
382
383         function arrayUnique(array) {
384                 var a = array.concat();
385                 for (var i = 0; i < a.length; ++i) {
386                         for (var j = i + 1; j < a.length; ++j) {
387                                 if (a[i] === a[j])
388                                         a.splice(j--, 1);
389                         }
390                 }
391                 return a;
392         }
393
394         $scope.submitUpload = function() {
395                 $http.post("policycreation/importBlackListForDecisionPolicy", fd, {
396                         withCredentials : false,
397                         headers : {
398                                 'Content-Type' : undefined
399                         },
400                         transformRequest : angular.identity
401                 }).success(function(data) {
402                         $scope.data = JSON.parse(data.data);
403                         $scope.temp.policy.blackListEntries = $scope.data.blackListEntries;
404                         if ($scope.temp.policy.blackListEntries[0] !== "error") {
405                                 $scope.blackListEntries = arrayUnique($scope.temp.policy.blackListEntries.concat($scope.temp.policy.yamlparams.blackList));
406                                 $scope.temp.policy.appendBlackListEntries = $scope.data.appendBlackListEntries;
407                                 $scope.blackListEntries = $scope.blackListEntries.filter(function(obj) {
408                                         return !$scope.temp.policy.appendBlackListEntries.includes(obj);
409                                 });
410                                 if ($scope.blackListEntries.length == 0) {
411                                         $scope.validateButton = true;
412                                         Notification.error("Black Lists are empty. Minimum one entry required.");
413                                 } else {
414                                         $scope.temp.policy.blackListEntries = $scope.blackListEntries;
415                                         Notification.success("Blacklist File Uploaded Successfully.");
416                                         $scope.validateButton = false;
417                                         $scope.importButton = true;
418                                 }
419                         } else {
420                                 Notification.error("Blacklist File Upload Failed." + $scope.temp.policy.blackListEntries[1]);
421                         }
422                 }).error(function(data) {
423                         Notification.error("Blacklist File Upload Failed.");
424                 });
425         };
426
427         $scope.initializeBlackList = function() {
428                 if ($scope.temp.policy.blackListEntryType === "Use File Upload") {
429                         $scope.validateButton = true;
430                 } else {
431                         $scope.validateButton = false;
432                 }
433                 $("#importFile").val('');
434         };
435
436         $scope.exportBlackListEntries = function() {
437                 var uuu = "policycreation/exportDecisionBlackListEntries";
438                 var postData = {
439                         policyData : $scope.temp.policy,
440                         date : $scope.temp.model.modifiedDate,
441                         version : $scope.temp.model.version
442                 };
443                 $.ajax({
444                         type : 'POST',
445                         url : uuu,
446                         dataType : 'json',
447                         contentType : 'application/json',
448                         data : JSON.stringify(postData),
449                         success : function(data) {
450                                 $scope.$apply(function() {
451                                         $scope.data = data.data;
452                                         var url = '../' + $scope.data;
453                                         window.location = url;
454                                         Notification.success("BlackList Entries Exported Successfully.");
455                                 });
456                                 console.log($scope.data);
457                         },
458                         error : function(data) {
459                                 Notification.error("Error Occured while Exporting BlackList Entries.");
460                         }
461                 });
462         };
463 } ]);