[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / AutoPushController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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 app.controller('policyPushController', function ($scope, PolicyAppService, modalService, $modal, Notification,$filter){
21     $( "#dialog" ).hide();
22
23    $scope.isDisabled = true;
24    $scope.loading = true;
25
26    PolicyAppService.getData('get_LockDownData').then(function(data){
27         var j = data;
28         $scope.data = JSON.parse(j.data);
29         $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
30         if($scope.lockdowndata[0].lockdown == true){
31             $scope.isDisabled = true;
32         }else{
33             $scope.isDisabled = false;
34         }
35         console.log($scope.data);
36     },function(error){
37         console.log("failed");
38     });
39
40     $scope.pdpdata;
41     PolicyAppService.getData('get_PDPGroupData').then(function (data) {
42         var j = data;
43         $scope.pdpdata = JSON.parse(j.data);
44         console.log($scope.pdpdata);
45         $scope.pushTabPDPGrid.data = $scope.pdpdata;
46     }, function (error) {
47         console.log("failed");
48     });
49
50     $scope.getPDPData = function(){
51          $scope.pushTabPDPGrid.data = $scope.pdpdata;
52     };
53     $scope.filterPdpGroup;
54     $scope.filterPDPGroupData = function() {
55         $scope.pushTabPDPGrid.data = $filter('filter')($scope.pdpdata, $scope.filterPdpGroup, undefined);
56     };
57     
58     $scope.pushTabPDPGrid = {   
59         onRegisterApi: function(gridApi) {
60             $scope.gridApi = gridApi;
61         },
62         enableFiltering: true,
63         columnDefs: [
64             { field: 'default',displayName : '', enableFiltering : false, enableSorting : false,
65                 cellTemplate: '<button  type="button"  ng-click="grid.appScope.editPDPGroupWindow(row.entity)"><a class="fa fa-pencil-square-o"></a></i></button> ',
66                 width: '5%'
67             },
68             { field: 'id', displayName : 'ID'},
69             { field: 'name', displayName : 'Name' },
70             { field: 'description' }
71         ]
72     };
73
74
75     $scope.editPDPGroupWindow = function (selectedPdpGroupData) {
76         $scope.removePDPGroupPolicies = selectedPdpGroupData;
77         if($scope.isDisabled){
78             Notification.error("Policy Application has been LockDown.");
79         }else{
80             var modalInstance = $modal.open({
81                 backdrop: 'static', keyboard: false,
82                 templateUrl: 'remove_PDPGroupPolicies_popup.html',
83                 controller: 'removeGroupPoliciesController',
84                 resolve: {
85                     message: function () {
86                         var message = {
87                             selectedPdpGroupData: $scope.removePDPGroupPolicies
88                         };
89                         return message;
90                     }
91                 }
92             });
93             modalInstance.result.then(function (response) {
94                 console.log('response', response);
95                 $scope.pdpdata = JSON.parse(response.data);
96                 $scope.pushTabPDPGrid.data =  $scope.pdpdata;
97             });
98         }
99     };
100
101     $scope.gridOptions = {
102                 data : 'policydatas',
103                  onRegisterApi: function(gridApi) {
104                     $scope.gridPolicyApi = gridApi;
105                 },
106                 enableSorting: true,
107                 enableFiltering: true,
108                 showTreeExpandNoChildren: true,
109                 paginationPageSizes: [10, 20, 50, 100],
110                 paginationPageSize: 20,
111                 columnDefs: [{name: 'policyName', displayName : 'Policy Name', sort: { direction: 'asc', priority: 0 }}, 
112                              {name: 'activeVersion', displayName : 'Version'}, 
113                              {name: 'modifiedDate', displayName : 'Last Modified',type: 'date', cellFilter: 'date:\'yyyy-MM-dd HH:MM:ss a\'' }]
114     };
115     
116    
117     PolicyAppService.getData('get_AutoPushPoliciesContainerData').then(function (data) {
118         $scope.loading = false;
119         var j = data;
120                 $scope.data = JSON.parse(j.data);
121                 console.log($scope.data);
122                 $scope.policydatas =JSON.parse($scope.data.policydatas);
123                 console.log($scope.policydatas);
124        }, function (error) {
125         console.log("failed");
126     });
127    
128     $scope.pushPoliciesButton = function(){
129         var policySelection = $scope.gridPolicyApi.selection.getSelectedRows();
130         console.log(policySelection);
131         var currentSelection = $scope.gridApi.selection.getSelectedRows();
132         if(policySelection.length == 0 && currentSelection.length == 0){
133                 Notification.error("Please Select Policy and PDP Group to Push");
134         }
135         if(policySelection.length == 0 && currentSelection.length != 0){
136                 Notification.error("Please Select Policy to Push");
137         }
138         if(policySelection.length != 0 && currentSelection.length == 0){
139                 Notification.error("Please Select PDP Group to Push");
140         }
141         if(policySelection.length != 0 && currentSelection.length != 0){
142                 var finalData = {
143                                 "pdpDatas": currentSelection,
144                                 "policyDatas": policySelection
145                 };
146                 console.log(finalData);
147                 var uuu = "auto_Push/PushPolicyToPDP.htm";
148                 var postData={pushTabData: finalData};
149                 $.ajax({
150                         type : 'POST',
151                         url : uuu,
152                         dataType: 'json',
153                         contentType: 'application/json',
154                         data: JSON.stringify(postData),
155                         success : function(data){
156                                 $scope.$apply(function(){
157                                         $scope.data=data.data;
158                                         $scope.pdpdata = JSON.parse(data.data);
159                                         $scope.pushTabPDPGrid.data =  $scope.pdpdata;
160                                         Notification.success("Policy Pushed Successfully");
161                                 });
162                                 console.log($scope.data);
163                         },
164                         error : function(data){
165                                 Notification.error("Error Occured while Pushing Policy.");
166                         }
167                 });
168
169         }
170     };
171   
172
173 });