Add sensor_yaml_config.py for ipmi sensor yaml configs

Existing ipmi sensor yaml configs are missing certain parts, e.g.
entityID, entityInstance, etc.
This script helps to fix the missing parts.

The initial version only add the fix for entityID and entityInstance, in
future it is expected to generate the whole ipmi sensor yaml config from
rpt file, which is generated by op-build.

Tested: Use Romulus ipmi sensor yaml config as input, verify the output
        yaml config passes the build and the entityID/entityInstance is
        OK.

Change-Id: Ia345db82254cdc1f9359d21aed44927a24ddd26f
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/leiyu/obmc-utils/sensor_yaml_config.py b/leiyu/obmc-utils/sensor_yaml_config.py
new file mode 100755
index 0000000..a5aa222
--- /dev/null
+++ b/leiyu/obmc-utils/sensor_yaml_config.py
@@ -0,0 +1,111 @@
+#!/usr/bin/env python3
+
+import yaml
+import argparse
+
+entityIds = {
+    'dimm': 32,
+    'core': 208,
+    'cpu': 3,
+    'gpu': 216,
+    'gpu_mem': 217,
+    'tpm': 3,
+    'state/host0': 33,  # Different interfaces using different entity ID
+    # and this requires extra fix.
+    #    {'state/host0', 34},
+    #    {'state/host0', 35},
+    'turbo': 3,
+    'fan': 29,
+    'vdd_temp': 218,
+    'power': 10,
+    'voltage': 10,
+    'current': 10,
+    'temperature/pcie': 35,
+    'temperature/ambient': 64,
+    'occ': 210,
+    'control/volatile': 33,
+}
+
+extraIds = {
+    'RebootPolicy': 33,
+    'Progress': 34,
+    'RebootAttempts': 34,
+    'OperatingSystem.Status': 35
+}
+
+
+def openYaml(f):
+    return yaml.load(open(f))
+
+
+def saveYaml(y, f):
+    yaml.dump(y, open(f, "w"))
+
+
+def getEntityId(p, i):
+    for k, v in entityIds.items():
+        if k in p:
+            if k == 'state/host0':
+                # get id from extraIds
+                for ek, ev in extraIds.items():
+                    if ek in i:
+                        return ev
+                raise Exception("Unable to find entity id:", p, i)
+            else:
+                return v
+    raise Exception('Unable to find entity id:', p)
+
+
+# Global entity instances
+entityInstances = {}
+
+
+def getEntityInstance(id):
+    instanceId = entityInstances.get(id, 0)
+    instanceId = instanceId + 1
+    entityInstances[id] = instanceId
+    print("EntityId:", id, "InstanceId:", instanceId)
+    return instanceId
+
+
+def main():
+    parser = argparse.ArgumentParser(
+        description='Yaml tool for updating ipmi sensor yaml config')
+    parser.add_argument('-i', '--input', required=True, dest='input',
+                        help='The ipmi sensor yaml config')
+    parser.add_argument('-o', '--output', required=True, dest='output',
+                        help='The output yaml file')
+    parser.add_argument('-r', '--rpt', dest='rpt',
+                        help='The .rpt file generated by op-build')
+    parser.add_argument('-e', '--entity', action='store_true',
+                        help='Fix entities')
+
+    args = parser.parse_args()
+    args = vars(args)
+
+    if args['input'] is None or args['output'] is None:
+        parser.print_help()
+        exit(1)
+
+# TODO: read rtp and generate the whole config yaml
+#    if args['rpt']:
+#        pass
+
+    y = openYaml(args['input'])
+
+    if args['entity']:
+        # Fix entities
+        for i in y:
+            path = y[i]['path']
+            intf = list(y[i]['interfaces'].keys())[0]
+            entityId = getEntityId(path, intf)
+            y[i]['entityID'] = entityId
+            y[i]['entityInstance'] = getEntityInstance(entityId)
+            print(y[i]['path'], "id:", entityId,
+                  "instance:", y[i]['entityInstance'])
+
+    saveYaml(y, args['output'])
+
+
+if __name__ == "__main__":
+    main()