Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSParamPolicyController.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('brmsParamPolicyController', ['$scope', '$window', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, $window, 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     $scope.validateSuccess = true;
39     var readValue = $scope.temp.policy.readOnly;
40     if(readValue){
41          $scope.validateSuccess = false;
42     }
43     
44     PolicyAppService.getData('getDictionary/get_BRMSControllerDataByName').then(function (data) {
45         var j = data;
46         $scope.data = JSON.parse(j.data);
47         console.log($scope.data);
48         $scope.brmsControllerDatas = JSON.parse($scope.data.brmsControllerDictionaryDatas);
49         console.log($scope.brmsControllerDatas);
50     }, function (error) {
51         console.log("failed");
52     });
53
54     PolicyAppService.getData('getDictionary/get_BRMSDependencyDataByName').then(function (data) {
55         var j = data;
56         $scope.data = JSON.parse(j.data);
57         console.log($scope.data);
58         $scope.brmsDependencyDatas = JSON.parse($scope.data.brmsDependencyDictionaryDatas);
59         console.log($scope.brmsDependencyDatas);
60     }, function (error) {
61         console.log("failed");
62     });
63
64     PolicyAppService.getData('getDictionary/get_BRMSParamDataByName').then(function (data) {
65         var j = data;
66         $scope.data = JSON.parse(j.data);
67         console.log($scope.data);
68         $scope.brmsParamDictionaryDatas = JSON.parse($scope.data.brmsParamDictionaryDatas);
69         console.log($scope.brmsParamDictionaryDatas);
70     }, function (error) {
71         console.log("failed");
72     });
73
74     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
75         var j = data;
76         $scope.data = JSON.parse(j.data);
77         console.log($scope.data);
78         $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
79         console.log($scope.riskTypeDictionaryDatas);
80     }, function (error) {
81         console.log("failed");
82     });
83     
84     $scope.temp.policy.dynamicLayoutMap = {};
85     $scope.addDataToFields = function(ruleName){
86         console.log(ruleName);   
87         if(ruleName != null){
88                  var uuu = "policyController/getBRMSTemplateData.htm";
89                  var postData={policyData: ruleName};
90              $.ajax({
91                  type : 'POST',
92                  url : uuu,
93                  dataType: 'json',
94                  contentType: 'application/json',
95                  data: JSON.stringify(postData),
96                  success : function(data){
97                      $scope.$apply(function(){
98                          $scope.temp.policy.dynamicLayoutMap = data.policyData;
99                      });
100                      console.log( $scope.temp.policy.dynamicLayoutMap);
101                  },
102                  error : function(data){
103                      alert("Error While Retriving the Template Layout Pattren.");
104                  }
105              });
106         }   
107     };
108     
109     $scope.ShowRule = function(policy){
110         console.log(policy);
111         var uuu = "policyController/ViewBRMSParamPolicyRule.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.datarule = data.policyData;
122                         var modalInstance = $modal.open({
123                         backdrop: 'static', keyboard: false,
124                         templateUrl : 'app/policyApp/policy-models/Editor/PolicyTemplates/BRMSShowParamRuleModal.html',
125                         controller: 'showrulecontroller',
126                         resolve: {
127                             message: function () {
128                                 var message = {
129                                     datas: $scope.datarule
130                                 };
131                                 return message;
132                             }
133                         }
134                     });
135                 });
136             },
137             error : function(data){
138                 Notification.error("Error Occured while Showing Rule.");
139             }
140         });
141     };
142     
143     $scope.saveBrmsParamPolicy = function(policy){
144         if(policy.itemContent != undefined){
145                 $scope.refreshCheck = true; 
146                 $scope.policyNavigator = policy.itemContent;
147                 policy.itemContent = "";
148         }
149         $scope.savebutton = false;
150         var uuu = "policycreation/save_policy";
151         var postData={policyData: policy};
152         $.ajax({
153             type : 'POST',
154             url : uuu,
155             dataType: 'json',
156             contentType: 'application/json',
157             data: JSON.stringify(postData),
158             success : function(data){
159                 $scope.$apply(function(){
160                     $scope.data=data.policyData;
161                     if($scope.data == 'success'){
162                         $scope.temp.policy.readOnly = 'true';
163                         $scope.safetyChecker = data.policyData.split("#")[2];
164                         if ($scope.safetyChecker!=undefined) {
165                                 Notification.success($scope.safetyChecker);
166                         }
167                         Notification.success("Policy Saved Successfully.");     
168                     }else if ($scope.data == 'PolicyExists'){
169                                                 $scope.savebutton = true;
170                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
171                                         }
172                 });
173                 console.log($scope.data);
174             },
175             error : function(data){
176                 Notification.error("Error Occured while saving Policy.");
177                 $scope.savebutton = true;
178             }
179         });
180     };
181
182     $scope.validatePolicy = function(policy){
183         console.log(policy);
184         document.getElementById("validate").innerHTML = "";
185         var uuu = "policyController/validate_policy.htm";
186                 var postData={policyData: policy};
187                 $.ajax({
188                         type : 'POST',
189                         url : uuu,
190                         dataType: 'json',
191                         contentType: 'application/json',
192                         data: JSON.stringify(postData),
193                         success : function(data){
194                                 $scope.$apply(function(){
195                                         $scope.validateData = data.data.replace(/\"/g, "");
196                                                 $scope.data=data.data.substring(1,8);
197                                                 var size = data.data.length;
198                                                 if($scope.data == 'success'){
199                                                         Notification.success("Validation Success.");
200                                                         $scope.savebutton = false;
201                                                         if (size > 18){
202                                                                 var displayWarning = data.data.substring(19,size);
203                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
204                                                                 document.getElementById("validate").style.color = "white";
205                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
206                                                         }       
207                                                         $scope.validateSuccess = false;
208                                                 }else{
209                                                         Notification.error("Validation Failed.");
210                                                         document.getElementById("validate").innerHTML = $scope.validateData;
211                                                         document.getElementById("validate").style.color = "white";
212                                                         document.getElementById("validate").style.backgroundColor = "red";
213                                                         $scope.savebutton = true;
214                                                 }
215                                                 
216                                 });
217                                 console.log($scope.data);
218                         },
219                         error : function(data){
220                                 Notification.error("Validation Failed.");
221                                 $scope.savebutton = true;
222                         }
223                 });
224     };
225     
226     function extend(obj, src) {
227         for (var key in src) {
228             if (src.hasOwnProperty(key)) obj[key] = src[key];
229         }
230         return obj;
231     }
232
233     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
234         $scope.temp.policy.attributes = [];
235     }else{
236            if($scope.temp.policy.attributes.length == 0){
237                    $scope.temp.policy.attributes = [];
238            }
239    }
240     
241     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
242     $scope.addNewChoice = function() {
243         var newItemNo = $scope.temp.policy.attributes.length+1;
244         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
245     };
246     $scope.removeChoice = function() {
247         var lastItem = $scope.temp.policy.attributes.length-1;
248         $scope.temp.policy.attributes.splice(lastItem);
249     };
250 }]);
251
252 app.controller('showrulecontroller' ,  function ($scope, $modalInstance, message){
253         if(message.datas!=null){
254                 $scope.datarule=message.datas;
255         }
256         
257         $scope.close = function() {
258         $modalInstance.close();
259     };
260
261 });