Commit includes ControlLoopPolicy API 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.ruleAlgorithmschoices = [];
147     }else{
148            if($scope.temp.policy.attributes.length == 0){
149                    $scope.temp.policy.attributes = [];
150            }
151            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
152                    $scope.temp.policy.ruleAlgorithmschoices = [];
153            }
154    }
155   
156     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
157     $scope.addNewChoice = function() {
158         var newItemNo = $scope.temp.policy.attributes .length+1;
159         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
160     };
161     $scope.removeChoice = function() {
162         var lastItem = $scope.temp.policy.attributes.length-1;
163         $scope.temp.policy.attributes.splice(lastItem);
164     };
165     
166    
167     $scope.ItemNo = 0;
168     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices}];
169     
170     $scope.addNewRuleAlgorithm = function(){
171       var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
172       $scope.ItemNo = newItemNo;
173         if(newItemNo > 1){
174             var value = newItemNo-1;
175             $scope.attributeDictionaryDatas.push('A'+value);
176         }
177       $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
178
179     };
180     
181     $scope.removeRuleAlgorithm = function() {
182       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
183       $scope.ItemNo = lastItem; 
184       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
185     };
186     
187 }]);