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