sensor_yaml_config.py: Add --dcmi option

This option is used to generate a json config which is part of IPMI
dcmi sensors for cpu core temperatures.

Tested: Using Romulus yaml, generate a json config, verify it could
        be parsed by ipmi daemon, and check ipmitool dcmi
        get_temp_reading gets cpu core temperatures.

Change-Id: Idd1a74158f7e59b47f154b71fc4d108a60d24f67
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
index 65a0a3b..8db1303 100755
--- a/leiyu/obmc-utils/sensor_yaml_config.py
+++ b/leiyu/obmc-utils/sensor_yaml_config.py
@@ -72,6 +72,11 @@
     'serviceInterface': 'org.freedesktop.DBus.Properties',
     'unit': 'xyz.openbmc_project.Sensor.Value.Unit.DegreesC'
 }
+sampleDcmiSensor = {
+    "instance": 1,
+    "dbus": "/xyz/openbmc_project/sensors/temperature/p0_core0_temp",
+    "record_id": 91
+}
 
 
 def openYaml(f):
@@ -162,6 +167,32 @@
     return r
 
 
+def isCoreTemp(p):
+    import re
+    m = re.search(r'p\d+_core\d+_temp', p)
+    return m is not None
+
+
+def getDcmiSensor(i, sensor):
+    import re
+    path = sensor['path']
+    name = path.split('/')[-1]
+    m = re.findall(r'\d+', name)
+    socket, core = int(m[0]), int(m[1])
+    instance = socket * 24 + core + 1
+    r = sampleDcmiSensor.copy()
+    r['instance'] = instance
+    r['dbus'] = path
+    r['record_id'] = i
+    return r
+
+
+def saveJson(data, f):
+    import json
+    with open(f, 'w') as outfile:
+        json.dump(data, outfile, indent=4)
+
+
 def main():
     parser = argparse.ArgumentParser(
         description='Yaml tool for updating ipmi sensor yaml config')
@@ -175,8 +206,16 @@
                         help='The .rpt file generated by op-build')
     parser.add_argument('-f', '--fix', action='store_true',
                         help='Fix entities and sensorNamePattern')
-    parser.add_argument('-g', '--generate', action='store_true',
-                        help='Generate maps for entityID and sensorNamePattern')
+
+    # -g expects output as yaml for mapping of entityID/sensorNamePattern
+    # -d expects output as json config for dcmi sensors
+    # Do not mess the output by enforcing only one argument is passed
+    # TODO: -f and -r could be used together, and they are conflicted with -g or -d
+    group = parser.add_mutually_exclusive_group()
+    group.add_argument('-g', '--generate', action='store_true',
+                       help='Generate maps for entityID and sensorNamePattern')
+    group.add_argument('-d', '--dcmi', action='store_true',
+                       help='Generate dcmi sensors json config')
 
     args = parser.parse_args()
     args = vars(args)
@@ -229,8 +268,17 @@
                                 'sensorNamePattern': sensorNamePattern}
         y = m
 
-    safe = False if args['generate'] else True
+    if args['dcmi']:
+        d = []
+        for i in y:
+            if isCoreTemp(y[i]['path']):
+                s = getDcmiSensor(i, y[i])
+                d.append(s)
+                print(s)
+        saveJson(d, args['output'])
+        return
 
+    safe = False if args['generate'] else True
     saveYaml(y, args['output'], safe)