Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSRawPolicyController.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('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     $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_BRMSControllerDataByName').then(function (data) {
40         var j = data;
41         $scope.data = JSON.parse(j.data);
42         console.log($scope.data);
43         $scope.brmsControllerDatas = JSON.parse($scope.data.brmsControllerDictionaryDatas);
44         console.log($scope.brmsControllerDatas);
45     }, function (error) {
46         console.log("failed");
47     });
48
49     PolicyAppService.getData('getDictionary/get_BRMSDependencyDataByName').then(function (data) {
50         var j = data;
51         $scope.data = JSON.parse(j.data);
52         console.log($scope.data);
53         $scope.brmsDependencyDatas = JSON.parse($scope.data.brmsDependencyDictionaryDatas);
54         console.log($scope.brmsDependencyDatas);
55     }, function (error) {
56         console.log("failed");
57     });
58
59     PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
60         var j = data;
61         $scope.data = JSON.parse(j.data);
62         console.log($scope.data);
63         $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
64         console.log($scope.riskTypeDictionaryDatas);
65     }, function (error) {
66         console.log("failed");
67     });
68
69     $scope.saveBrmsRawPolicy = function(policy){
70         if(policy.itemContent != undefined){
71                 $scope.refreshCheck = true; 
72                 $scope.policyNavigator = policy.itemContent;
73                 policy.itemContent = "";
74         }
75         $scope.savebutton = false;
76         var uuu = "policycreation/save_policy";
77                 var postData={policyData: policy};
78                 $.ajax({
79                         type : 'POST',
80                         url : uuu,
81                         dataType: 'json',
82                         contentType: 'application/json',
83                         data: JSON.stringify(postData),
84                         success : function(data){
85                                 $scope.$apply(function(){
86                                         $scope.data=data.policyData;
87                                         if($scope.data == 'success'){
88                                                 $scope.temp.policy.readOnly = 'true';
89                                                 Notification.success("Policy Saved Successfully.");     
90                                         }else if ($scope.data == 'PolicyExists'){
91                                                 $scope.savebutton = true;
92                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
93                                         }
94                                 });
95                                 console.log($scope.data);
96                         },
97                         error : function(data){
98                                 Notification.error("Error Occured while saving Policy.");
99                                 $scope.savebutton = true;
100                         }
101                 });
102     };
103     
104     $scope.validatePolicy = function(policy){
105         console.log(policy);
106         document.getElementById("validate").innerHTML = "";
107          var uuu = "policyController/validate_policy.htm";
108                 var postData={policyData: policy};
109                 $.ajax({
110                         type : 'POST',
111                         url : uuu,
112                         dataType: 'json',
113                         contentType: 'application/json',
114                         data: JSON.stringify(postData),
115                         success : function(data){
116                                 $scope.$apply(function(){
117                                         $scope.validateData = data.data.replace(/\"/g, "");
118                                                 $scope.data=data.data.substring(1,8);
119                                                 var size = data.data.length;
120                                                 if($scope.data == 'success'){
121                                                         Notification.success("Validation Success.");
122                                                         $scope.savebutton = false;
123                                                         if (size > 18){
124                                                                 var displayWarning = data.data.substring(19,size);
125                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
126                                                                 document.getElementById("validate").style.color = "white";
127                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
128                                                         }       
129                                                 }else{
130                                                         Notification.error("Validation Failed.");
131                                                         document.getElementById("validate").innerHTML = $scope.validateData;
132                                                         document.getElementById("validate").style.color = "white";
133                                                         document.getElementById("validate").style.backgroundColor = "red";
134                                                         $scope.savebutton = true;
135                                                 }
136                                                 
137                                 });
138                                 console.log($scope.data);
139                         },
140                         error : function(data){
141                                 Notification.error("Validation Failed.");
142                         }
143                 });
144     };
145     
146     function extend(obj, src) {
147         for (var key in src) {
148             if (src.hasOwnProperty(key)) obj[key] = src[key];
149         }
150         return obj;
151     }
152
153     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
154         $scope.temp.policy.attributes = [];
155     }else{
156            if($scope.temp.policy.attributes.length == 0){
157                    $scope.temp.policy.attributes = [];
158            }
159    }
160     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
161     $scope.addNewChoice = function() {
162       var newItemNo = $scope.temp.policy.attributes.length+1;
163       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
164     };    
165     $scope.removeChoice = function() {
166       var lastItem = $scope.temp.policy.attributes.length-1;
167       $scope.temp.policy.attributes.splice(lastItem);
168     };
169 }]);