Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BRMSParamPolicyController.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
21 /**
22  * 
23  */
24 angular.module('abs').controller('brmsParamPolicyController', function ($scope, $window, PolicyDictionaryService, PapUrlService, modalService, $modal, Notification) {
25     $("#dialog").hide();
26
27         $scope.temp.policy.ttlDate = new Date($scope.temp.policy.ttlDate);
28     $scope.validateSuccess = true;
29     var readValue = $scope.temp.policy.readOnly;
30     if(readValue){
31          $scope.validateSuccess = false;
32     }
33     
34         var papUrl;
35         PapUrlService.getPapUrl().then(function(data) {
36                 var config = data;
37                 papUrl = config.PAP_URL;
38                 console.log(papUrl);
39                 
40             PolicyDictionaryService.getBRMSParamDictionaryData(papUrl).then(function (data) {
41                 var j = data;
42                 $scope.data = JSON.parse(j.data);
43                 console.log($scope.data);
44                 $scope.brmsParamDictionaryDatas = JSON.parse($scope.data.brmsParamDictionaryDatas);
45                 console.log($scope.brmsParamDictionaryDatas);
46             }, function (error) {
47                 console.log("failed");
48             });
49             PolicyDictionaryService.getRiskTypeDictionaryData(papUrl).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             PolicyDictionaryService.getRiskLevelValueData().then(function (data) {
59                 var j = data;
60                 $scope.data = JSON.parse(j.data);
61                 console.log($scope.data);
62                 $scope.riskLevelDatas = JSON.parse($scope.data.riskLevelDatas);
63                 console.log($scope.riskLevelDatas);
64             }, function (error) {
65                 console.log("failed");
66             });
67             PolicyDictionaryService.getGuardValueData().then(function (data) {
68                 var j = data;
69                 $scope.data = JSON.parse(j.data);
70                 console.log($scope.data);
71                 $scope.guardDatas = JSON.parse($scope.data.guardDatas);
72                 console.log($scope.guardDatas);
73             }, function (error) {
74                 console.log("failed");
75             });
76         });
77     
78     $scope.temp.policy.dynamicLayoutMap = [];
79     $scope.addDataToFields = function(ruleName){
80         console.log(ruleName);   
81         if(ruleName != null){
82                  var uuu = "policyController/getBRMSTemplateData.htm";
83                  var postData={policyData: ruleName};
84              $.ajax({
85                  type : 'POST',
86                  url : uuu,
87                  dataType: 'json',
88                  contentType: 'application/json',
89                  data: JSON.stringify(postData),
90                  success : function(data){
91                      $scope.$apply(function(){
92                          $scope.temp.policy.dynamicLayoutMap = data.policyData;
93                      });
94                      console.log( $scope.temp.policy.dynamicLayoutMap);
95                  },
96                  error : function(data){
97                      alert("Error While Retriving the Template Layout Pattren.");
98                  }
99              });
100         }   
101     };
102     
103     $scope.ShowRule = function(policy){
104         console.log(policy);
105         var uuu = "policyController/ViewBRMSParamPolicyRule.htm";
106         var postData={policyData: policy};
107         $.ajax({
108             type : 'POST',
109             url : uuu,
110             dataType: 'json',
111             contentType: 'application/json',
112             data: JSON.stringify(postData),
113             success : function(data){
114                 $scope.$apply(function(){
115                         window.alert(data.policyData);
116                 });
117             },
118             error : function(data){
119                 Notification.error("Error Occured while Showing Rule.");
120             }
121         });
122     };
123     
124     $scope.saveBrmsParamPolicy = function(policy){
125         console.log(policy);
126         var uuu = "policyController/save_BRMSParamPolicy.htm";
127         var postData={policyData: policy};
128         $.ajax({
129             type : 'POST',
130             url : uuu,
131             dataType: 'json',
132             contentType: 'application/json',
133             data: JSON.stringify(postData),
134             success : function(data){
135                 $scope.$apply(function(){
136                     $scope.data=data.data;
137                         $scope.temp.policy.readOnly = 'true';
138                     Notification.success("Policy Saved Successfully.");
139                 });
140                 console.log($scope.data);
141                 $modalInstance.close();
142
143             },
144             error : function(data){
145                 Notification.error("Error Occured while saving Policy.");
146             }
147         });
148     };
149
150     $scope.validatePolicy = function(policy){
151         console.log(policy);
152          var uuu = "policyController/validate_policy.htm";
153                 var postData={policyData: policy};
154                 $.ajax({
155                         type : 'POST',
156                         url : uuu,
157                         dataType: 'json',
158                         contentType: 'application/json',
159                         data: JSON.stringify(postData),
160                         success : function(data){
161                                 $scope.$apply(function(){
162                                                 $scope.data=data.data.substring(1,8);
163                                                 var size = data.data.length;
164                                                 if($scope.data == 'success'){
165                                                         Notification.success("Validation Success.");
166                                                         if (size > 18){
167                                                                 var displayWarning = data.data.substring(19,size);
168                                                                 window.alert(displayWarning);
169                                                         }       
170                                                         $scope.validateSuccess = false;
171                                                 }else{
172                                                         Notification.error("Validation Failed.");
173                                                 }
174                                                 
175                                 });
176                                 console.log($scope.data);
177                                 /*$modalInstance.close();*/
178                                 
179                         },
180                         error : function(data){
181                                 Notification.error("Validation Failed.");
182                         }
183                 });
184     };
185     
186     function extend(obj, src) {
187         for (var key in src) {
188             if (src.hasOwnProperty(key)) obj[key] = src[key];
189         }
190         return obj;
191     }
192
193     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
194         $scope.temp.policy.attributes = [];
195     }else{
196            if($scope.temp.policy.attributes.length == 0){
197                    $scope.temp.policy.attributes = [];
198            }
199    }
200     
201     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
202     $scope.addNewChoice = function() {
203         var newItemNo = $scope.temp.policy.attributes.length+1;
204         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
205     };
206     $scope.removeChoice = function() {
207         var lastItem = $scope.temp.policy.attributes.length-1;
208         $scope.temp.policy.attributes.splice(lastItem);
209     };
210 });