[Policy-52, Policy-92, Policy-93] Policy Enhancements and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / DecisionPolicyController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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 angular.module('abs').controller('decisionPolicyController', ['$scope', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
21     $("#dialog").hide();
22     
23     $scope.policyNavigator;
24     $scope.savebutton = true;
25     $scope.refreshCheck = false;
26     
27     $scope.refresh = function(){
28         if($scope.refreshCheck){
29                 $scope.policyNavigator.refresh();
30         }
31         $scope.modal('createNewPolicy', true);
32         $scope.temp.policy = "";
33     };
34     
35     $scope.modal = function(id, hide) {
36         return $('#' + id).modal(hide ? 'hide' : 'show');
37     };
38
39         if($scope.temp.policy.ruleProvider==undefined){
40                 $scope.temp.policy.ruleProvider="Custom";
41         }
42                 
43         PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
44                 var j = data;
45                 $scope.data = JSON.parse(j.data);
46                 console.log($scope.data);
47                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
48                 console.log($scope.ecompNameDictionaryDatas);
49         }, function (error) {
50                 console.log("failed");
51         });
52
53         PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function (data) {
54                 var j = data;
55                 $scope.data = JSON.parse(j.data);
56                 console.log($scope.data);
57                 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
58                 console.log($scope.settingsDictionaryDatas);
59         }, function (error) {
60                 console.log("failed");
61         });
62
63         PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function (data) {
64                 var j = data;
65                 $scope.data = JSON.parse(j.data);
66                 console.log($scope.data);
67                 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
68                 console.log($scope.functionDefinitionDatas);
69         }, function (error) {
70                 console.log("failed");
71         });
72
73         PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function (data) {
74                 var j = data;
75                 $scope.data = JSON.parse(j.data);
76                 console.log($scope.data);
77                 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
78                 console.log($scope.attributeDictionaryDatas);
79         }, function (error) {
80                 console.log("failed");
81         });
82
83         PolicyAppService.getData('getDictionary/get_RainyDayDictionaryDataByName').then(function (data) {
84                 var j = data;
85                 $scope.data = JSON.parse(j.data);
86                 console.log($scope.data);
87         $scope.rainyDayDictionaryDatas = JSON.parse($scope.data.rainyDayDictionaryDatas);
88                 console.log($scope.rainyDayDictionaryDatas);
89         }, function (error) {
90                 console.log("failed");
91         });
92         
93     PolicyAppService.getData('getDictionary/get_RainyDayDictionaryData').then(function (data) {
94         var j = data;
95         $scope.data = JSON.parse(j.data);
96         console.log($scope.data);
97         $scope.rainyDayDictionaryDataEntity = JSON.parse($scope.data.rainyDayDictionaryDatas);
98         console.log($scope.rainyDayDictionaryDatasEntity);
99     }, function (error) {
100         console.log("failed");
101     });
102
103     function extend(obj, src) {
104         for (var key in src) {
105             if (src.hasOwnProperty(key)) obj[key] = src[key];
106         }
107         return obj;
108     }
109     
110     $scope.saveDecisionPolicy = function(policy){
111         if(policy.itemContent != undefined){
112                 $scope.refreshCheck = true; 
113                 $scope.policyNavigator = policy.itemContent;
114                 policy.itemContent = "";
115         }
116         $scope.savebutton = false;
117         console.log(policy);
118         var uuu = "policycreation/save_policy";
119                 var postData={policyData: policy};
120                 $.ajax({
121                         type : 'POST',
122                         url : uuu,
123                         dataType: 'json',
124                         contentType: 'application/json',
125                         data: JSON.stringify(postData),
126                         success : function(data){
127                                 $scope.$apply(function(){
128                                         $scope.data=data.policyData;
129                                         if($scope.data == 'success'){
130                                                 $scope.temp.policy.readOnly = 'true';
131                                                 Notification.success("Policy Saved Successfully.");     
132                                         }else if ($scope.data == 'PolicyExists'){
133                                                 $scope.savebutton = true;
134                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
135                                         }       
136                                 });
137                                 console.log($scope.data);
138                                 
139                         },
140                         error : function(data){
141                                 Notification.error("Error Occured while saving Policy.");
142                         }
143                 });
144     };
145     
146     $scope.validatePolicy = function(policy){
147         console.log(policy);
148         document.getElementById("validate").innerHTML = "";
149          var uuu = "policyController/validate_policy.htm";
150                 var postData={policyData: policy};
151                 $.ajax({
152                         type : 'POST',
153                         url : uuu,
154                         dataType: 'json',
155                         contentType: 'application/json',
156                         data: JSON.stringify(postData),
157                         success : function(data){
158                                 $scope.$apply(function(){
159                                         $scope.validateData = data.data.replace(/\"/g, "");
160                                                 $scope.data=data.data;
161                                                 if($scope.data == '"success"'){
162                                                         Notification.success("Validation Success.");
163                                                         $scope.savebutton = false;
164                                                 }else{
165                                                         Notification.error("Validation Failed.");
166                                                         document.getElementById("validate").innerHTML = $scope.validateData;
167                                                         document.getElementById("validate").style.color = "white";
168                                                         document.getElementById("validate").style.backgroundColor = "red";
169                                                         $scope.savebutton = true;
170                                                 }
171                                                 
172                                 });
173                                 console.log($scope.data);
174                         },
175                         error : function(data){
176                                 Notification.error("Validation Failed.");
177                                 $scope.savebutton = true; 
178                         }
179                 });
180     };
181    
182     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
183         $scope.temp.policy.attributes = [];
184         $scope.temp.policy.settings = [];
185         $scope.temp.policy.ruleAlgorithmschoices = [];
186         if(!$scope.temp.policy.yamlparams){
187                 $scope.temp.policy.yamlparams = {};
188         }
189         if(!$scope.temp.policy.yamlparams.targets){
190                 $scope.temp.policy.yamlparams.targets = [];
191         }
192         if(!$scope.temp.policy.yamlparams.blackList){
193                 $scope.temp.policy.yamlparams.blackList = [];
194         }
195         if(!$scope.temp.policy.rainyday){
196                 $scope.temp.policy.rainyday = {};
197         }
198         if(!$scope.temp.policy.rainyday.treatmentTableChoices){
199                 $scope.temp.policy.rainyday.treatmentTableChoices = [];
200         }
201     
202     }else if($scope.temp.policy.ruleProvider=="Custom"){
203            if($scope.temp.policy.attributes.length == 0){
204                    $scope.temp.policy.attributes = [];    
205            }
206            if($scope.temp.policy.settings.length == 0){
207                    $scope.temp.policy.settings = [];
208            }
209            if($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0){
210                    $scope.temp.policy.ruleAlgorithmschoices = [];
211            }
212     }else if($scope.temp.policy.ruleProvider=="GUARD_BL_YAML"){
213            if($scope.temp.policy.yamlparams.blackList.length==0){
214                    $scope.temp.policy.yamlparams.blackList = [];
215            }
216     }else if($scope.temp.policy.ruleProvider=="GUARD_YAML"){
217         if($scope.temp.policy.yamlparams.targets.length==0){
218                    $scope.temp.policy.yamlparams.targets = [];
219                 }
220     }else if($scope.temp.policy.ruleProvider=="Rainy_Day"){
221         if($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0){
222                 $scope.temp.policy.rainyday.treatmentTableChoices = [];
223         }
224     }
225     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
226     $scope.addNewChoice = function() {
227       var newItemNo = $scope.temp.policy.attributes.length+1;
228       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
229     };    
230     $scope.removeChoice = function() {
231       var lastItem = $scope.temp.policy.attributes.length-1;
232       $scope.temp.policy.attributes.splice(lastItem);
233     };
234     
235     $scope.settingsDatas = [{"settings" : $scope.temp.policy.settings}];
236     $scope.addNewSettingsChoice = function() {
237       var newItemNo = $scope.temp.policy.settings.length+1;
238       $scope.temp.policy.settings.push({'id':'choice'+newItemNo});
239     };    
240     $scope.removeSettingsChoice = function() {
241       var lastItem = $scope.temp.policy.settings.length-1;
242       $scope.temp.policy.settings.splice(lastItem);
243     };
244     
245     $scope.addNewTarget = function(){
246         $scope.temp.policy.yamlparams.targets.push('');
247     };
248     $scope.removeTarget = function(){
249         var lastItem = $scope.temp.policy.yamlparams.targets.length-1;
250         $scope.temp.policy.yamlparams.targets.splice(lastItem);
251     };
252     
253     $scope.addNewBL = function() {
254         $scope.temp.policy.yamlparams.blackList.push('');
255     };
256     $scope.removeBL = function() {
257         var lastItem = $scope.temp.policy.yamlparams.blackList.length-1;
258         $scope.temp.policy.yamlparams.blackList.splice(lastItem);
259     };
260     
261     $scope.treatmentDatas = [{"treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices}];
262     $scope.addNewTreatment = function() {
263         $scope.temp.policy.rainyday.treatmentTableChoices.push({});
264     };
265     $scope.removeTreatment = function() {
266         var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length-1;
267         $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
268     };
269     
270         $scope.workstepDictionaryDatas = [];
271         $scope.getWorkstepValues = function(bbidValue){
272                 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
273             var obj = $scope.rainyDayDictionaryDataEntity[i];
274             if (obj.bbid == bbidValue){
275                 $scope.workstepDictionaryDatas.push(obj.workstep);
276             }
277         }
278         };
279         
280         $scope.allowedTreatmentsDatas = [];
281         $scope.getTreatmentValues = function(bbidValue, workstepValue){
282                 for (var i = 0; i < $scope.rainyDayDictionaryDataEntity.length; ++i) {
283             var obj = $scope.rainyDayDictionaryDataEntity[i];
284             if (obj.bbid == bbidValue && obj.workstep == workstepValue){
285                 var splitAlarm = obj.treatments.split(',');
286                 for (var j = 0; j < splitAlarm.length; ++j) {
287                         $scope.allowedTreatmentsDatas.push(splitAlarm[j]);
288                 }
289             }
290         }       
291         };
292     
293     $scope.ItemNo = 0;
294     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices }];
295     
296     $scope.addNewRuleAlgorithm = function() {
297         if($scope.temp.policy.ruleAlgorithmschoices != null){
298                 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
299         }else{
300                 var newItemNo = 1;
301         }
302         if(newItemNo > 1){
303                 var value = newItemNo-1;
304                 $scope.attributeDictionaryDatas.push('A'+value);
305         }
306         $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
307     };
308     
309     $scope.removeRuleAlgorithm = function() {
310       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
311       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
312     };
313     
314     $scope.providerListener = function(ruleProvider) {
315         if (ruleProvider!="Custom"){
316                 $scope.temp.policy.ruleAlgorithmschoices  = [];
317                 $scope.temp.policy.settings = [];
318                 $scope.temp.policy.attributes = [];
319         }
320     };
321 }]);