[POLICY-67] Add new Rainy Day Decision Policy
[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     function extend(obj, src) {
84         for (var key in src) {
85             if (src.hasOwnProperty(key)) obj[key] = src[key];
86         }
87         return obj;
88     }
89     
90     $scope.saveDecisionPolicy = function(policy){
91         if(policy.itemContent != undefined){
92                 $scope.refreshCheck = true; 
93                 $scope.policyNavigator = policy.itemContent;
94                 policy.itemContent = "";
95         }
96         $scope.savebutton = false;
97         console.log(policy);
98         var uuu = "policycreation/save_policy";
99                 var postData={policyData: policy};
100                 $.ajax({
101                         type : 'POST',
102                         url : uuu,
103                         dataType: 'json',
104                         contentType: 'application/json',
105                         data: JSON.stringify(postData),
106                         success : function(data){
107                                 $scope.$apply(function(){
108                                         $scope.data=data.policyData;
109                                         if($scope.data == 'success'){
110                                                 $scope.temp.policy.readOnly = 'true';
111                                                 Notification.success("Policy Saved Successfully.");     
112                                         }else if ($scope.data == 'PolicyExists'){
113                                                 $scope.savebutton = true;
114                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
115                                         }       
116                                 });
117                                 console.log($scope.data);
118                                 
119                         },
120                         error : function(data){
121                                 Notification.error("Error Occured while saving Policy.");
122                         }
123                 });
124     };
125     
126     $scope.validatePolicy = function(policy){
127         console.log(policy);
128         document.getElementById("validate").innerHTML = "";
129          var uuu = "policyController/validate_policy.htm";
130                 var postData={policyData: policy};
131                 $.ajax({
132                         type : 'POST',
133                         url : uuu,
134                         dataType: 'json',
135                         contentType: 'application/json',
136                         data: JSON.stringify(postData),
137                         success : function(data){
138                                 $scope.$apply(function(){
139                                         $scope.validateData = data.data.replace(/\"/g, "");
140                                                 $scope.data=data.data;
141                                                 if($scope.data == '"success"'){
142                                                         Notification.success("Validation Success.");
143                                                         $scope.savebutton = false;
144                                                 }else{
145                                                         Notification.error("Validation Failed.");
146                                                         document.getElementById("validate").innerHTML = $scope.validateData;
147                                                         document.getElementById("validate").style.color = "white";
148                                                         document.getElementById("validate").style.backgroundColor = "red";
149                                                         $scope.savebutton = true;
150                                                 }
151                                                 
152                                 });
153                                 console.log($scope.data);
154                         },
155                         error : function(data){
156                                 Notification.error("Validation Failed.");
157                                 $scope.savebutton = true; 
158                         }
159                 });
160     };
161    
162     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
163         $scope.temp.policy.attributes = [];
164         $scope.temp.policy.settings = [];
165         $scope.temp.policy.ruleAlgorithmschoices = [];
166         if(!$scope.temp.policy.yamlparams){
167                 $scope.temp.policy.yamlparams = {};
168         }
169         if(!$scope.temp.policy.yamlparams.blackList){
170                 $scope.temp.policy.yamlparams.blackList = [];
171         }
172         if(!$scope.temp.policy.rainyday){
173                 $scope.temp.policy.rainyday = {};
174         }
175         if(!$scope.temp.policy.rainyday.treatmentTableChoices){
176                 $scope.temp.policy.rainyday.treatmentTableChoices = [];
177         }
178     
179     }else if($scope.temp.policy.ruleProvider=="Custom"){
180            if($scope.temp.policy.attributes.length == 0){
181                    $scope.temp.policy.attributes = [];
182            }
183            if($scope.temp.policy.settings.length == 0){
184                    $scope.temp.policy.settings = [];
185            }
186            if($scope.temp.policy.ruleAlgorithmschoices == null || $scope.temp.policy.ruleAlgorithmschoices.length == 0){
187                    $scope.temp.policy.ruleAlgorithmschoices = [];
188            }
189     }else if($scope.temp.policy.ruleProvider=="GUARD_BL_YAML"){
190            if($scope.temp.policy.yamlparams.blackList.length==0){
191                    $scope.temp.policy.yamlparams.blackList = [];
192            }
193     }else if($scope.temp.policy.ruleProvider=="Rainy_Day"){
194         if($scope.temp.policy.rainyday.treatmentTableChoices == null || $scope.temp.policy.rainyday.treatmentTableChoices.length == 0){
195                 $scope.temp.policy.rainyday.treatmentTableChoices = [];
196         }
197     }
198     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
199     $scope.addNewChoice = function() {
200       var newItemNo = $scope.temp.policy.attributes.length+1;
201       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
202     };    
203     $scope.removeChoice = function() {
204       var lastItem = $scope.temp.policy.attributes.length-1;
205       $scope.temp.policy.attributes.splice(lastItem);
206     };
207     
208     $scope.settingsDatas = [{"settings" : $scope.temp.policy.settings}];
209     $scope.addNewSettingsChoice = function() {
210       var newItemNo = $scope.temp.policy.settings.length+1;
211       $scope.temp.policy.settings.push({'id':'choice'+newItemNo});
212     };    
213     $scope.removeSettingsChoice = function() {
214       var lastItem = $scope.temp.policy.settings.length-1;
215       $scope.temp.policy.settings.splice(lastItem);
216     };
217     
218     $scope.addNewBL = function() {
219         $scope.temp.policy.yamlparams.blackList.push('');
220     };
221     $scope.removeBL = function() {
222         var lastItem = $scope.temp.policy.yamlparams.blackList.length-1;
223         $scope.temp.policy.yamlparams.blackList.splice(lastItem);
224     };
225     
226     $scope.treatmentDatas = [{"treatmentValues" : $scope.temp.policy.rainyday.treatmentTableChoices}];
227     $scope.addNewTreatment = function() {
228         $scope.temp.policy.rainyday.treatmentTableChoices.push({});
229     };
230     $scope.removeTreatment = function() {
231         var lastItem = $scope.temp.policy.rainyday.treatmentTableChoices.length-1;
232         $scope.temp.policy.rainyday.treatmentTableChoices.splice(lastItem);
233     };
234     
235     $scope.ItemNo = 0;
236     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices }];
237     
238     $scope.addNewRuleAlgorithm = function() {
239         if($scope.temp.policy.ruleAlgorithmschoices != null){
240                 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
241         }else{
242                 var newItemNo = 1;
243         }
244         if(newItemNo > 1){
245                 var value = newItemNo-1;
246                 $scope.attributeDictionaryDatas.push('A'+value);
247         }
248         $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
249     };
250     
251     $scope.removeRuleAlgorithm = function() {
252       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
253       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
254     };
255     
256     $scope.providerListener = function(ruleProvider) {
257         if (ruleProvider!="Custom"){
258                 $scope.temp.policy.ruleAlgorithmschoices  = [];
259                 $scope.temp.policy.settings = [];
260                 $scope.temp.policy.attributes = [];
261         }
262     };
263 }]);