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