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