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