Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / controller / AutoPushController.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 app.controller('policyPushController', function ($scope, PDPService, AdminTabService, AutoPushService, modalService, $modal, Notification,$filter){
22     $( "#dialog" ).hide();
23
24    $scope.isDisabled = true;
25    $scope.loading = true;
26
27     AdminTabService.getData().then(function(data){
28         var j = data;
29         $scope.data = JSON.parse(j.data);
30         $scope.lockdowndata = JSON.parse($scope.data.lockdowndata);
31         if($scope.lockdowndata[0].lockdown == true){
32             $scope.isDisabled = true;
33         }else{
34             $scope.isDisabled = false;
35         }
36         console.log($scope.data);
37     },function(error){
38         console.log("failed");
39     });
40
41     $scope.pdpdata;
42     PDPService.getPDPData().then(function (data) {
43         var j = data;
44         $scope.pdpdata = JSON.parse(j.data);
45         console.log($scope.pdpdata);
46         $scope.pushTabPDPGrid.data = $scope.pdpdata;
47     }, function (error) {
48         console.log("failed");
49     });
50
51     $scope.getPDPData = function(){
52          $scope.pushTabPDPGrid.data = $scope.pdpdata;
53     };
54     $scope.filterPdpGroup;
55     $scope.filterPDPGroupData = function() {
56         $scope.pushTabPDPGrid.data = $filter('filter')($scope.pdpdata, $scope.filterPdpGroup, undefined);
57     };
58     
59     $scope.pushTabPDPGrid = {   
60         onRegisterApi: function(gridApi) {
61             $scope.gridApi = gridApi;
62         },
63         enableFiltering: true,
64         columnDefs: [
65             { field: 'default',displayName : '', enableFiltering : false, enableSorting : false,
66                 cellTemplate: '<button  type="button"  class="btn btn-primary"  ng-click="grid.appScope.editPDPGroupWindow(row.entity)"><i class="fa fa-pencil-square-o"></i></button> ',
67                 width: '8%'
68             },
69             { field: 'id', displayName : 'ID'},
70             { field: 'name', displayName : 'Name' },
71             { field: 'description' }
72         ]
73     };
74
75
76     $scope.editPDPGroupWindow = function (selectedPdpGroupData) {
77         $scope.removePDPGroupPolicies = selectedPdpGroupData;
78         if($scope.isDisabled){
79             Notification.error("Policy Application has been LockDown.");
80         }else{
81             var modalInstance = $modal.open({
82                 backdrop: 'static', keyboard: false,
83                 templateUrl: 'remove_PDPGroupPolicies_popup.html',
84                 controller: 'removeGroupPoliciesController',
85                 resolve: {
86                     message: function () {
87                         var message = {
88                             selectedPdpGroupData: $scope.removePDPGroupPolicies
89                         };
90                         return message;
91                     }
92                 }
93             });
94             modalInstance.result.then(function (response) {
95                 console.log('response', response);
96                 $scope.pdpdata = JSON.parse(response.data);
97                 $scope.pushTabPDPGrid.data =  $scope.pdpdata;
98             });
99         }
100     };
101
102     $scope.gridOptions = {
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: 'name'}, {name: 'version'}, {name: 'dateModified'}]
112     };
113     
114     $scope.files;
115     var data = [];
116     $scope.filterPolicy;
117     $scope.filterPolicyData = function() {
118         $scope.gridOptions.data = $filter('filter')($scope.files, $scope.filterPolicy, undefined);
119     };
120    
121     AutoPushService.getAutoPushPoliciesData().then(function (data1) {
122         $scope.loading = false;
123         $scope.files =  data1.data;
124         var data = data1.data;
125          
126         var id=0;
127         var writeoutNode = function(childArray, currentLevel, dataArray){
128           childArray.forEach( function(childNode){
129           if (childNode.files.length > 0){
130               childNode.$$treeLevel = currentLevel;
131               id=childNode.categoryId;
132              if(childNode.categoryId == childNode.parentCategoryId){
133                 childNode.parent='';
134               }
135            }else{
136            if((id!=childNode.parentCategoryId) || (childNode.categoryId == childNode.parentCategoryId)){
137               if(childNode.categoryId == childNode.parentCategoryId){
138                 childNode.parent='';
139               }
140               childNode.$$treeLevel = currentLevel;
141             }
142           }
143             dataArray.push( childNode );
144             writeoutNode( childNode.files, currentLevel + 1, dataArray );
145           });
146         };
147
148         $scope.gridOptions.data = [];
149         writeoutNode(data, 0, $scope.gridOptions.data);
150        }, function (error) {
151         console.log("failed");
152     });
153     
154     
155     
156     $scope.pushPoliciesButton = function(){
157         var policySelection = $scope.gridPolicyApi.selection.getSelectedRows();
158         console.log(policySelection);
159         var currentSelection = $scope.gridApi.selection.getSelectedRows();
160         if(policySelection.length == 0 && currentSelection.length == 0){
161                 Notification.error("Please Select Policy and PDP Group to Push");
162         }
163         if(policySelection.length == 0 && currentSelection.length != 0){
164                 Notification.error("Please Select Policy to Push");
165         }
166         if(policySelection.length != 0 && currentSelection.length == 0){
167                 Notification.error("Please Select PDP Group to Push");
168         }
169         if(policySelection.length != 0 && currentSelection.length != 0){
170                 var finalData = {
171                                 "pdpDatas": currentSelection,
172                                 "policyDatas": policySelection
173                 };
174                 console.log(finalData);
175                 var uuu = "auto_Push/PushPolicyToPDP.htm";
176                 var postData={pushTabData: finalData};
177                 $.ajax({
178                         type : 'POST',
179                         url : uuu,
180                         dataType: 'json',
181                         contentType: 'application/json',
182                         data: JSON.stringify(postData),
183                         success : function(data){
184                                 $scope.$apply(function(){
185                                         $scope.data=data.data;
186                                         $scope.pdpdata = JSON.parse(data.data);
187                                         $scope.pushTabPDPGrid.data =  $scope.pdpdata;
188                                         Notification.success("Policy Pushed Successfully");
189                                 });
190                                 console.log($scope.data);
191                         },
192                         error : function(data){
193                                 Notification.error("Error Occured while Pushing Policy.");
194                         }
195                 });
196
197         }
198     };
199   
200
201 });