sensor_yaml_config.py: Add option to fix sensorNamePattern

Change the --entity option to --fix, to fix both enityID and
sensorNamePattern, based on a pre-generated map.

Remove the unused getEntityId() and related data.

Tested: Based on Romulus ipmi sensor yaml, verify this tool fixes the
        entityID and sensorNamePattern.

Change-Id: I3a9239f110d6dcab8c850194607d993acb55a8aa
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 f68e132..65a0a3b 100755
--- a/leiyu/obmc-utils/sensor_yaml_config.py
+++ b/leiyu/obmc-utils/sensor_yaml_config.py
@@ -16,37 +16,6 @@
     targetPath: str
 
 
-entityIds = {
-    'dimm': 32,
-    'core': 208,
-    'cpu': 3,
-    'occ': 210,
-    '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,
-    'control/volatile': 33,
-}
-
-extraIds = {
-    'RebootPolicy': 33,
-    'Progress': 34,
-    'RebootAttempts': 34,
-    'OperatingSystem.Status': 35
-}
-
-
 sampleDimmTemp = {
     'bExp': 0,
     'entityID': 32,
@@ -119,18 +88,12 @@
         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)
+def getEntityIdAndNamePattern(p, intfs, m):
+    key = (p, intfs)
+    match = m.get(key, None)
+    if match is None:
+        raise Exception('Unable to find sensor', key, 'from map')
+    return (m[key]['entityID'], m[key]['sensorNamePattern'])
 
 
 # Global entity instances
@@ -206,10 +169,12 @@
                         help='The ipmi sensor yaml config')
     parser.add_argument('-o', '--output', required=True, dest='output',
                         help='The output yaml file')
+    parser.add_argument('-m', '--map', dest='map', default='sensor_map.yaml',
+                        help='The sample map 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')
+    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')
 
@@ -222,14 +187,17 @@
 
     y = openYaml(args['input'])
 
-    if args['entity']:
-        # Fix entities
+    if args['fix']:
+        # Fix entities and sensorNamePattern
+        m = openYaml(args['map'])
+
         for i in y:
             path = y[i]['path']
-            intf = list(y[i]['interfaces'].keys())[0]
-            entityId = getEntityId(path, intf)
+            intfs = tuple(sorted(list(y[i]['interfaces'].keys())))
+            entityId, namePattern = getEntityIdAndNamePattern(path, intfs, m)
             y[i]['entityID'] = entityId
             y[i]['entityInstance'] = getEntityInstance(entityId)
+            y[i]['sensorNamePattern'] = namePattern
             print(y[i]['path'], "id:", entityId,
                   "instance:", y[i]['entityInstance'])