393780705b2e9dd3725c24f904475885609c0020
[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', ['$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           };
33             
34           $scope.modal = function(id, hide) {
35               return $('#' + id).modal(hide ? 'hide' : 'show');
36           };
37          
38          PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
39                  var j = data;
40                  $scope.data = JSON.parse(j.data);
41                  console.log($scope.data);
42                  $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
43                  console.log($scope.ecompNameDictionaryDatas);
44          }, function (error) {
45                  console.log("failed");
46          });
47
48          PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
49                  var j = data;
50                  $scope.data = JSON.parse(j.data);
51                  console.log($scope.data);
52                  $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
53                  console.log($scope.riskTypeDictionaryDatas);
54          }, function (error) {
55                  console.log("failed");
56          });
57                         
58                     
59          PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
60                 var j = data;
61                 $scope.PMData = j;
62             });
63             
64             if($scope.temp.policy.readOnly && $scope.temp.policy.editPolicy){
65                 $scope.temp.policy.verticaMetrics = [];
66                     $scope.temp.policy.description = [];
67                     $scope.temp.policy.attributes = [];
68             }
69             
70             $scope.addDataToFields = function(serviceType){
71                 if($scope.PMData == undefined){
72                         $scope.temp.policy.verticaMetrics = [];
73                         $scope.temp.policy.description = [];
74                         $scope.temp.policy.attributes = [];
75                         PolicyAppService.getData('app/policyApp/JSONDataFiles/JSONConfig.json').then(function(data){
76                                 var j = data;
77                                 $scope.PMData = j;
78                                 if(serviceType.serviceTypePolicyName == 'Registration Failure(Trinity)'){
79                                         var myNewData = $scope.PMData[0];
80                                 }else if(serviceType.serviceTypePolicyName == 'International Fraud(Trinity)'){
81                                         var myNewData = $scope.PMData[1];
82                                 }else if(serviceType.serviceTypePolicyName == 'No dial tone(Trinity)'){
83                                         var myNewData = $scope.PMData[2];
84                                 }else if(serviceType.serviceTypePolicyName == 'Call storm(Trinity)'){
85                                         var myNewData = $scope.PMData[3];
86                                 }else if(serviceType.serviceTypePolicyName == 'Registration storm(Trinity)'){
87                                         var myNewData = $scope.PMData[4];
88                                 }
89                                 
90                                 $scope.temp.policy.verticaMetrics = myNewData;
91                                 $scope.temp.policy.description = myNewData;
92                                 $scope.temp.policy.attributeFields = myNewData;
93                         });
94                 }else{
95                                 $scope.temp.policy.verticaMetrics = serviceType;
96                                 $scope.temp.policy.description = serviceType;
97                                 $scope.temp.policy.attributeFields = serviceType;
98                 }
99                 
100             };
101             
102             $scope.saveCLPMPolicy = function(policy){
103                 if(policy.itemContent != undefined){
104                         $scope.refreshCheck = true; 
105                         $scope.policyNavigator = policy.itemContent;
106                         policy.itemContent = "";
107                 }
108                 $scope.savebutton = false;
109                 var uuu = "policycreation/save_policy";
110                         var postData={policyData: policy};
111                         $.ajax({
112                                 type : 'POST',
113                                 url : uuu,
114                                 dataType: 'json',
115                                 contentType: 'application/json',
116                                 data: JSON.stringify(postData),
117                                 success : function(data){
118                                         $scope.$apply(function(){
119                                                 $scope.data=data.policyData;
120                                                 if($scope.data == 'success'){
121                                                         $scope.temp.policy.readOnly = 'true';
122                                                         Notification.success("Policy Saved Successfully.");     
123                                                 }else if ($scope.data == 'PolicyExists'){
124                                                         $scope.savebutton = true;
125                                                         Notification.error("Policy Already Exists with Same Name in Scope.");
126                                                 }
127                                         });
128                                         console.log($scope.data);
129                                 },
130                                 error : function(data){
131                                         Notification.error("Error Occured while saving Policy.");
132                                 }
133                         });
134             };
135             
136             $scope.validatePolicy = function(policy){
137                 console.log(policy);
138                 document.getElementById("validate").innerHTML = "";
139                  var uuu = "policyController/validate_policy.htm";
140                         var postData={policyData: policy};
141                         $.ajax({
142                                 type : 'POST',
143                                 url : uuu,
144                                 dataType: 'json',
145                                 contentType: 'application/json',
146                                 data: JSON.stringify(postData),
147                                 success : function(data){
148                                         $scope.$apply(function(){
149                                                 $scope.validateData = data.data.replace(/\"/g, "");
150                                                         $scope.data=data.data.substring(1,8);
151                                                         var size = data.data.length;
152                                                         if($scope.data == 'success'){
153                                                                 Notification.success("Validation Success.");
154                                                                 $scope.savebutton = false;
155                                                                 if (size > 18){
156                                                                         var displayWarning = data.data.substring(19,size);
157                                                                         document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
158                                                                         document.getElementById("validate").style.color = "white";
159                                                                         document.getElementById("validate").style.backgroundColor = "skyblue";
160                                                                 }       
161                                                         }else{
162                                                                 Notification.error("Validation Failed.");
163                                                                 document.getElementById("validate").innerHTML = $scope.validateData;
164                                                                 document.getElementById("validate").style.color = "white";
165                                                                 document.getElementById("validate").style.backgroundColor = "red";
166                                                                 $scope.savebutton = true;
167                                                         }
168                                                         
169                                         });
170                                         console.log($scope.data);                               
171                                 },
172                                 error : function(data){
173                                         Notification.error("Validation Failed.");
174                                         $scope.savebutton = true;
175                                 }
176                         });
177             };
178           
179 }]);