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