Refresh the firmware page after downloading image

Fix includes
- Polling the number of software objects for a new image. During
  which time the 'Downloading in progress' status bar will be
  displayed. A 2-minute timeout is used.
- Displaying a 'Download complete' message if the download succeeds
  and refreshing the firmware page to show the new image once it is
  downloaded.
- Displaying an error pop-up with reason for failure when the
  download fails.

Resolves openbmc/openbmc#3054

Change-Id: I79a8b8c661c01474a12cef4af524cafdc2cee1fe
Signed-off-by: CamVan Nguyen <ctnguyen@us.ibm.com>
diff --git a/app/configuration/controllers/firmware-controller.html b/app/configuration/controllers/firmware-controller.html
index c30b4d2..2e5bf25 100644
--- a/app/configuration/controllers/firmware-controller.html
+++ b/app/configuration/controllers/firmware-controller.html
@@ -47,6 +47,7 @@
 				</div>
 				<div class="inline uploading" ng-show="downloading">Downloading in progress...</div>
 				<p class="download_error error-msg" ng-show="download_error_msg" role="alert">{{download_error_msg}}</p>
+				<p class="download_success" ng-show="download_success" role="alert">Download complete. Check image tables above.</p>
 			</fieldset>
 		</div>
 	</form>
diff --git a/app/configuration/controllers/firmware-controller.js b/app/configuration/controllers/firmware-controller.js
index 85edcb8..780bb19 100644
--- a/app/configuration/controllers/firmware-controller.js
+++ b/app/configuration/controllers/firmware-controller.js
@@ -50,8 +50,10 @@
                     $scope.uploading = false;
                     $scope.activate = { reboot: true };
                     $scope.download_error_msg = "";
+                    $scope.download_success = false;
 
                     var pollActivationTimer = undefined;
+                    var pollDownloadTimer = undefined;
 
                     $scope.error = {
                         modal_title: "",
@@ -164,25 +166,71 @@
                       }
                     }
 
+                    //TODO: openbmc/openbmc#1691 Add support to return
+                    //the id of the newly created image, downloaded via
+                    //tftp. Polling the number of software objects is a
+                    //near term solution.
+                    function waitForDownload(){
+                        var deferred = $q.defer();
+                        var startTime = new Date();
+                        pollDownloadTimer = $interval(function(){
+                            var now = new Date();
+                            if((now.getTime() - startTime.getTime()) >= Constants.TIMEOUT.DOWNLOAD_IMAGE){
+                                $interval.cancel(pollDownloadTimer);
+                                pollDownloadTimer = undefined;
+                                deferred.reject(new Error(Constants.MESSAGES.POLL.DOWNLOAD_IMAGE_TIMEOUT));
+                            }
+
+                            APIUtils.getFirmwares().then(function(response){
+                                if(response.data.length === $scope.firmwares.length + 1){
+                                    $interval.cancel(pollDownloadTimer);
+                                    pollDownloadTimer = undefined;
+                                    deferred.resolve(response.data);
+                                }
+                            }, function(error){
+                                $interval.cancel(pollDownloadTimer);
+                                pollDownloadTimer = undefined;
+                                deferred.reject(error);
+                            });
+                        }, Constants.POLL_INTERVALS.DOWNLOAD_IMAGE);
+
+                        return deferred.promise;
+                    }
+
                     $scope.download = function(){
+                        $scope.download_success = false;
                         $scope.download_error_msg = "";
                         if(!$scope.download_host || !$scope.download_filename){
                           $scope.download_error_msg = "Field is required!";
                           return false;
                         }
+
                         $scope.downloading = true;
-                        APIUtils.downloadImage($scope.download_host, $scope.download_filename).then(function(response){
-                          $scope.downloading = false;
-                          // TODO: refresh firmware page to display new image
+                        APIUtils.getFirmwares().then(function(response){
+                            $scope.firmwares =  response.data;
+                        }).then(function(){
+                            return APIUtils.downloadImage($scope.download_host,
+                                    $scope.download_filename).then(function(downloadStatus){
+                                return downloadStatus;
+                            });
+                        }).then(function(downloadStatus){
+                            return waitForDownload();
+                        }).then(function(newFirmwareList){
+                            $scope.download_host = "";
+                            $scope.download_filename = "";
+                            $scope.downloading = false;
+                            $scope.download_success = true;
+                            $scope.loadFirmwares();
                         }, function(error){
-                          $scope.downloading = false;
-                          $scope.displayError({
-                            modal_title: 'Error during downloading Image',
-                            title: 'Error during downloading Image',
-                            desc: JSON.stringify(error),
-                            type: 'Error'
-                          });
-                      });
+                            console.log(error);
+                            $scope.displayError({
+                                modal_title: 'Error during downloading Image',
+                                title: 'Error during downloading Image',
+                                desc: error,
+                                type: 'Error'
+                            });
+                            $scope.downloading = false;
+                        });
                     }
 
                     $scope.changePriority = function(imageId, imageVersion, from, to){
diff --git a/app/configuration/styles/firmware.scss b/app/configuration/styles/firmware.scss
index 1a39ad6..8ca34a9 100644
--- a/app/configuration/styles/firmware.scss
+++ b/app/configuration/styles/firmware.scss
@@ -142,6 +142,7 @@
 .firmware__upload-tftp {
   padding-top: 2em;
   padding-left: 1em;
+  padding-bottom: 50px;
 }
 
 .download_error
@@ -150,3 +151,9 @@
     max-width: 35%;
   }
 }
+
+.download_success
+{
+  color: $primebtn__bg;
+  padding: 1em;
+}