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