Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-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
21 /**
22  * 
23  */
24 angular.module('abs').controller('brmsRawPolicyController', function ($scope, $window, PolicyDictionaryService, PapUrlService, modalService, $modal, Notification) {
25     $("#dialog").hide();
26     
27         $scope.temp.policy.ttlDate = new Date($scope.temp.policy.ttlDate);
28         var papUrl;
29         PapUrlService.getPapUrl().then(function(data) {
30                 var config = data;
31                 papUrl = config.PAP_URL;
32                 console.log(papUrl);
33                 
34             PolicyDictionaryService.getRiskTypeDictionaryData(papUrl).then(function (data) {
35                 var j = data;
36                 $scope.data = JSON.parse(j.data);
37                 console.log($scope.data);
38                 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
39                 console.log($scope.riskTypeDictionaryDatas);
40             }, function (error) {
41                 console.log("failed");
42             });
43             PolicyDictionaryService.getRiskLevelValueData().then(function (data) {
44                 var j = data;
45                 $scope.data = JSON.parse(j.data);
46                 console.log($scope.data);
47                 $scope.riskLevelDatas = JSON.parse($scope.data.riskLevelDatas);
48                 console.log($scope.riskLevelDatas);
49             }, function (error) {
50                 console.log("failed");
51             });
52             PolicyDictionaryService.getGuardValueData().then(function (data) {
53                 var j = data;
54                 $scope.data = JSON.parse(j.data);
55                 console.log($scope.data);
56                 $scope.guardDatas = JSON.parse($scope.data.guardDatas);
57                 console.log($scope.guardDatas);
58             }, function (error) {
59                 console.log("failed");
60             });
61         });
62     $scope.saveBrmsRawPolicy = function(policy){
63         console.log(policy);
64        /* var finalData = extend(policy, $scope.attributeDatas[0]);*/
65         var uuu = "policyController/save_BRMSRawPolicy.htm";
66                 var postData={policyData: policy};
67                 $.ajax({
68                         type : 'POST',
69                         url : uuu,
70                         dataType: 'json',
71                         contentType: 'application/json',
72                         data: JSON.stringify(postData),
73                         success : function(data){
74                                 $scope.$apply(function(){
75                                                 $scope.data=data.data;
76                                                 $scope.temp.policy.readOnly = 'true';
77                                                 Notification.success("Policy Saved Successfully.");
78                                 });
79                                 console.log($scope.data);
80                                 $modalInstance.close();
81                                 
82                         },
83                         error : function(data){
84                                 Notification.error("Error Occured while saving Policy.");
85                         }
86                 });
87     };
88     
89     $scope.validatePolicy = function(policy){
90         console.log(policy);
91          var uuu = "policyController/validate_policy.htm";
92                 var postData={policyData: policy};
93                 $.ajax({
94                         type : 'POST',
95                         url : uuu,
96                         dataType: 'json',
97                         contentType: 'application/json',
98                         data: JSON.stringify(postData),
99                         success : function(data){
100                                 $scope.$apply(function(){
101                                                 $scope.data=data.data.substring(1,8);
102                                                 var size = data.data.length;
103                                                 if($scope.data == 'success'){
104                                                         Notification.success("Validation Success.");
105                                                         if (size > 18){
106                                                                 var displayWarning = data.data.substring(19,size);
107                                                                 window.alert(displayWarning);
108                                                         }       
109                                                 }else{
110                                                         Notification.error("Validation Failed.");
111                                                 }
112                                                 
113                                 });
114                                 console.log($scope.data);
115                                 /*$modalInstance.close();*/
116                                 
117                         },
118                         error : function(data){
119                                 Notification.error("Validation Failed.");
120                         }
121                 });
122     };
123     
124     function extend(obj, src) {
125         for (var key in src) {
126             if (src.hasOwnProperty(key)) obj[key] = src[key];
127         }
128         return obj;
129     }
130
131     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
132         $scope.temp.policy.attributes = [];
133     }else{
134            if($scope.temp.policy.attributes.length == 0){
135                    $scope.temp.policy.attributes = [];
136            }
137    }
138     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
139     $scope.addNewChoice = function() {
140       var newItemNo = $scope.temp.policy.attributes.length+1;
141       $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
142     };    
143     $scope.removeChoice = function() {
144       var lastItem = $scope.temp.policy.attributes.length-1;
145       $scope.temp.policy.attributes.splice(lastItem);
146     };
147 });