Resolved Policy GUI Issues
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / ClosedLoopPMController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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('clPMController', ['$scope', '$window', '$timeout', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function($scope, $window, $timeout, 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           $('#ttlDate').datepicker({
40                   dateFormat: 'dd/mm/yy',
41                   changeMonth: true,
42                   changeYear: true,
43                   onSelect: function(date) {
44                           angular.element($('#ttlDate')).triggerHandler('input');
45                   }
46           });
47          
48          PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function (data) {
49                  var j = data;
50                  $scope.data = JSON.parse(j.data);
51                  console.log($scope.data);
52                  $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
53                  console.log($scope.onapNameDictionaryDatas);
54          }, function (error) {
55                  console.log("failed");
56          });
57
58          PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
59                  var j = data;
60                  $scope.data = JSON.parse(j.data);
61                  console.log($scope.data);
62                  $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
63                  console.log($scope.riskTypeDictionaryDatas);
64          }, function (error) {
65                  console.log("failed");
66          });
67                         
68                     
69          PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
70                 var j = data;
71                 $scope.PMData = j;
72             });
73             
74             if($scope.temp.policy.readOnly && $scope.temp.policy.editPolicy){
75                 $scope.temp.policy.verticaMetrics = [];
76                     $scope.temp.policy.description = [];
77                     $scope.temp.policy.attributes = [];
78             }
79             
80             $scope.addDataToFields = function(serviceType){
81                 if($scope.PMData == undefined){
82                         $scope.temp.policy.verticaMetrics = [];
83                         $scope.temp.policy.description = [];
84                         $scope.temp.policy.attributes = [];
85                         PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
86                                 var j = data;
87                                 $scope.PMData = j;
88                                 if(serviceType.serviceTypePolicyName == 'Registration Failure(Trinity)'){
89                                         var myNewData = $scope.PMData[0];
90                                 }else if(serviceType.serviceTypePolicyName == 'International Fraud(Trinity)'){
91                                         var myNewData = $scope.PMData[1];
92                                 }else if(serviceType.serviceTypePolicyName == 'No dial tone(Trinity)'){
93                                         var myNewData = $scope.PMData[2];
94                                 }else if(serviceType.serviceTypePolicyName == 'Call storm(Trinity)'){
95                                         var myNewData = $scope.PMData[3];
96                                 }else if(serviceType.serviceTypePolicyName == 'Registration storm(Trinity)'){
97                                         var myNewData = $scope.PMData[4];
98                                 }
99                                 
100                                 $scope.temp.policy.verticaMetrics = myNewData;
101                                 $scope.temp.policy.description = myNewData;
102                                 $scope.temp.policy.attributeFields = myNewData;
103                         });
104                 }else{
105                                 $scope.temp.policy.verticaMetrics = serviceType;
106                                 $scope.temp.policy.description = serviceType;
107                                 $scope.temp.policy.attributeFields = serviceType;
108                 }
109                 
110             };
111             
112             $scope.saveCLPMPolicy = function(policy){
113                 if(policy.itemContent != undefined){
114                         $scope.refreshCheck = true; 
115                         $scope.policyNavigator = policy.itemContent;
116                         policy.itemContent = "";
117                 }
118                 $scope.savebutton = false;
119                 var uuu = "policycreation/save_policy";
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.policyData;
130                                                 if($scope.data == 'success'){
131                                                         $scope.temp.policy.readOnly = 'true';
132                                                         Notification.success("Policy Saved Successfully.");     
133                                                 }else if ($scope.data == 'PolicyExists'){
134                                                         $scope.savebutton = true;
135                                                         Notification.error("Policy Already Exists with Same Name in Scope.");
136                                                 }
137                                         });
138                                         console.log($scope.data);
139                                 },
140                                 error : function(data){
141                                         Notification.error("Error Occured while saving Policy.");
142                                 }
143                         });
144             };
145             
146             $scope.validatePolicy = function(policy){
147                 console.log(policy);
148                 document.getElementById("validate").innerHTML = "";
149                  var uuu = "policyController/validate_policy.htm";
150                         var postData={policyData: policy};
151                         $.ajax({
152                                 type : 'POST',
153                                 url : uuu,
154                                 dataType: 'json',
155                                 contentType: 'application/json',
156                                 data: JSON.stringify(postData),
157                                 success : function(data){
158                                         $scope.$apply(function(){
159                                                 $scope.validateData = data.data.replace(/\"/g, "");
160                                                         $scope.data=data.data.substring(1,8);
161                                                         var size = data.data.length;
162                                                         if($scope.data == 'success'){
163                                                                 Notification.success("Validation Success.");
164                                                                 $scope.savebutton = false;
165                                                                 if (size > 18){
166                                                                         var displayWarning = data.data.substring(19,size);
167                                                                         document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
168                                                                         document.getElementById("validate").style.color = "white";
169                                                                         document.getElementById("validate").style.backgroundColor = "skyblue";
170                                                                 }       
171                                                         }else{
172                                                                 Notification.error("Validation Failed.");
173                                                                 document.getElementById("validate").innerHTML = $scope.validateData;
174                                                                 document.getElementById("validate").style.color = "white";
175                                                                 document.getElementById("validate").style.backgroundColor = "red";
176                                                                 $scope.savebutton = true;
177                                                         }
178                                                         
179                                         });
180                                         console.log($scope.data);                               
181                                 },
182                                 error : function(data){
183                                         Notification.error("Validation Failed.");
184                                         $scope.savebutton = true;
185                                 }
186                         });
187             };
188           
189 }]);