d0c72682b9724eafcf70f85be96b69d2bbb6357a
[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     };
33     
34     $scope.modal = function(id, hide) {
35         return $('#' + id).modal(hide ? 'hide' : 'show');
36     };
37
38         if($scope.temp.policy.ruleProvider==undefined){
39                 $scope.temp.policy.ruleProvider="Custom";
40         }
41                 
42         PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
43                 var j = data;
44                 $scope.data = JSON.parse(j.data);
45                 console.log($scope.data);
46                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
47                 console.log($scope.ecompNameDictionaryDatas);
48         }, function (error) {
49                 console.log("failed");
50         });
51
52         PolicyAppService.getData('getDictionary/get_SettingsDictionaryDataByName').then(function (data) {
53                 var j = data;
54                 $scope.data = JSON.parse(j.data);
55                 console.log($scope.data);
56                 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
57                 console.log($scope.settingsDictionaryDatas);
58         }, function (error) {
59                 console.log("failed");
60         });
61
62         PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function (data) {
63                 var j = data;
64                 $scope.data = JSON.parse(j.data);
65                 console.log($scope.data);
66                 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
67                 console.log($scope.functionDefinitionDatas);
68         }, function (error) {
69                 console.log("failed");
70         });
71
72         PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function (data) {
73                 var j = data;
74                 $scope.data = JSON.parse(j.data);
75                 console.log($scope.data);
76                 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
77                 console.log($scope.attributeDictionaryDatas);
78         }, function (error) {
79                 console.log("failed");
80         });
81
82         
83
84     function extend(obj, src) {
85         for (var key in src) {
86             if (src.hasOwnProperty(key)) obj[key] = src[key];
87         }
88         return obj;
89     }
90     
91     $scope.saveDecisionPolicy = function(policy){
92         if(policy.itemContent != undefined){
93                 $scope.refreshCheck = true; 
94                 $scope.policyNavigator = policy.itemContent;
95                 policy.itemContent = "";
96         }
97         $scope.savebutton = false;
98         console.log(policy);
99         var uuu = "policycreation/save_policy";
100                 var postData={policyData: policy};
101                 $.ajax({
102                         type : 'POST',
103                         url : uuu,
104                         dataType: 'json',
105                         contentType: 'application/json',
106                         data: JSON.stringify(postData),
107                         success : function(data){
108                                 $scope.$apply(function(){
109                                         $scope.data=data.policyData;
110                                         if($scope.data == 'success'){
111                                                 $scope.temp.policy.readOnly = 'true';
112                                                 Notification.success("Policy Saved Successfully.");     
113                                         }else if ($scope.data == 'PolicyExists'){
114                                                 $scope.savebutton = true;
115                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
116                                         }       
117                                 });
118                                 console.log($scope.data);
119                                 
120                         },
121                         error : function(data){
122                                 Notification.error("Error Occured while saving Policy.");
123                         }
124                 });
125     };
126     
127     $scope.validatePolicy = function(policy){
128         console.log(policy);
129         document.getElementById("validate").innerHTML = "";
130          var uuu = "policyController/validate_policy.htm";
131                 var postData={policyData: policy};
132                 $.ajax({
133                         type : 'POST',
134                         url : uuu,
135                         dataType: 'json',
136                         contentType: 'application/json',
137                         data: JSON.stringify(postData),
138                         success : function(data){
139                                 $scope.$apply(function(){
140                                         $scope.validateData = data.data.replace(/\"/g, "");
141                                                 $scope.data=data.data;
142                                                 if($scope.data == '"success"'){
143                                                         Notification.success("Validation Success.");
144                                                         $scope.savebutton = false;
145                                                 }else{
146                                                         Notification.error("Validation Failed.");
147                                                         document.getElementById("validate").innerHTML = $scope.validateData;
148                                                         document.getElementById("validate").style.color = "white";
149                                                         document.getElementById("validate").style.backgroundColor = "red";
150                                                         $scope.savebutton = true;
151                                                 }
152                                                 
153                                 });
154                                 console.log($scope.data);
155                         },
156                         error : function(data){
157                                 Notification.error("Validation Failed.");
158                                 $scope.savebutton = true; 
159                         }
160                 });
161     };
162    
163     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
164         $scope.temp.policy.attributes = [];
165           $scope.temp.policy.settings = [];
166          $scope.temp.policy.ruleAlgorithmschoices = [];
167     }else if($scope.temp.policy.ruleProvider=="Custom"){
168            if($scope.temp.policy.attributes.length == 0){
169                    $scope.temp.policy.attributes = [];
170            }
171            if($scope.temp.policy.settings.length == 0){
172                    $scope.temp.policy.settings = [];
173            }
174            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
175                    $scope.temp.policy.ruleAlgorithmschoices = [];
176            }
177    }
178     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
179     $scope.addNewChoice = function() {
180       var newItemNo = $scope.temp.policy.attributes.length+1;
181       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
182     };    
183     $scope.removeChoice = function() {
184       var lastItem = $scope.temp.policy.attributes.length-1;
185       $scope.temp.policy.attributes.splice(lastItem);
186     };
187     
188   
189     $scope.settingsDatas = [{"settings" : $scope.temp.policy.settings}];
190     $scope.addNewSettingsChoice = function() {
191       var newItemNo = $scope.temp.policy.settings.length+1;
192       $scope.temp.policy.settings.push({'id':'choice'+newItemNo});
193     };    
194     $scope.removeSettingsChoice = function() {
195       var lastItem = $scope.temp.policy.settings.length-1;
196       $scope.temp.policy.settings.splice(lastItem);
197     };
198     
199     $scope.ItemNo = 0;
200     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices }];
201     
202     $scope.addNewRuleAlgorithm = function() {
203         if($scope.temp.policy.ruleAlgorithmschoices != null){
204                 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
205         }else{
206                 var newItemNo = 1;
207         }
208         if(newItemNo > 1){
209                 var value = newItemNo-1;
210                 $scope.attributeDictionaryDatas.push('A'+value);
211         }
212         $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
213     };
214     
215     $scope.removeRuleAlgorithm = function() {
216       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
217       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
218     };
219     
220     $scope.providerListener = function(ruleProvider) {
221         if (ruleProvider!="Custom"){
222                 $scope.temp.policy.ruleAlgorithmschoices  = [];
223                 $scope.temp.policy.settings = [];
224                 $scope.temp.policy.attributes = [];
225         }
226     };
227 }]);