Add dynamic content to hardware inventory page

Change-Id: I3b34e58ada63c32e2476e37b7116232f5763bbdb
Signed-off-by: Iftekharul Islam <iislam@us.ibm.com>
diff --git a/app/common/services/api-utils.js b/app/common/services/api-utils.js
index ed10768..b044328 100644
--- a/app/common/services/api-utils.js
+++ b/app/common/services/api-utils.js
@@ -599,7 +599,7 @@
               getBMCEthernetInfo: function(callback){
                 $http({
                   method: 'GET',
-                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
+                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory",
                   headers: {
                       'Accept': 'application/json',
                       'Content-Type': 'application/json'
@@ -620,7 +620,7 @@
               getBMCInfo: function(callback){
                 $http({
                   method: 'GET',
-                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
+                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory",
                   headers: {
                       'Accept': 'application/json',
                       'Content-Type': 'application/json'
@@ -638,6 +638,110 @@
                   console.log(error);
                 });
               },
+              getHardwares: function(callback){
+                $http({
+                  method: 'GET',
+                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system",
+                  headers: {
+                      'Accept': 'application/json',
+                      'Content-Type': 'application/json'
+                  },
+                  withCredentials: true
+                }).success(function(response){
+                      var json = JSON.stringify(response);
+                      var content = JSON.parse(json);
+                      var hardwareData = [];
+                      var keyIndexMap = {};
+                      var title = "";
+                      var data = [];
+                      var searchText = "";
+                      var componentIndex = -1;
+                      var tempParts = [];
+
+
+                      function isSubComponent(key){
+
+                        for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
+                          if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
+                        }
+
+                        return false;
+                      }
+
+                      function titlelize(title){
+                        title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
+                        for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
+                          if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
+                            return title.toUpperCase();
+                          }
+                        }
+
+                        return title;
+                      }
+
+                      function camelcaseToLabel(obj){
+                        var transformed = [], label = "";
+                        for(var key in obj){
+                          label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
+                          if(obj[key] !== ""){
+                            transformed.push({key:label, value: obj[key]});
+                          }
+                        }
+
+                        return transformed;
+                      }
+
+                      function getSearchText(data){
+                        var searchText = "";
+                        for(var i = 0; i < data.length; i++){
+                          searchText += " " + data[i].key + " " + data[i].value;
+                        }
+
+                        return searchText; 
+                      }
+
+                      for(var key in content.data){
+                        if(content.data.hasOwnProperty(key) && 
+                           key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
+
+                          data = camelcaseToLabel(content.data[key]);
+                          searchText = getSearchText(data);
+                          title = key.split("/").pop();
+
+                          title = titlelize(title);
+
+                          if(!isSubComponent(key)){
+                              hardwareData.push(Object.assign({
+                                path: key,
+                                title: title,
+                                selected: false,
+                                expanded: false,
+                                search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
+                                sub_components: [], 
+                                original_data: {key: key, value: content.data[key]}
+                              }, {items: data}));
+
+                              keyIndexMap[key] = hardwareData.length - 1;
+                          }else{
+                            var tempParts = key.split("/");
+                            tempParts.pop();
+                            tempParts = tempParts.join("/");
+                            componentIndex = keyIndexMap[tempParts];
+                            data = content.data[key];
+                            data.title = title;
+                            hardwareData[componentIndex].sub_components.push(data);
+                            hardwareData[componentIndex].search_text += " " + title.toLowerCase();
+                          }
+                      }
+                    }
+
+                    if(callback){
+                       callback(hardwareData, content.data);
+                    }else{
+                       return { data: hardwareData, original_data: content.data};
+                    }
+                });
+              },
           };
           return SERVICE;
         }]);
diff --git a/app/common/services/constants.js b/app/common/services/constants.js
index 7616b3e..1445081 100644
--- a/app/common/services/constants.js
+++ b/app/common/services/constants.js
@@ -20,7 +20,7 @@
                     password: "testpass",
                 },
                 API_CREDENTIALS: {
-                    host: 'https://9.3.185.173',
+                    host: 'https://9.3.164.177',
                     mock_host: 'http://localhost:3000'
                 },
                 API_RESPONSE: {
@@ -61,6 +61,15 @@
                 PAGINATION: {
                     LOG_ITEMS_PER_PAGE: 4
                 },
+                HARDWARE: {
+                  component_key_filter: '/xyz/openbmc_project/inventory/system',
+                  parent_components: [
+                   /xyz\/openbmc_project\/inventory\/system\/chassis\/motherboard\/cpu\d+\//
+                  ],
+                  uppercase_titles: [
+                   'cpu', 'dimm'
+                  ]
+                },
                 SENSOR_DATA_TEMPLATE: {
                     sensors: [
                         {