multiple-channel configuration via yaml

Handles converting a yaml file mapping IPMI
channel to ethernet device and using this instead of
a hard-coded map.

Change-Id: Iedfe7cb52a2d0663b9c8a0f6f9d37fe733b63a58
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/Makefile.am b/Makefile.am
index 99808ab..8190320 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -15,7 +15,8 @@
                ipmiwhitelist.cpp \
                sensor-gen.cpp \
                inventory-sensor-gen.cpp \
-               fru-read-gen.cpp
+               fru-read-gen.cpp \
+               channel-gen.cpp
 
 CLEANFILES = $(BUILT_SOURCES)
 
@@ -46,6 +47,9 @@
 fru-read-gen.cpp:
 	$(AM_V_GEN)@FRUGEN@ -o $(top_builddir) generate-cpp
 
+channel-gen.cpp:
+	$(AM_V_GEN)@CHANNELGEN@ -o $(top_builddir) generate-cpp
+
 libapphandlerdir = ${libdir}/ipmid-providers
 libapphandler_LTLIBRARIES = libapphandler.la
 libapphandler_la_SOURCES = \
diff --git a/configure.ac b/configure.ac
index a041971..4d52659 100644
--- a/configure.ac
+++ b/configure.ac
@@ -88,6 +88,10 @@
 FRUGEN="$PYTHON $srcdir/scripts/fru_gen.py -i $FRU_YAML_GEN"
 AC_SUBST(FRUGEN)
 
+AS_IF([test "x$CHANNEL_YAML_GEN" == "x"], [CHANNEL_YAML_GEN="channel-example.yaml"])
+CHANNELGEN="$PYTHON ${srcdir}/scripts/channel_gen.py -i $CHANNEL_YAML_GEN"
+AC_SUBST(CHANNELGEN)
+
 AC_DEFINE(CALLOUT_FWD_ASSOCIATION, "callout", [The name of the callout's forward association.])
 AC_DEFINE(BOARD_SENSOR, "/xyz/openbmc_project/inventory/system/chassis/motherboard", [The inventory path to the motherboard fault sensor.])
 AC_DEFINE(SYSTEM_SENSOR, "/xyz/openbmc_project/inventory/system", [The inventory path to the system event sensor.])
diff --git a/net.cpp b/net.cpp
index 6c492a2..17be41e 100644
--- a/net.cpp
+++ b/net.cpp
@@ -1,6 +1,8 @@
 #include <map>
 #include <string>
 
+#include "utils.hpp"
+
 // Not sure if this should live in utils.  Because it's really a per-system
 // configuration, instead of just hard-coding channel 1 to be eth0, one could
 // conceivably configure it however they pleased.
@@ -12,23 +14,14 @@
 namespace network
 {
 
-// This map should come from a configuration yaml.
-// Also, no need to really be a map, could be just an array
-// we index into by channel. :D
-std::map<int, std::string> ethDeviceMap = {
-    {1, "eth0"},
-    {2, "eth1"},
-};
-
+extern const ipmi::network::ChannelEthMap ethdevices;
 
 // Given a channel number, return a matching ethernet device, or empty string
 // if there is no match.
-// TODO provide this from a configuration:
-// https://github.com/openbmc/openbmc/issues/2667
 std::string ChanneltoEthernet(int channel)
 {
-    auto dev = ethDeviceMap.find(channel);
-    if (dev == ethDeviceMap.end())
+    auto dev = ethdevices.find(channel);
+    if (dev == ethdevices.end())
     {
         return "";
     }
diff --git a/scripts/channel-example.yaml b/scripts/channel-example.yaml
new file mode 100644
index 0000000..819f251
--- /dev/null
+++ b/scripts/channel-example.yaml
@@ -0,0 +1,6 @@
+# Channel Number (must be unique) is the key
+1:
+  # ifName the ethernet device name (used in the dbus path)
+  ifName: eth0
+2:
+  ifName: eth1
diff --git a/scripts/channel_gen.py b/scripts/channel_gen.py
new file mode 100755
index 0000000..77fbd2e
--- /dev/null
+++ b/scripts/channel_gen.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import yaml
+import argparse
+from mako.template import Template
+
+
+def generate_cpp(interface_yaml, output_dir):
+    with open(os.path.join(script_dir, interface_yaml), 'r') as f:
+        ifile = yaml.safe_load(f)
+        if not isinstance(ifile, dict):
+            ifile = {}
+
+        # Render the mako template
+
+        t = Template(filename=os.path.join(
+                     script_dir,
+                     "writechannel.mako.cpp"))
+
+        output_cpp = os.path.join(output_dir, "channel-gen.cpp")
+        with open(output_cpp, 'w') as fd:
+            fd.write(t.render(interfaceDict=ifile))
+
+
+def main():
+
+    valid_commands = {
+        'generate-cpp': generate_cpp
+    }
+    parser = argparse.ArgumentParser(
+        description="IPMI ethernet interface parser and code generator")
+
+    parser.add_argument(
+        '-i', '--interface_yaml', dest='interface_yaml',
+        default='example.yaml', help='input sensor 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.interface_yaml)))):
+        sys.exit("Can not find input yaml file " + args.interface_yaml)
+
+    function = valid_commands[args.command]
+    function(args.interface_yaml, args.outputdir)
+
+
+if __name__ == '__main__':
+    script_dir = os.path.dirname(os.path.realpath(__file__))
+    main()
diff --git a/scripts/writechannel.mako.cpp b/scripts/writechannel.mako.cpp
new file mode 100644
index 0000000..922611a
--- /dev/null
+++ b/scripts/writechannel.mako.cpp
@@ -0,0 +1,20 @@
+## This file is a template.  The comment below is emitted
+## into the rendered file; feel free to edit this file.
+// !!! WARNING: This is a GENERATED Code..Please do NOT Edit !!!
+
+#include "types.hpp"
+
+namespace ipmi
+{
+namespace network
+{
+
+extern const ChannelEthMap ethdevices = {
+% for channel,channelInfo in interfaceDict.iteritems():
+    {${channel},"${channelInfo['ifName']}"},
+% endfor
+};
+
+} // namespace network
+} // namespace ipmi
+
diff --git a/types.hpp b/types.hpp
index a43c8c0..62f7db7 100644
--- a/types.hpp
+++ b/types.hpp
@@ -181,6 +181,7 @@
 
 namespace network
 {
+using ChannelEthMap = std::map<int, std::string>;
 
 constexpr auto MAC_ADDRESS_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
 constexpr auto IP_ADDRESS_FORMAT = "%u.%u.%u.%u";