3e75e05a9a5d0fae122c235d8e527b23338d52f9
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSRawPolicyController.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('brmsRawPolicyController', ['$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     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
28         $scope.temp.policy = {
29                 policyType : "Config",
30                 configPolicyType : "BRMS_Raw"
31         }
32     }
33     
34     $scope.refresh = function(){
35         if($scope.refreshCheck){
36                 $scope.policyNavigator.refresh();
37         }
38         $scope.modal('createNewPolicy', true);
39         $scope.temp.policy = "";
40     };
41     
42     $scope.modal = function(id, hide) {
43         return $('#' + id).modal(hide ? 'hide' : 'show');
44     };
45     
46     $('#ttlDate').datepicker({
47         dateFormat: 'dd/mm/yy',
48         changeMonth: true,
49         changeYear: true,
50         onSelect: function(date) {
51                 angular.element($('#ttlDate')).triggerHandler('input');
52         }
53     });
54     
55     PolicyAppService.getData('getDictionary/get_BRMSControllerDataByName').then(function (data) {
56         var j = data;
57         $scope.data = JSON.parse(j.data);
58         console.log($scope.data);
59         $scope.brmsControllerDatas = JSON.parse($scope.data.brmsControllerDictionaryDatas);
60         console.log($scope.brmsControllerDatas);
61     }, function (error) {
62         console.log("failed");
63     });
64
65     PolicyAppService.getData('getDictionary/get_BRMSDependencyDataByName').then(function (data) {
66         var j = data;
67         $scope.data = JSON.parse(j.data);
68         console.log($scope.data);
69         $scope.brmsDependencyDatas = JSON.parse($scope.data.brmsDependencyDictionaryDatas);
70         console.log($scope.brmsDependencyDatas);
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.saveBrmsRawPolicy = function(policy){
86         if(policy.itemContent != undefined){
87                 $scope.refreshCheck = true; 
88                 $scope.policyNavigator = policy.itemContent;
89                 policy.itemContent = "";
90         }
91         $scope.savebutton = false;
92         var uuu = "policycreation/save_policy";
93                 var postData={policyData: policy};
94                 $.ajax({
95                         type : 'POST',
96                         url : uuu,
97                         dataType: 'json',
98                         contentType: 'application/json',
99                         data: JSON.stringify(postData),
100                         success : function(data){
101                                 $scope.$apply(function(){
102                                         $scope.data=data.policyData;
103                                         if($scope.data == 'success'){
104                                                 $scope.temp.policy.readOnly = 'true';
105                                                 Notification.success("Policy Saved Successfully.");     
106                                         }else if ($scope.data == 'PolicyExists'){
107                                                 $scope.savebutton = true;
108                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
109                                         }
110                                 });
111                                 console.log($scope.data);
112                         },
113                         error : function(data){
114                                 Notification.error("Error Occured while saving Policy.");
115                                 $scope.savebutton = true;
116                         }
117                 });
118     };
119     
120     $scope.validatePolicy = function(policy){
121         console.log(policy);
122         document.getElementById("validate").innerHTML = "";
123          var uuu = "policyController/validate_policy.htm";
124                 var postData={policyData: policy};
125                 $.ajax({
126                         type : 'POST',
127                         url : uuu,
128                         dataType: 'json',
129                         contentType: 'application/json',
130                         data: JSON.stringify(postData),
131                         success : function(data){
132                                 $scope.$apply(function(){
133                                         $scope.validateData = data.data.replace(/\"/g, "");
134                                                 $scope.data=data.data.substring(1,8);
135                                                 var size = data.data.length;
136                                                 if($scope.data == 'success'){
137                                                         Notification.success("Validation Success.");
138                                                         $scope.savebutton = false;
139                                                         if (size > 18){
140                                                                 var displayWarning = data.data.substring(19,size);
141                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
142                                                                 document.getElementById("validate").style.color = "white";
143                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
144                                                         }       
145                                                 }else{
146                                                         Notification.error("Validation Failed.");
147                                                         document.getElementById("validate").innerHTML = $scope.validateData;
148                                                         document.getElementById("validate").style.color = "white";
149                                                         document.getElementById("validate").style.backgroundColor = "red";
150                                                         $scope.savebutton = true;
151                                                 }
152                                                 
153                                 });
154                                 console.log($scope.data);
155                         },
156                         error : function(data){
157                                 Notification.error("Validation Failed.");
158                         }
159                 });
160     };
161     
162     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
163         $scope.temp.policy.attributes = [];
164     }else{
165            if($scope.temp.policy.attributes.length == 0){
166                    $scope.temp.policy.attributes = [];
167            }
168    }
169     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
170     $scope.addNewChoice = function() {
171       var newItemNo = $scope.temp.policy.attributes.length+1;
172       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
173     };    
174     $scope.removeChoice = function() {
175       var lastItem = $scope.temp.policy.attributes.length-1;
176       $scope.temp.policy.attributes.splice(lastItem);
177     };
178 }]);