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