Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / ClosedLoopPMController.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('clPMController', function($scope, $window, $timeout, PolicyAppService, modalService, $modal, Notification){
21          $("#dialog").hide();
22          
23          $scope.savebutton = true;
24          $scope.finalPath = null;
25          
26          PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
27                  var j = data;
28                  $scope.data = JSON.parse(j.data);
29                  console.log($scope.data);
30                  $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
31                  console.log($scope.ecompNameDictionaryDatas);
32          }, function (error) {
33                  console.log("failed");
34          });
35
36          PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
37                  var j = data;
38                  $scope.data = JSON.parse(j.data);
39                  console.log($scope.data);
40                  $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
41                  console.log($scope.riskTypeDictionaryDatas);
42          }, function (error) {
43                  console.log("failed");
44          });
45                         
46                     
47          PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
48                 var j = data;
49                 $scope.PMData = j;
50             });
51             
52             if($scope.temp.policy.readOnly && $scope.temp.policy.editPolicy){
53                 $scope.temp.policy.verticaMetrics = [];
54                     $scope.temp.policy.description = [];
55                     $scope.temp.policy.attributes = [];
56             }
57             
58             $scope.addDataToFields = function(serviceType){
59                 if($scope.PMData == undefined){
60                         $scope.temp.policy.verticaMetrics = [];
61                         $scope.temp.policy.description = [];
62                         $scope.temp.policy.attributes = [];
63                         PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
64                                 var j = data;
65                                 $scope.PMData = j;
66                                 if(serviceType.serviceTypePolicyName == 'Registration Failure(Trinity)'){
67                                         var myNewData = $scope.PMData[0];
68                                 }else if(serviceType.serviceTypePolicyName == 'International Fraud(Trinity)'){
69                                         var myNewData = $scope.PMData[1];
70                                 }else if(serviceType.serviceTypePolicyName == 'No dial tone(Trinity)'){
71                                         var myNewData = $scope.PMData[2];
72                                 }else if(serviceType.serviceTypePolicyName == 'Call storm(Trinity)'){
73                                         var myNewData = $scope.PMData[3];
74                                 }else if(serviceType.serviceTypePolicyName == 'Registration storm(Trinity)'){
75                                         var myNewData = $scope.PMData[4];
76                                 }
77                                 
78                                 $scope.temp.policy.verticaMetrics = myNewData;
79                                 $scope.temp.policy.description = myNewData;
80                                 $scope.temp.policy.attributeFields = myNewData;
81                         });
82                 }else{
83                                 $scope.temp.policy.verticaMetrics = serviceType;
84                                 $scope.temp.policy.description = serviceType;
85                                 $scope.temp.policy.attributeFields = serviceType;
86                 }
87                 
88             };
89             
90             $scope.saveCLPMPolicy = function(policy){
91                 console.log(policy);
92                 $scope.savebutton = false;
93                 var uuu = "policycreation/save_policy";
94                         var postData={policyData: policy};
95                         $.ajax({
96                                 type : 'POST',
97                                 url : uuu,
98                                 dataType: 'json',
99                                 contentType: 'application/json',
100                                 data: JSON.stringify(postData),
101                                 success : function(data){
102                                         $scope.$apply(function(){
103                                                 $scope.data=data.policyData;
104                                                 if($scope.data == 'success'){
105                                                         $scope.temp.policy.readOnly = 'true';
106                                                         Notification.success("Policy Saved Successfully.");     
107                                                 }else if ($scope.data == 'PolicyExists'){
108                                                         $scope.savebutton = true;
109                                                         Notification.error("Policy Already Exists with Same Name in Scope.");
110                                                 }
111                                         });
112                                         console.log($scope.data);
113                                 },
114                                 error : function(data){
115                                         Notification.error("Error Occured while saving Policy.");
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                                         $scope.savebutton = true;
159                                 }
160                         });
161             };
162           
163 })