Fixed the Policy API issues and Bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / ActionPolicyController.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 app.controller('actionPolicyController', ['$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     PolicyAppService.getData('getDictionary/get_ActionPolicyDictDataByName').then(function (data) {
40         var j = data;
41         $scope.data = JSON.parse(j.data);
42         console.log($scope.data);
43         $scope.actionPolicyDictionaryDatas = JSON.parse($scope.data.actionPolicyDictionaryDatas);
44         console.log($scope.actionPolicyDictionaryDatas);
45     }, function (error) {
46         console.log("failed");
47     });
48
49     PolicyAppService.getData('getDictionary/get_AttributeDatabyAttributeName').then(function (data) {
50         var j = data;
51         $scope.data = JSON.parse(j.data);
52         console.log($scope.data);
53         $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
54         console.log($scope.attributeDictionaryDatas);
55     }, function (error) {
56         console.log("failed");
57     });
58
59     PolicyAppService.getData('get_FunctionDefinitionDataByName').then(function (data) {
60         var j = data;
61         $scope.data = JSON.parse(j.data);
62         console.log($scope.data);
63         $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
64         console.log($scope.functionDefinitionDatas);
65     }, function (error) {
66         console.log("failed");
67     });
68
69     function extend(obj, src) {
70         for (var key in src) {
71             if (src.hasOwnProperty(key)) obj[key] = src[key];
72         }
73         return obj;
74     }
75     
76     $scope.saveActionPolicy = function(policy){
77         if(policy.itemContent != undefined){
78                 $scope.refreshCheck = true; 
79                 $scope.policyNavigator = policy.itemContent;
80                 policy.itemContent = "";
81         }
82         $scope.savebutton = false;
83         var uuu = "policycreation/save_policy";
84                 var postData={policyData: policy};
85                 $.ajax({
86                         type : 'POST',
87                         url : uuu,
88                         dataType: 'json',
89                         contentType: 'application/json',
90                         data: JSON.stringify(postData),
91                         success : function(data){
92                                 $scope.$apply(function(){
93                                         $scope.data=data.policyData;
94                                         if($scope.data == 'success'){
95                                                 $scope.temp.policy.readOnly = 'true';
96                                                 Notification.success("Policy Saved Successfully.");     
97                                         }else if ($scope.data == 'PolicyExists'){
98                                                 $scope.savebutton = true;
99                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
100                                         }
101                                 });
102                         },
103                         error : function(data){
104                                 Notification.error("Error Occured while saving Policy.");
105                                 $scope.savebutton = true;
106                         }
107                 });
108     };
109
110     $scope.validatePolicy = function(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                         },
137                         error : function(data){
138                                 Notification.error("Validation Failed.");
139                                 $scope.savebutton = true;
140                         }
141                 });
142     };
143
144     if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
145         $scope.temp.policy.attributes = [];
146         $scope.temp.policy.attributes.push({'id':'choice'+1});
147          $scope.temp.policy.ruleAlgorithmschoices = [];
148     }else{
149            if($scope.temp.policy.attributes.length == 0){
150                    $scope.temp.policy.attributes = [];
151            }
152            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
153                    $scope.temp.policy.ruleAlgorithmschoices = [];
154            }
155    }
156   
157     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
158     $scope.addNewChoice = function() {
159         var newItemNo = $scope.temp.policy.attributes .length+1;
160         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
161     };
162     $scope.removeChoice = function() {
163         var lastItem = $scope.temp.policy.attributes.length-1;
164         $scope.temp.policy.attributes.splice(lastItem);
165     };
166     
167    
168     $scope.ItemNo = 0;
169     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices}];
170     
171     $scope.addNewRuleAlgorithm = function(){
172       var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
173       $scope.ItemNo = newItemNo;
174         if(newItemNo > 1){
175             var value = newItemNo-1;
176             $scope.attributeDictionaryDatas.push('A'+value);
177         }
178       $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
179
180     };
181     
182     $scope.removeRuleAlgorithm = function() {
183       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
184       $scope.ItemNo = lastItem; 
185       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
186     };
187     
188 }]);