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