#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import json
import sys
import os


class Metadata(object):

    def __init__(self):
        pass

    def load_file(self, filename):
        json_str = None
        try:
            with open(filename) as data:
                json_str = data.read()
        except IOError:
            print("Machine facts json is missing")
            exit(1)
        sys_data = {}
        sys_data['system_data'] = json.loads(json_str)
        return sys_data

    def get_hardware_metadata(self, sys_data):
        hard_dict = {}
        for item in sys_data['system_data']:
            if 'hardware_details' not in hard_dict:
                hard_dict['hardware_details'] = []
            hardware_dict = {}
            hardware_dict['label'] = item['inventory_hostname']
            hardware_dict['kernel'] = item['ansible_kernel']
            hardware_dict['total_mem'] = item[
                'ansible_memory_mb']['real']['total']
            hardware_dict['total_logical_cores'] = item[
                'facter_processorcount']
            hardware_dict['os_name'] = item['ansible_distribution'] + \
                item['ansible_distribution_version']
            hardware_dict['ip'] = item['ansible_default_ipv4']['address']
            hardware_dict['num_interface'] = len(item['ansible_interfaces'])
            hardware_dict['machine_make'] = item['ansible_product_name']
            hard_dict['hardware_details'].append(hardware_dict)
        return hard_dict

    def get_environment_metadata(self, sys_data):
        env_dict = {}
        for item in sys_data['system_data']:
            if 'environment_setup' not in env_dict:
                env_dict['environment_setup'] = {}
            for key, value in item.items():
                if 'osp' in key:
                    env_dict['environment_setup'][key] = value
        return env_dict

    def get_software_metadata(self, sys_data):
        soft_all_dict = []
        for item in sys_data['system_data']:
            nodes = ['controller', 'undercloud', 'compute']
            if any(node in item['inventory_hostname'] for node in nodes):
                software_dict = {}
                for soft in item:
                    if 'openstack' in soft:
                        service = soft.split('_')
                        key = soft.split('_', 2)[2]
                        service_name = service[1]
                        node = item['inventory_hostname']
                        if service_name in software_dict:
                            if service_name in soft:
                                software_dict[service_name][key] = item[soft]
                        else:
                            software_dict[service_name] = {}
                            if service_name in soft:
                                software_dict[service_name]['node_name'] = node
                                software_dict[service_name][key] = item[soft]

                soft_all_dict.append(software_dict)
        return soft_all_dict

    def write_metadata_file(self, data, filename):
        with open(filename, 'w') as json_file:
            json.dump(data, json_file, indent=4, sort_keys=True)


def main():
    _filename = os.path.join(sys.argv[1], 'machine_facts.json')
    metadata = Metadata()
    sysdata = metadata.load_file(_filename)
    env_data = metadata.get_environment_metadata(sysdata)
    metadata.write_metadata_file(
        env_data, os.path.join(sys.argv[1], 'environment-metadata.json'))
    hardware_data = metadata.get_hardware_metadata(sysdata)
    metadata.write_metadata_file(
        hardware_data, os.path.join(sys.argv[1], 'hardware-metadata.json'))
    software_data = metadata.get_software_metadata(sysdata)
    metadata.write_metadata_file(
        software_data, os.path.join(sys.argv[1], 'software-metadata.json'))

if __name__ == '__main__':
    sys.exit(main())

@@ -585,16 +585,32 @@
 port os%0A
+import argparse%0A
 %0A%0Aclass 
@@ -1933,24 +1933,114 @@
 duct_name'%5D%0A
+            hardware_dict%5B'processor_type'%5D = ' '.join(item%5B'facter_processor0'%5D.split())%0A
             
@@ -3825,24 +3825,198 @@
 def main():%0A
+    parser = argparse.ArgumentParser(description='Metadata Generation')%0A    parser.add_argument(dest='path', help='Location of machine-facts')%0A    args = parser.parse_args()%0A
     _filenam
@@ -4032,27 +4032,25 @@
 th.join(
-sys.argv%5B1%5D
+args.path
 , 'machi
@@ -4185,32 +4185,33 @@
 tadata(sysdata)%0A
+%0A
     metadata.wri
@@ -4259,27 +4259,25 @@
 th.join(
-sys.argv%5B1%5D
+args.path
 , 'envir
@@ -4430,27 +4430,25 @@
 th.join(
-sys.argv%5B1%5D
+args.path
 , 'hardw
@@ -4602,19 +4602,17 @@
 oin(
-sys.argv%5B1%5D
+args.path
 , 's

Add Processor information to hardware metadata