Updating network settings

Adding display network settings info in network settings page

Change-Id: I5ab3b399445b8d2d6422f907dc4c5a477bfc794d
Signed-off-by: Iftekharul Islam <iffy.ryan@ibm.com>
diff --git a/app/common/services/api-utils.js b/app/common/services/api-utils.js
index c17577c..a7a6a58 100644
--- a/app/common/services/api-utils.js
+++ b/app/common/services/api-utils.js
@@ -72,6 +72,55 @@
                     var hostname = "";
                     var macAddress = "";
 
+                    function parseNetworkData(content){
+                      var data = {
+                        interface_ids: [],
+                        interfaces: {
+                        }
+                      };
+                      var interfaceId = '', keyParts = [], interfaceHash = '', interfaceType = '';
+                      for(var key in content.data){
+                        if(key.match(/network\/eth\d+$/ig)){
+                          interfaceId = key.split("/").pop();
+                          if(data.interface_ids.indexOf(interfaceId) == -1){
+                            data.interface_ids.push(interfaceId);
+                            data.interfaces[interfaceId] = {
+                              interfaceIname: '',
+                              domainName:'',
+                              MACAddress:'',
+                              Nameservers: [],
+                              DHCPEnabled: 0,
+                              ipv4:
+                                {
+                                 ids: [],
+                                 values: []
+                                },
+                              ipv6:
+                                {
+                                 ids: [],
+                                 values: []
+                                }
+                            };
+                            data.interfaces[interfaceId].MACAddress = content.data[key].MACAddress;
+                            data.interfaces[interfaceId].DomainName = content.data[key].DomainName.join(" ");
+                            data.interfaces[interfaceId].Nameservers = content.data[key].Nameservers;
+                            data.interfaces[interfaceId].DHCPEnabled = content.data[key].DHCPEnabled;
+                          }
+                        }else if(key.match(/network\/eth\d+\/ipv[4|6]\/[a-z0-9]+$/ig)){
+                          keyParts = key.split("/");
+                          interfaceHash = keyParts.pop();
+                          interfaceType = keyParts.pop();
+                          interfaceId = keyParts.pop();
+
+                          if(data.interfaces[interfaceId][interfaceType].ids.indexOf(interfaceHash) == -1){
+                            data.interfaces[interfaceId][interfaceType].ids.push(interfaceHash);
+                            data.interfaces[interfaceId][interfaceType].values.push(content.data[key]);
+                          }
+                        }
+                      }
+                      return data;
+                    }
+
                     if(content.data.hasOwnProperty('/xyz/openbmc_project/network/config') &&
                       content.data['/xyz/openbmc_project/network/config'].hasOwnProperty('HostName')
                       ){
@@ -88,6 +137,7 @@
                       data: content.data,
                       hostname: hostname,
                       mac_address: macAddress,
+                      formatted_data: parseNetworkData(content)
                     });
                 }).error(function(error){
                   console.log(error);
@@ -594,7 +644,7 @@
                         if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
 
                           functional = (content.data[key].Priority == 0);
-                          active = (/\.Active$/).test(content.data[key].Activation);
+                          active = !functional && (/\.Active$/).test(content.data[key].Activation);
                           ready = (/\.Ready$/).test(content.data[key].Activation);
                           activationStatus = {functional: functional, active: active, ready: ready};
                           imageType = content.data[key].Purpose.split(".").pop();
@@ -644,8 +694,8 @@
                   method: 'PUT',
                   url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Priority",
                   headers: {
-                      'Accept': 'application/octet-stream',
-                      'Content-Type': 'application/octet-stream'
+                      'Accept': 'application/json',
+                      'Content-Type': 'application/json'
                   },
                   withCredentials: true,
                   data: JSON.stringify({"data": priority})
@@ -660,15 +710,58 @@
 
                 return deferred.promise;
               },
-              uploadImage: function(file){
+              deleteImage: function(imageId){
+                var deferred = $q.defer();
+                $http({
+                  method: 'POST',
+                  url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/action/Delete",
+                  headers: {
+                      'Accept': 'application/json',
+                      'Content-Type': 'application/json'
+                  },
+                  withCredentials: true,
+                  data: JSON.stringify({"data": []})
+                }).success(function(response){
+                      var json = JSON.stringify(response);
+                      var content = JSON.parse(json);
+                      deferred.resolve(content);
+                }).error(function(error){
+                  console.log(error);
+                  deferred.reject(error);
+                });
+
+                return deferred.promise;
+              },
+              activateImage: function(imageId){
                 var deferred = $q.defer();
                 $http({
                   method: 'PUT',
+                  url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/RequestedActivation",
+                  headers: {
+                      'Accept': 'application/json',
+                      'Content-Type': 'application/json'
+                  },
+                  withCredentials: true,
+                  data: JSON.stringify({"data": Constants.FIRMWARE.ACTIVATE_FIRMWARE})
+                }).success(function(response){
+                      var json = JSON.stringify(response);
+                      var content = JSON.parse(json);
+                      deferred.resolve(content);
+                }).error(function(error){
+                  console.log(error);
+                  deferred.reject(error);
+                });
+
+                return deferred.promise;
+              },
+              uploadImage: function(file){
+                var deferred = $q.defer();
+                $http({
+                  method: 'POST',
                   timeout: 5 * 60 * 1000,
                   url: DataService.getHost() + "/upload/image/",
                   headers: {
-                      'Accept': 'application/octet-stream',
-                      'Content-Type': 'application/octet-stream'
+                    'Content-Type': 'application/octet-stream'
                   },
                   withCredentials: true,
                   data: file
diff --git a/app/common/services/constants.js b/app/common/services/constants.js
index 4852954..76bc590 100644
--- a/app/common/services/constants.js
+++ b/app/common/services/constants.js
@@ -111,6 +111,7 @@
                 ],
                 SENSOR_SORT_ORDER_DEFAULT: 8,
                 FIRMWARE: {
+                  ACTIVATE_FIRMWARE: 'xyz.openbmc_project.Software.Activation.RequestedActivations.Active',
                   FALLBACK_DOWNLOAD_FILENAME: 'firmware_download.tar',
                   TYPES: {
                     Functional: 'Functional',
diff --git a/app/configuration/controllers/firmware-controller.js b/app/configuration/controllers/firmware-controller.js
index 50df4f0..cafc707 100644
--- a/app/configuration/controllers/firmware-controller.js
+++ b/app/configuration/controllers/firmware-controller.js
@@ -63,7 +63,20 @@
                     }
 
                     $scope.preserveSettingsConfirmed = function(){
-                        //show progress..callapi..hide..iferror..show error
+                        $scope.uploading = true;
+                        APIUtils.activateImage($scope.activate_image_id).then(function(response){
+                            $scope.uploading = false; 
+                            if(response.status == 'error'){
+                                $scope.displayError({
+                                    modal_title: response.data.description,
+                                    title: response.data.description,
+                                    desc: response.data.exception,
+                                    type: 'Error'
+                                });
+                            }else{
+                                $scope.loadFirmwares();
+                            }
+                        });
                         $scope.preserve_settings_confirm = false;
                     }
 
@@ -105,7 +118,7 @@
                             var contentType = headers['content-type'];
 
                             if(!headers['x-filename']){
-                                filename = Constants.FIRMWARE.FALLBACK_DOWNLOAD_FILENAME;
+                                filename = $scope.download_filename;
                             }
 
                             var linkElement = document.createElement('a');
@@ -161,10 +174,23 @@
                         $scope.delete_image_id = imageId;
                         $scope.confirm_delete = true;
                     }
-                    $scope.confirmDeleteImage = function(imageId){
+                    $scope.confirmDeleteImage = function(){
+                        $scope.loading = true;
+                        APIUtils.deleteImage($scope.delete_image_id).then(function(response){
+                            $scope.loading = false;
+                            if(response.status == 'error'){
+                                $scope.displayError({
+                                    modal_title: response.data.description,
+                                    title: response.data.description,
+                                    desc: response.data.exception,
+                                    type: 'Error'
+                                });
+                            }else{
+                                $scope.loadFirmwares();
+                            }
+                        });
                         $scope.confirm_delete = false;
                     }
-
                     $scope.fileNameChanged = function(){
                         $scope.file_empty = false;
                     }
diff --git a/app/configuration/controllers/network-controller.html b/app/configuration/controllers/network-controller.html
index 6276ad3..caa2913 100644
--- a/app/configuration/controllers/network-controller.html
+++ b/app/configuration/controllers/network-controller.html
@@ -9,30 +9,27 @@
 			</div>
 			<fieldset>
 				<div class="column small-12 large-3">
+					<label for="net-config__mac">Hostname</label>
+					<input id="net-config__mac" type="text" readonly value="{{hostname}}"/>
+				</div>
+				<div class="column small-12 large-3">
 					<label for="netinterface-select">Network interface</label>
 					<div id="netinterface-select" class="dropdown__wrapper">
-						<button class="dropdown__button" ng-click="networkDevice = ! networkDevice">eth0</button>
+						<button type="button" class="dropdown__button" ng-click="networkDevice = !networkDevice">{{selectedInterface}}</button>
 						<ul class="dropdown__list inline" ng-show="networkDevice">
-							<li>
-								<button ng-click="networkDevice=false;">eth0</button>
-							</li>
-							<li>
-								<button ng-click="networkDevice=false;">eth1</button>
+							<li ng-repeat="interface_id in network.interface_ids">
+								<button type="button" ng-click="selectInterface(interface_id);">{{interface_id}}</button>
 							</li>
 						</ul>
 					</div>
 				</div>
 				<div class="column small-12 large-3">
-					<label for="net-config__mac">MAC address</label>
-					<input id="net-config__mac" type="text" readonly/>
-				</div>
-				<div class="column small-12 large-3">
-					<label for="net-config__host">Hostname</label>
-					<input id="net-config__host" type="text"/>
+					<label for="net-config__host">MAC address</label>
+					<input id="net-config__host" type="text" ng-model="interface.MACAddress"/>
 				</div>
 				<div class="column small-12 large-3">
 					<label for="net-config__domain">Domain name</label>
-					<input id="net-config__domain" type="text"/>
+					<input id="net-config__domain" type="text" ng-model="interface.DomainName"/>
 				</div>
 			</fieldset>
 		</section>
@@ -43,28 +40,28 @@
 			<fieldset>
 				<div class="row column">
 					<label class="control-radio" for="dhcp-address">Obtain an IP address automatically using DHCP
-						<input type="radio" name="ip-address" id="dhcp-address" ng-checked="true" ng-model="dhcp" ng-value="true" ng-init="dhcp=true"/>
+						<input type="radio" name="ip-address" id="dhcp-address" ng-checked="true" ng-model="interface.DHCPEnabled" ng-value="true">
 						<span class="control__indicator control__indicator-on"></span>
 					</label>
 				</div>
 				<div class="row column">
 					<label class="control-radio" for="static-ip-address">Assign a static IP address
-						<input type="radio" name="ip-address" id="static-ip-address" ng-model="dhcp" ng-value="false"/>
+						<input type="radio" name="ip-address" id="static-ip-address" ng-model="interface.DHCPEnabled" ng-value="false"/>
 						<span class="control__indicator control__indicator-on"></span>
 					</label>
 				</div>
-				<fieldset class="net-config__static-ip-wrap">
+				<fieldset class="net-config__static-ip-wrap" ng-repeat="ipv4 in interface.ipv4.values">
 					<div class="column small-12 large-4">
 						<label for="net-config__ipv4-address" class="inline">IPV4 address</label>
-						<input id="net-config__ipv4-address" type="text" ng-disabled="dhcp" value=""/>
+						<input id="net-config__ipv4-address" type="text" ng-disabled="interface.DHCPEnabled" ng-model="ipv4.Address"/>
 					</div>
 					<div class="column small-12 large-4">
-						<label for="net-config__subnet" class="inline">Subnet mask</label>
-						<input id="net-config__subnet" type="text" ng-disabled="dhcp" value=""/>
+						<label for="net-config__subnet" class="inline">Gateway</label>
+						<input id="net-config__subnet" type="text" ng-disabled="interface.DHCPEnabled" value="" ng-model="ipv4.Gateway"/>
 					</div>
 					<div class="column small-12 large-4">
-						<label for="net-config__default-gateway" class="inline">Default gateway</label>
-						<input id="net-config__default-gateway" type="text" ng-disabled="dhcp" value=""/>
+						<label for="net-config__default-gateway" class="inline">Origin</label>
+						<input id="net-config__default-gateway" type="text" ng-disabled="interface.DHCPEnabled"  ng-model="ipv4.Origin"/>
 					</div>
 				</fieldset>
 			</fieldset>
@@ -75,16 +72,12 @@
 			</div>
 			<fieldset>
 				<div class="column small-12 large-4">
-					<label for="net-config__prime-dns">Primary DNS server</label>
-					<input id="net-config__prime-dns" type="text"/>
-				</div>
-				<div class="column small-12 large-4">
-					<label for="net-config__secondary-dns">Secondary DNS server</label>
-					<input id="net-config__secondary-dns" type="text"/>
-				</div>
-				<div class="column small-12 large-4">
-					<label for="net-config__tertiary-dns">Tertiary DNS server</label>
-					<input id="net-config__tertiary-dns" type="text"/>
+					<ul>
+						<li ng-repeat="dns in interface.Nameservers track by $index">
+							<label for="net-config__prime-dns">DNS Server {{$index+1}}</label>{{dns}}
+							<br/>
+						</li>
+					</ul>
 				</div>
 			</fieldset>
 			<div class="network-config__submit-wrapper">
diff --git a/app/configuration/controllers/network-controller.js b/app/configuration/controllers/network-controller.js
index 31b5030..90b89f8 100644
--- a/app/configuration/controllers/network-controller.js
+++ b/app/configuration/controllers/network-controller.js
@@ -19,6 +19,24 @@
             'dataService',
             function($scope, $window, APIUtils, dataService){
                 $scope.dataService = dataService;
+                $scope.network = {};
+                $scope.interface = {};
+                $scope.networkDevice  = false;
+                $scope.hostname = "";
+
+                $scope.selectInterface = function(interfaceId){
+                    $scope.interface = $scope.network.interfaces[interfaceId];
+                    $scope.selectedInterface = interfaceId;
+                    $scope.networkDevice = false;
+                }
+                APIUtils.getNetworkInfo().then(function(data){
+                    $scope.network = data.formatted_data;
+                    $scope.hostname = data.hostname;
+                    if($scope.network.interface_ids.length){
+                       $scope.selectedInterface = $scope.network.interface_ids[0];
+                       $scope.interface = $scope.network.interfaces[$scope.selectedInterface];
+                    }
+                });
             }
         ]
     );