Add IPMI FRU parser code generator.

Add a script that generates c++ structures
based on system dependent configuration.

Change-Id: I3a005b2dd31d7db3137bed9be6df2a0658c48d82
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/scripts/example.yaml b/scripts/example.yaml
new file mode 100644
index 0000000..a9543ba
--- /dev/null
+++ b/scripts/example.yaml
@@ -0,0 +1,119 @@
+# A YAML similar to this example would have to be generated, for eg with MRW
+# inputs and system configuration, to depict IPMI Fru information.
+#
+# This file maps IPMI properties to phosphor dbus inventory properties
+#
+# This YAML could help generate C++ code.
+# Format of the YAML:
+# Fruid:
+#   Associated Fru paths
+#     d-bus Interafaces
+#       d-bus Properties
+#         IPMI Fru mapping
+0:
+  /system:
+    xyz.openbmc_project.Inventory.Item:
+      PrettyName:
+        IPMIFruProperty: Product Name
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Decorator.Asset:
+      Manufacturer:
+        IPMIFruProperty: Manufacturer
+        IPMIFruSection: Product
+      PartNumber:
+        IPMIFruProperty: Part Number
+        IPMIFruSection: Product
+      SerialNumber:
+        IPMIFruProperty: Serial Number
+        IPMIFruSection: Product
+      BuildDate:
+        IPMIFruProperty: Mfg Date
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Revision:
+      Version:
+        IPMIFruProperty: Version
+        IPMIFruSection: Product
+1:
+  /system/chassis/motherboard/dimm0:
+    xyz.openbmc_project.Inventory.Item:
+      PrettyName:
+        IPMIFruProperty: Product Name
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Decorator.Asset:
+      Manufacturer:
+        IPMIFruProperty: Manufacturer
+        IPMIFruSection: Product
+      BuildDate:
+        IPMIFruProperty: Mfg Date
+        IPMIFruSection: Product
+      SerialNumber:
+        IPMIFruProperty: Serial Number
+        IPMIFruSection: Product
+      PartNumber:
+        IPMIFruProperty: Part Number
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Revision:
+      Version:
+        IPMIFruProperty: Version
+        IPMIFruSection: Product
+2:
+  /system/chassis/motherboard/dimm1:
+    xyz.openbmc_project.Inventory.Item:
+      PrettyName:
+        IPMIFruProperty: Product Name
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Decorator.Asset:
+      Manufacturer:
+        IPMIFruProperty: Manufacturer
+        IPMIFruSection: Product
+      BuildDate:
+        IPMIFruProperty: Mfg Date
+        IPMIFruSection: Product
+      SerialNumber:
+        IPMIFruProperty: Serial Number
+        IPMIFruSection: Product
+      PartNumber:
+        IPMIFruProperty: Part Number
+        IPMIFruSection: Product
+    xyz.openbmc_project.Inventory.Revision:
+      Version:
+        IPMIFruProperty: Version
+        IPMIFruSection: Product
+3:
+  /system/chassis/motherboard/cpu0:
+    xyz.openbmc_project.Inventory.Item:
+      PrettyName:
+        IPMIFruProperty: Product Name
+        IPMIFruSection: Board
+    xyz.openbmc_project.Inventory.Decorator.Asset:
+      BuildDate:
+        IPMIFruProperty: Mfg Date
+        IPMIFruSection: Board
+      SerialNumber:
+        IPMIFruProperty: Serial Number
+        IPMIFruSection: Board
+      PartNumber:
+        IPMIFruProperty: Part Number
+        IPMIFruSection: Board
+      Manufacturer:
+        IPMIFruProperty: Manufacturer
+        IPMIFruSection: Board
+4:
+  /system/chassis/motherboard/cpu1:
+    xyz.openbmc_project.Inventory.Item:
+      PrettyName:
+        IPMIFruProperty: Product Name
+        IPMIFruSection: Board
+    xyz.openbmc_project.Inventory.Decorator.Asset:
+      BuildDate:
+        IPMIFruProperty: Mfg Date
+        IPMIFruSection: Board
+      SerialNumber:
+        IPMIFruProperty: Serial Number
+        IPMIFruSection: Board
+      PartNumber:
+        IPMIFruProperty: Part Number
+        IPMIFruSection: Board
+      Manufacturer:
+        IPMIFruProperty: Manufacturer
+        IPMIFruSection: Board
diff --git a/scripts/fru_gen.py b/scripts/fru_gen.py
new file mode 100755
index 0000000..391bb5f
--- /dev/null
+++ b/scripts/fru_gen.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import yaml
+import argparse
+from mako.template import Template
+
+
+def generate_hpp(inventory_yaml, output_dir):
+    with open(os.path.join(script_dir, inventory_yaml), 'r') as f:
+        ifile = yaml.safe_load(f)
+
+        # Render the mako template
+
+        t = Template(filename=os.path.join(
+                     script_dir,
+                     "writefru.mako.hpp"))
+
+        output_hpp = os.path.join(output_dir, "fru-gen.hpp")
+        with open(output_hpp, 'w') as fd:
+            fd.write(t.render(fruDict=ifile))
+
+
+def main():
+
+    valid_commands = {
+        'generate-hpp': generate_hpp
+    }
+    parser = argparse.ArgumentParser(
+        description="IPMI FRU parser and code generator")
+
+    parser.add_argument(
+        '-i', '--inventory_yaml', dest='inventory_yaml',
+        default='example.yaml', help='input inventory yaml file to parse')
+
+    parser.add_argument(
+        "-o", "--output-dir", dest="outputdir",
+        default=".",
+        help="output directory")
+
+    parser.add_argument(
+        'command', metavar='COMMAND', type=str,
+        choices=valid_commands.keys(),
+        help='Command to run.')
+
+    args = parser.parse_args()
+
+    if (not (os.path.isfile(os.path.join(script_dir, args.inventory_yaml)))):
+        sys.exit("Can not find input yaml file " + args.inventory_yaml)
+
+    function = valid_commands[args.command]
+    function(args.inventory_yaml, args.outputdir)
+
+if __name__ == '__main__':
+    script_dir = os.path.dirname(os.path.realpath(__file__))
+    main()
diff --git a/scripts/writefru.mako.hpp b/scripts/writefru.mako.hpp
new file mode 100644
index 0000000..a5c2f24
--- /dev/null
+++ b/scripts/writefru.mako.hpp
@@ -0,0 +1,50 @@
+// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
+#pragma once
+
+#include <iostream>
+
+#include <string>
+#include <list>
+#include <map>
+
+using IPMIFruMetadata = std::string;
+using IPMIFruMetadataValue = std::string;
+using IPMIFruMap = std::map<IPMIFruMetadata,IPMIFruMetadataValue>;
+
+using DbusProperty = std::string;
+using DbusPropertyMap = std::map<DbusProperty,IPMIFruMap>;
+
+using DbusInterface = std::string;
+using DbusInterfaceMap = std::map<DbusInterface,DbusPropertyMap>;
+
+using FruInstancePath = std::string;
+using FruInstanceMap = std::map<FruInstancePath,DbusInterfaceMap>;
+
+using FruId = uint32_t;
+using FruMap = std::map<FruId,FruInstanceMap>;
+
+
+const FruMap frus = {
+% for key in fruDict.iterkeys():
+   {${key},{
+<%
+    fru = fruDict[key]
+%>
+    % for object,interfaces in fru.iteritems():
+         {"${object}",{
+         % for interface,properties in interfaces.iteritems():
+             {"${interface}",{
+            % for dbus_property,property_value in properties.iteritems():
+                 {"${dbus_property}",{
+                % for name,value in property_value.iteritems():
+                     {"${name}","${value}"},
+                % endfor
+                 }},
+            % endfor
+             }},
+         % endfor
+        }},
+    % endfor
+   }},
+% endfor
+};