Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / DecisionPolicyController.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
25 angular.module('abs').controller('decisionPolicyController', function ($scope, PapUrlService, PolicyDictionaryService,modalService, $modal, Notification) {
26     $("#dialog").hide();
27         var papUrl;
28         if($scope.temp.policy.ruleProvider==undefined){
29                 $scope.temp.policy.ruleProvider="Custom";
30         }
31         PapUrlService.getPapUrl().then(function(data) {
32                 var config = data;
33                 papUrl = config.PAP_URL;
34                 console.log(papUrl);
35                 
36             PolicyDictionaryService.getEcompDictionaryData(papUrl).then(function (data) {
37                 var j = data;
38                 $scope.data = JSON.parse(j.data);
39                 console.log($scope.data);
40                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
41                 console.log($scope.ecompNameDictionaryDatas);
42             }, function (error) {
43                 console.log("failed");
44             });
45             
46             PolicyDictionaryService.getDecisionSettingsDictionaryData(papUrl).then(function (data) {
47                 var j = data;
48                 $scope.data = JSON.parse(j.data);
49                 console.log($scope.data);
50                 $scope.settingsDictionaryDatas = JSON.parse($scope.data.settingsDictionaryDatas);
51                 console.log($scope.settingsDictionaryDatas);
52             }, function (error) {
53                 console.log("failed");
54             });
55             
56             PolicyDictionaryService.getFunctionDefinitionData().then(function (data) {
57                 var j = data;
58                 $scope.data = JSON.parse(j.data);
59                 console.log($scope.data);
60                 $scope.functionDefinitionDatas = JSON.parse($scope.data.functionDefinitionDatas);
61                 console.log($scope.functionDefinitionDatas);
62             }, function (error) {
63                 console.log("failed");
64             });
65
66             PolicyDictionaryService.getAttributeDictionaryData(papUrl).then(function (data) {
67                 var j = data;
68                 $scope.data = JSON.parse(j.data);
69                 console.log($scope.data);
70                 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
71                 console.log($scope.attributeDictionaryDatas);
72             }, function (error) {
73                 console.log("failed");
74             });
75
76                 
77         });
78         
79
80     function extend(obj, src) {
81         for (var key in src) {
82             if (src.hasOwnProperty(key)) obj[key] = src[key];
83         }
84         return obj;
85     }
86     
87     $scope.saveDecisionPolicy = function(policy){
88         console.log(policy);
89         var uuu = "policyController/save_DecisionPolicy.htm";
90                 var postData={policyData: policy};
91                 $.ajax({
92                         type : 'POST',
93                         url : uuu,
94                         dataType: 'json',
95                         contentType: 'application/json',
96                         data: JSON.stringify(postData),
97                         success : function(data){
98                                 $scope.$apply(function(){
99                                 $scope.data=data.policyData;
100                                 if($scope.data.success != undefined){
101                                         $scope.temp.policy.readOnly = 'true';
102                                         Notification.success("Policy Saved Successfully.");
103                                 }else{
104                                         Notification.error("Error Occured while saving Policy.");
105                                 }               
106                         });
107                                 console.log($scope.data);
108                                 $modalInstance.close();
109                                 
110                         },
111                         error : function(data){
112                                 Notification.error("Error Occured while saving Policy.");
113                         }
114                 });
115     };
116     
117     $scope.validatePolicy = function(policy){
118         console.log(policy);
119          var uuu = "policyController/validate_policy.htm";
120                 var postData={policyData: policy};
121                 $.ajax({
122                         type : 'POST',
123                         url : uuu,
124                         dataType: 'json',
125                         contentType: 'application/json',
126                         data: JSON.stringify(postData),
127                         success : function(data){
128                                 $scope.$apply(function(){
129                                                 $scope.data=data.data;
130                                                 if($scope.data == '"success"'){
131                                                         Notification.success("Validation Success.");
132                                                 }else{
133                                                         Notification.error("Validation Failed.");
134                                                 }
135                                                 
136                                 });
137                                 console.log($scope.data);
138                                 /*$modalInstance.close();*/
139                                 
140                         },
141                         error : function(data){
142                                 Notification.error("Validation Failed.");
143                         }
144                 });
145     };
146    
147     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
148         $scope.temp.policy.attributes = [];
149           $scope.temp.policy.settings = [];
150          $scope.temp.policy.ruleAlgorithmschoices = [];
151     }else{
152            if($scope.temp.policy.attributes.length == 0){
153                    $scope.temp.policy.attributes = [];
154            }
155            if($scope.temp.policy.settings.length == 0){
156                    $scope.temp.policy.settings = [];
157            }
158            if($scope.temp.policy.ruleAlgorithmschoices.length == 0){
159                    $scope.temp.policy.ruleAlgorithmschoices = [];
160            }
161    }
162     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
163     $scope.addNewChoice = function() {
164       var newItemNo = $scope.temp.policy.attributes.length+1;
165       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
166     };    
167     $scope.removeChoice = function() {
168       var lastItem = $scope.temp.policy.attributes.length-1;
169       $scope.temp.policy.attributes.splice(lastItem);
170     };
171     
172   
173     $scope.settingsDatas = [{"settings" : $scope.temp.policy.settings}];
174     $scope.addNewSettingsChoice = function() {
175       var newItemNo = $scope.temp.policy.settings.length+1;
176       $scope.temp.policy.settings.push({'id':'choice'+newItemNo});
177     };    
178     $scope.removeSettingsChoice = function() {
179       var lastItem = $scope.temp.policy.settings.length-1;
180       $scope.temp.policy.settings.splice(lastItem);
181     };
182     
183     $scope.ItemNo = 0;
184     $scope.ruleAlgorithmDatas = [{"ruleAlgorithms" : $scope.temp.policy.ruleAlgorithmschoices }];
185     
186     $scope.addNewRuleAlgorithm = function() {
187         if($scope.temp.policy.ruleAlgorithmschoices != null){
188                 var newItemNo = $scope.temp.policy.ruleAlgorithmschoices.length+1;
189         }else{
190                 var newItemNo = 1;
191         }
192         if(newItemNo > 1){
193                 var value = newItemNo-1;
194                 $scope.attributeDictionaryDatas.push('A'+value);
195                 $scope.$apply();
196         }
197         $scope.temp.policy.ruleAlgorithmschoices.push({'id':'A'+newItemNo});
198     };
199     
200     $scope.removeRuleAlgorithm = function() {
201       var lastItem = $scope.temp.policy.ruleAlgorithmschoices.length-1;
202       $scope.temp.policy.ruleAlgorithmschoices.splice(lastItem);
203     };
204     
205     $scope.providerListener = function(ruleProvider) {
206         if (ruleProvider!="Custom"){
207                 $scope.temp.policy.ruleAlgorithmschoices  = [];
208                 $scope.temp.policy.settings = [];
209                 $scope.temp.policy.attributes = [];
210         }
211     };
212 });