Epic-231:versioning, and backup the configuration
[sdnc/oam.git] / configbackuprestore / vnfconfigbackupservice / src / main / webapp / js / sdnc-controller / sdnc-backupConfig-controller.js
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SDNC-FEATURES
4  * ================================================================================
5  * Copyright 2018 TechMahindra
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 myApp.controller('BackupConfigCtrl', ['$scope', '$http','$window', 'growl', 'deviceConfigService', 'ObjectDiff', '$timeout' , function($scope, $http, $window, growl, deviceConfigService, ObjectDiff, $timeout) {
21
22     $scope.showResult = false;
23     $scope.showCompare = false;
24
25     //THIS FUNCTION WILL BE CALLED ON PAGE LOAD
26     $scope.getAllVNF = function() {
27
28         deviceConfigService.getAllVNF().then(function(data) {
29             if (data != null) {
30                 console.log(data);
31                 $scope.objvnfList = data;
32                 console.log("CompareConfigCtrl:getAllVNF called" + $scope.objvnfList);
33             } else {
34                 $scope.warningMessage = "No VNF is eligible for configuration!!!";
35                 growl.error($scope.warningMessage, {
36                     title: 'Warning!',
37                     globalDisableCloseButton: false,
38                     ttl: 7000,
39                     disableCountDown: true  
40                 });
41             }
42         });
43     };
44     $scope.getAllVNF();
45
46     $scope.selectVnf = function(selectedValueVnf) {
47
48         if (selectedValueVnf != null && selectedValueVnf != "") {
49             console.log("selectedvnf Value", selectedValueVnf);
50             //selectedItem = selectedValueVnf.split("%");
51 //            var vnfName = selectedItem[0];
52 //            var vnfType = selectedItem[1];
53             var vnfId = selectedValueVnf;
54             $scope.getVersionList(vnfId);
55         } else {
56             $scope.ShowResult = false;
57             $scope.showCompare = false;
58             $scope.showResult = false;
59             $scope.errorMessage = "Please select a VNF!!!";
60             growl.error($scope.errorMessage, {
61                 title: 'Error!',
62                 globalDisableCloseButton: false,
63                 ttl: 7000,
64                 disableCountDown: true  
65             });
66         }
67         
68     }
69
70     //THIS FUNCTION WILL BE CALLED ON SELECTION OF VNF
71     $scope.getVersionList = function(vnfId) {
72
73         $scope.ShowResult = false;
74
75         //service call to fetch the version list
76         deviceConfigService.getVersions(vnfId).then(function(result) {
77                 console.log("--CompareConfigCtrl::getVersionList called from controler--", JSON.stringify(result));
78                 var status = result.status;
79                 var result = result.data;
80                 if (status == 200) {
81                     if (result.length >= 1) {
82                         //in case of success, build the model object to store the service output here
83                         $scope.createVersionModel(result);
84                     } else {
85                         $scope.ShowResult = false;
86                         $scope.warningMessage = "No configruation found for the selected VNF !!";
87                         growl.warning($scope.warningMessage, {
88                             title: 'Warning!',
89                             globalDisableCloseButton: false,
90                             ttl: 7000,
91                             disableCountDown: true  
92                         });
93                     }
94                 } else {
95                     $scope.ShowResult = false;
96                     $scope.warningMessage = "No configruation found for the selected VNF !!";
97                     growl.warning($scope.warningMessage, {
98                         title: 'Warning!',
99                         globalDisableCloseButton: false,
100                         ttl: 7000,
101                         disableCountDown: true  
102                     });
103                 }
104             },
105             function(response) {
106                 $scope.errorMessage = "Something went wrong, Please try again !!";
107                 growl.error($scope.errorMessage, {
108                     title: 'Error!',
109                     globalDisableCloseButton: false,
110                     ttl: 7000,
111                     disableCountDown: true  
112                 });
113                 console.log("--CompareConfigCtrl::getVersionList::Error--", response);
114             });
115     }
116
117     //Function to build the UI model to be shown
118     $scope.createVersionModel = function(result) {
119
120         $scope.objVersionModel = result;
121         $scope.objVersion = [];
122
123         console.log("--CompareConfigCtrl::createVersionModel::--", JSON.stringify($scope.objVersionModel));
124         if ($scope.objVersionModel.length >= 1) {
125             $scope.ShowResult = true;
126             $scope.showCompare = true;
127             for (var i = 0; i < $scope.objVersionModel.length; i++) {
128                 var objVersionDetail = {};
129                 objVersionDetail.vnfname = $scope.objVersionModel[i].vnfname;
130                 objVersionDetail.vnfid = $scope.objVersionModel[i].vnfid;
131                 objVersionDetail.versionNo = $scope.objVersionModel[i].vnfversion;
132                 objVersionDetail.createdAt = $scope.objVersionModel[i].creationdate;
133                 objVersionDetail.configinfo = $scope.objVersionModel[i].configinfo;
134                 objVersionDetail.selected = false;
135                 
136                 $scope.objVersion.push(objVersionDetail);
137             }
138         }
139         console.log("--CompareConfigCtrl::createVersionModel::final VersionModel--" + JSON.stringify($scope.objVersion));
140
141     }
142
143     $scope.CompareConfig = function(objVersion) {
144         var count = 0;
145         angular.forEach(objVersion, function(item) {
146             if (item.selected == true) 
147                 count++;
148         });
149         if (count > 2) {
150             $scope.errorMessage = "Only two config files can be selected for the comparison!!!";
151             growl.error($scope.errorMessage, {
152                 title: 'Error!',
153                 globalDisableCloseButton: false,
154                 ttl: 7000,
155                 disableCountDown: true  
156             });
157         } else if (count === 1){
158                  $scope.errorMessage = "At least two config files can be selected for the comparison!!!";
159              growl.error($scope.errorMessage, {
160                  title: 'Error!',
161                  globalDisableCloseButton: false,
162                  ttl: 7000,
163                  disableCountDown: true  
164              });
165         }else
166             $scope.createCompareModelNew(objVersion);
167     };
168
169     $scope.createCompareModelNew = function(objVersion) {
170
171         $scope.objCompareModel1 = {};
172         $scope.objCompareModel2 = {};
173
174         $scope.versionsSelected = [];
175         angular.forEach(objVersion, function(item) {
176             angular.forEach($scope.objVersionModel, function(val, index) {
177                 if (item.versionNo == val['versionNo'] && item.selected == false) {
178                     $scope.objVersionModel.splice(index, 1);
179                 }
180                 if (item.selected) {
181                     if ($scope.versionsSelected.indexOf(item) == -1)
182                         $scope.versionsSelected.push(item);
183                 }
184             })
185         });
186         console.log("--CompareConfigCtrl::createCompareModel::$scope.objVersionModel", JSON.stringify($scope.objVersionModel));
187         angular.forEach($scope.objVersionModel, function(item) {
188             var versionObj = {};
189             var versionDetails = {};
190             versionDetails.versionNo = item['vnfversion'];
191             /*versionDetails.vnfName = item['vnfname'];
192             versionDetails.vnfid = item['vnfid'];*/
193             versionDetails.timeStamp = item.creationdate;
194             versionObj.versionDetails = versionDetails;
195
196             //fetch all the other topology/network,opertaion status for the vnf
197            // versionObj.topologyInfo = $scope.fetchConfigDetails(item);
198             versionObj.topologyInfo = $scope.fetchTopologyInfo(item);
199                 
200             versionObj.networkTopologyInfo = $scope.fetchNetworkTopologyInfo(item);
201             versionObj.operationStatus = $scope.operationStatus(item);
202             versionObj.vnfTopologyIdentifier = $scope.vnfTopologyIdentifier(item);
203
204             if ((versionObj.versionDetails.versionNo == $scope.versionsSelected[0].versionNo)) {
205                 $scope.objCompareModel1 = versionObj;
206             } else
207                 $scope.objCompareModel2 = versionObj;
208
209         });
210         $scope.showResult = true;
211         console.log("CompareConfigCtrl::createCompareModel::objCompareModel1", JSON.stringify($scope.objCompareModel1));
212         console.log("CompareConfigCtrl::createCompareModel::objCompareModel2", JSON.stringify($scope.objCompareModel2));
213     }
214     
215     
216
217     $scope.fetchTopologyInfo = function(item) {
218         var topologyInfo = {};
219         item = JSON.parse(item.configinfo);
220         if (angular.isDefined(item['preload-data']) && angular.isDefined(item['preload-data']['vnf-topology-information'])) {
221             var vnfTopologyInfo = item['preload-data']['vnf-topology-information'];
222             if (angular.isDefined(vnfTopologyInfo['vnf-parameters'] && vnfTopologyInfo['vnf-parameters'] != null)) {
223                 var vnfParameters = vnfTopologyInfo['vnf-parameters'];
224                 for (var i = 0; i < vnfParameters.length; i++) {
225
226                     var key = vnfParameters[i]['vnf-parameter-name'];
227                     var value = vnfParameters[i]['vnf-parameter-value'];
228                     console.log("CompareConfigCtrl::fetchTopologyInfo::key", key);
229                     console.log("CompareConfigCtrl::fetchTopologyInfo::value", value);
230                     topologyInfo[key] = value;
231
232                 }
233                 console.log("CompareConfigCtrl::fetchTopologyInfo::", JSON.stringify(topologyInfo));
234                 return topologyInfo;
235             }
236         }
237     }
238     
239     $scope.fetchNetworkTopologyInfo = function(item) {
240         var networkTopology = {};
241         item = JSON.parse(item.configinfo);
242         if (angular.isDefined(item['preload-data']) && angular.isDefined(item['preload-data']['network-topology-information'])) {
243             var netwrokTopologyInfo = item['preload-data']['network-topology-information'];
244             if (angular.isDefined(netwrokTopologyInfo) && netwrokTopologyInfo != null) {
245                 for (var i = 0; i < netwrokTopologyInfo.length; i++) {
246
247                     var key = netwrokTopologyInfo[i]['vnf-parameter-name'];
248                     var value = netwrokTopologyInfo[i]['vnf-parameter-value'];
249                     console.log("CompareConfigCtrl::fetchTopologyInfo::key", key);
250                     console.log("CompareConfigCtrl::fetchTopologyInfo::value", value);
251                     networkTopology[key] = value;
252                 }
253             }
254         }
255         console.log("CompareConfigCtrl::fetchNetworkTopologyInfo::", JSON.stringify(networkTopology));
256         return networkTopology;
257     }
258     
259     $scope.operationStatus = function(item) {
260         var operationStatus = {};
261         item = JSON.parse(item.configinfo);
262         if (angular.isDefined(item['preload-data']) && angular.isDefined(item['preload-data']['oper-status'])) {
263             var operStatus = item['preload-data']['oper-status'];
264             if (angular.isDefined(operStatus) && operStatus != null) {
265
266                 var value = operStatus['order-status'];
267                 operationStatus['order-status'] = value;
268
269             }
270         }
271         console.log("CompareConfigCtrl::operationStatus::", JSON.stringify(operationStatus));
272         return operationStatus;
273     }
274     
275     $scope.vnfTopologyIdentifier = function(item) {
276         var topologyIdnetifier = {};
277         item = JSON.parse(item.configinfo);
278         if (angular.isDefined(item['preload-data']) && angular.isDefined(item['preload-data']['vnf-topology-information']['vnf-topology-identifier'])) {
279             var topologyInfoidentifier = item['preload-data']['vnf-topology-information']['vnf-topology-identifier'];
280             if (angular.isDefined(topologyInfoidentifier)) {
281                 angular.forEach(topologyInfoidentifier, function(value, key) {
282
283                     console.log("CompareConfigCtrl::fetchTopologyInfo::key", key);
284                     console.log("CompareConfigCtrl::fetchTopologyInfo::value", value);
285                     topologyIdnetifier[key] = value;
286                 });
287             }
288         }
289
290         console.log("CompareConfigCtrl::vnfTopologyIdentifier::", JSON.stringify(topologyIdnetifier));
291         return topologyIdnetifier;
292     }
293     
294     $scope.invokeBackup = function(){
295         deviceConfigService.invokeBackup().then(function(data) {
296                 console.log("response -- data -- "+data)
297                 $window.location.reload();
298         });
299     }
300     
301     $scope.getLastModifiedTime=function(){
302 //      $timeout(function(result){
303 //              console.log("response-data-"+ result);
304 //              $scope.lastModifiedTime="Testcode Dushyant"
305 //      }
306 //    ,5000);
307         
308         deviceConfigService.getlastupdated().then(function(result) {
309                 $scope.lastModifiedTime= result.data;
310                 console.log("response -- getlastupdated -- "+JSON.stringify(result))
311         });
312     }
313     $scope.getLastModifiedTime()
314 }]);