Epic-231:versioning, and backup the configuration
[sdnc/oam.git] / configbackuprestore / vnfconfigbackupservice / src / main / webapp / js / sdnc-controller / sdnc-validationTest-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
21
22 myApp.controller('TestController', ['$scope', '$http', 'growl', 'viewReportService', 'validationTestService','CERTIFICATION_API_BASE', function($scope, $http, growl, viewReportService, validationTestService,CERTIFICATION_API_BASE) {
23
24     $scope.showTestReport = false;
25
26     //Input data for validation test dropdown
27     var validationTestList = [{
28             "typeId": 1,
29             "validationType": "Network Layer",
30             "Selected": false
31         },
32         {
33             "typeId": 2,
34             "validationType": "Protocol Layer",
35             "Selected": false
36         }
37     ];
38
39     $scope.validationTestList = validationTestList;
40
41
42     $scope.getselectval = function() {
43         $scope.selectedvalues = 'Name: ' + $scope.selitem.validationType + ' Id: ' + $scope.selitem.typeId;
44     }
45
46     $scope.showError = false;
47     $scope.errorMessage = "";
48     $scope.showSuccess = false;
49     $scope.successMessage = "";
50     $scope.showWarning = false;
51     $scope.warningMessage = "";
52     $scope.showInfo = false;
53     $scope.infoMessage = "";
54     $scope.ShowResult = false;
55     $scope.allowTestSelection = false;
56     $scope.hostNamePattern = /([a-z0-9](.[a-z0-9-]*[a-z0-9]))/;
57
58     //Called when user clicks on runtest button
59     $scope.runTest = function() {
60
61         $scope.showError = false;
62         $scope.vnfSelected = [];
63         $scope.testSelected = [];
64
65         var message = "";
66         for (var i = 0; i < $scope.validationTestList.length; i++) {
67             if ($scope.validationTestList[i].Selected) {
68                 var typeId = $scope.validationTestList[i].typeId;
69                 var validationType = $scope.validationTestList[i].validationType;
70                 message += "typeId: " + typeId + " validationType: " + validationType + "\n";
71                 console.log("--TestController::Runtest--", message);
72                 var testDetails = {};
73                 testDetails.typeId = typeId;
74                 testDetails.validationType = validationType;
75                 $scope.testSelected.push(testDetails);
76             }
77         }
78         console.log("--TestController::Runtest--", JSON.stringify($scope.testSelected));
79
80         var vnfDetails = {};
81         if ($scope.IPAddress != null && $scope.HostName != null && $scope.testSelected.length >= 1) {
82             vnfDetails.IpAddress = $scope.IPAddress;
83             vnfDetails.HostName = $scope.HostName;
84             $scope.vnfSelected.push(vnfDetails);
85             console.log("--TestController::Runtest--", JSON.stringify($scope.vnfSelected));
86
87             //Call the validation test service with vnfselected and testType as the parameter
88
89             validationTestService.runPretest($scope.vnfSelected, $scope.testSelected).then(function(response) {
90                     console.log("--TestController::runTest--", JSON.stringify(response));
91                     
92                     //in case of success, build the model object to store the service output here
93                     if (response.status === 200) {
94                         if (response.preTestResponse != null && response.preTestResponse.length >= 1) {
95                             $scope.ShowResult = true;
96                             $scope.createTestModel(response.preTestResponse);
97                             $scope.showMessage(response.preTestResponse);
98                          
99                         } else {
100                             console.log(response);
101                             $scope.showError = true;
102                             $scope.errorMessage = "Something went wrong!!!";
103                             growl.error($scope.errorMessage, {
104                                 title: 'Error!',
105                                 globalDisableCloseButton:false
106                             });
107                         }
108                     } else {
109                         console.log(response);
110                         $scope.showError = true;
111                         $scope.errorMessage = "Something went wrong!!! Please try again later";
112                         growl.error($scope.errorMessage, {
113                             title: 'Error!',
114                             globalDisableCloseButton:false
115                         });
116                     }
117                 },
118                 function(response) {
119                     console.log("--TestController--", response);
120                 });
121         } else {
122             $scope.showError = true;
123             $scope.errorMessage = "Please provide the inputs for VNF and test to be performed!!";
124             growl.error($scope.errorMessage, {
125                 title: 'Error!',
126                 globalDisableCloseButton:false
127             });
128         }
129
130     };
131     
132 $scope.showMessage=function(data){
133         
134         var iterate = true;
135         if(angular.isDefined(data) && data.length>=1){
136                 
137              iterate=true;
138                  angular.forEach(data, function(value, key){
139
140                 if(iterate=true){
141                  if(value.status == "unreachable"){
142                    console.log("--TestController::showMessage--",value.status);
143                    $scope.showError = true;
144                    $scope.errorMessage = "PreTest validation Failed, Please check logs for further details!!!";
145                    growl.error($scope.errorMessage, {
146                      title: 'Error!',
147                      globalDisableCloseButton:false
148                    });
149                    iterate=false;
150                  }
151              }
152                  else
153                          {
154                           $scope.showSuccess = true;
155                       $scope.successMessage = "Pre test validation completed!!";
156                       growl.success($scope.successMessage, {
157                           title: 'Success!',
158                           globalDisableCloseButton:false
159                       });
160                          }
161                  
162                  });
163                 
164         }       
165     }
166
167     //Function to build the UI model to be shown
168     $scope.createTestModel = function(result) {
169
170         $scope.showError = false;
171         $scope.showWarning = false;
172         $scope.objPreTestModel = result;
173         $scope.objPreTest = [];
174
175
176         if ($scope.objPreTestModel.length >= 1) {
177             for (var i = 0; i < $scope.objPreTestModel.length; i++) {
178                 var objTestReport = {};
179                 objTestReport.ipaddress = $scope.objPreTestModel[i].ipaddress;
180                 objTestReport.status = $scope.objPreTestModel[i].status;
181                 objTestReport.testtype = $scope.objPreTestModel[i].testtype;
182                 objTestReport.statistics = $scope.objPreTestModel[i].statistics;
183                 objTestReport.avgTime = $scope.objPreTestModel[i].avgTime;
184
185
186                 if ($scope.objPreTestModel[i].testtype === "Network Layer") {
187                     if (objTestReport.statistics != null) {
188                         //fetching the statistics to show in progress bar
189                         
190                         var statistics = objTestReport.statistics;
191                         statistics = statistics.split("%");
192                         objTestReport.statistics = statistics[0];
193                         if (objTestReport.statistics == 0) {
194                             objTestReport.statisticPer = parseInt(objTestReport.statistics) + 50;
195                         } else
196                             objTestReport.statisticPer = objTestReport.statistics;
197                     }
198
199                     //fetching the avg time to show in progress bar
200                     
201                     if (objTestReport.avgTime != null) {
202                         var avgTime = objTestReport.avgTime;
203                         avgTime = avgTime.split("=");
204                         var Testtime = avgTime[1];
205                         objTestReport.avgTime = Testtime.slice(0, -2).trim();
206                         console.log("--TestController::createTestModel--", objTestReport.avgTime);
207                         if (objTestReport.avgTime < 50) {
208                             objTestReport.avgTimePer = parseInt(objTestReport.avgTime) + 10;
209                         } else
210                             objTestReport.avgTimePer = objTestReport.avgTime;
211
212                     }
213                 }
214                 $scope.objPreTest.push(objTestReport);
215                 console.log("--TestController::createTestModel--", JSON.stringify($scope.objPreTest));
216             }
217         }
218        console.log("--TestController::createTestModel::final PreTestModel--" + JSON.stringify($scope.objPreTest));
219
220     }
221
222 }]);