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