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