Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / BaseConfigPolicyController.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 app.controller('baseConfigController', function ($scope, PolicyDictionaryService,modalService, PapUrlService, $modal, Notification) {
24     $("#dialog").hide();
25     
26     $scope.savebutton = true;
27         
28         $scope.temp.policy.ttlDate = new Date($scope.temp.policy.ttlDate);
29         PapUrlService.getPapUrl().then(function(data) {
30                 var papUrl;
31                 var config = data;
32                 papUrl = config.PAP_URL;
33                 console.log(papUrl);
34                 
35             PolicyDictionaryService.getEcompDictionaryData(papUrl).then(function (data) {
36                 var j = data;  
37                 $scope.data = JSON.parse(j.data);
38                 console.log($scope.data);
39                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
40                 console.log($scope.ecompNameDictionaryDatas);
41             }, function (error) {
42                 console.log("failed");
43             });
44
45             PolicyDictionaryService.getAttributeDictionaryData(papUrl).then(function (data) {
46                 var j = data;
47                 $scope.data = JSON.parse(j.data);
48                 console.log($scope.data);
49                 $scope.attributeDictionaryDatas = JSON.parse($scope.data.attributeDictionaryDatas);
50                 console.log($scope.attributeDictionaryDatas);
51             }, function (error) {
52                 console.log("failed");
53             });
54             
55             PolicyDictionaryService.getRiskTypeDictionaryData(papUrl).then(function (data) {
56                 var j = data;
57                 $scope.data = JSON.parse(j.data);
58                 console.log($scope.data);
59                 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
60                 console.log($scope.riskTypeDictionaryDatas);
61             }, function (error) {
62                 console.log("failed");
63             });
64             PolicyDictionaryService.getRiskLevelValueData().then(function (data) {
65                 var j = data;
66                 $scope.data = JSON.parse(j.data);
67                 console.log($scope.data);
68                 $scope.riskLevelDatas = JSON.parse($scope.data.riskLevelDatas);
69                 console.log($scope.riskLevelDatas);
70             }, function (error) {
71                 console.log("failed");
72             });
73             PolicyDictionaryService.getGuardValueData().then(function (data) {
74                 var j = data;
75                 $scope.data = JSON.parse(j.data);
76                 console.log($scope.data);
77                 $scope.guardDatas = JSON.parse($scope.data.guardDatas);
78                 console.log($scope.guardDatas);
79             }, function (error) {
80                 console.log("failed");
81             });
82             
83         });
84         
85     function extend(obj, src) {
86         for (var key in src) {
87             if (src.hasOwnProperty(key)) obj[key] = src[key];
88         }
89         return obj;
90     }
91     
92     $scope.close = function(){
93         $scope.temp = {};
94     };
95
96     $scope.savePolicy = function(policy){
97         console.log(policy);
98         var uuu = "policyController/save_policy.htm";
99                 var postData={policyData: policy};
100                 $.ajax({
101                         type : 'POST',
102                         url : uuu,
103                         dataType: 'json',
104                         contentType: 'application/json',
105                         data: JSON.stringify(postData),
106                         success : function(data){
107                                 $scope.$apply(function(){
108                                                 $scope.data=data.data;
109                                                 $scope.temp.policy.readOnly = 'true';
110                                                 Notification.success("Policy Saved Successfully.");
111                                 });
112                                 console.log($scope.data);
113                                 $modalInstance.close();
114                                 
115                         },
116                         error : function(data){
117                                 Notification.error("Error Occured while saving Policy.");
118                         }
119                 });
120     };
121     
122     $scope.validatePolicyName = false;
123     $scope.validateEcompName = false;
124     $scope.validateConfigName = false;
125     $scope.validateConfigType = false;
126     $scope.validateConfigTypeNull = false;
127     $scope.validateConfigTypeBody = false;
128     $scope.validateRiskType = false;
129     $scope.validateRiskLevel = false;
130     $scope.validateGuard = false;
131     $scope.policyNameErrorMessage = "";
132     $scope.errorMessage = "";
133     $scope.validatePolicy = function(policy){
134         console.log(policy);
135         var uuu = "policyController/validate_policy.htm";
136                 var postData={policyData: policy};
137                 $.ajax({
138                         type : 'POST',
139                         url : uuu,
140                         dataType: 'json',
141                         contentType: 'application/json',
142                         data: JSON.stringify(postData),
143                         success : function(data){
144                                 $scope.$apply(function(){       
145                                         $scope.data=data.data.substring(1,8);
146                                         var size = data.data.length;
147                                         if($scope.data == 'success'){
148                                                 Notification.success("Validation Success.");
149                                                 $scope.savebutton = false;
150                                                 if (size > 18){
151                                                         var displayWarning = data.data.substring(19,size);
152                                                         window.alert(displayWarning);
153                                                 }
154                                         }else{
155                                                 Notification.error("Validation Failed.");
156                                         }               
157                                 });                     
158                         },
159                         error : function(data){
160                                 Notification.error("Validation Failed.");
161                                 $scope.savebutton = true;
162                         }
163                 }); 
164     };
165     
166     if(!$scope.temp.policy.editPolicy  && !$scope.temp.policy.readOnly){
167         $scope.temp.policy.attributes = [];
168     }else{
169            if($scope.temp.policy.attributes.length == 0){
170                    $scope.temp.policy.attributes = [];
171            }
172    }
173     
174     
175     $scope.attributeDatas = [{"attributes" : $scope.temp.policy.attributes}];
176     $scope.addNewChoice = function() {
177         var newItemNo = $scope.temp.policy.attributes .length+1;
178         $scope.temp.policy.attributes.push({'id':'choice'+newItemNo});
179     };
180     $scope.removeChoice = function() {
181         var lastItem = $scope.temp.policy.attributes.length-1;
182         $scope.temp.policy.attributes.splice(lastItem);
183     };
184     
185 });