Enabling the avahi service.

Regsitering the following services with avahi.
obmc-console,ssh,sftp(dropbear),obmc-rest

Fixes openbmc/openbmc#548
Fixes openbmc/openbmc#549

Change-Id: I20c31e9e8dfa6d52575bc3c1f55ac62eef82ec13
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/meta-phosphor/classes/obmc-phosphor-discovery-service.bbclass b/meta-phosphor/classes/obmc-phosphor-discovery-service.bbclass
new file mode 100644
index 0000000..4c9db0f
--- /dev/null
+++ b/meta-phosphor/classes/obmc-phosphor-discovery-service.bbclass
@@ -0,0 +1,76 @@
+inherit obmc-phosphor-utils
+DISCOVERY_SVC_PACKAGES ?= "${PN}"
+
+python() {
+    avahi_enabled = bb.utils.contains(
+            'DISTRO_FEATURES', 'avahi', 'true', 'false', d)
+    slp_enabled = False # later
+
+    if not avahi_enabled and slp_enabled:
+        return
+
+    syscnfdir = d.getVar('sysconfdir', True)
+    dest_dir = d.getVar('D', True)
+    set_append(d, 'AVAHI_SERVICES_DIR', os.path.join(
+        dest_dir+syscnfdir,
+        'avahi',
+        'services'))
+
+    for pkg in listvar_to_list(d, 'DISCOVERY_SVC_PACKAGES'):
+        for service in listvar_to_list(d, 'REGISTERED_SERVICES_%s' % pkg):
+            if avahi_enabled:
+                set_append(d, 'RRECOMMENDS_%s' % pkg, 'avahi-daemon')
+                svc_name, svc_type, svc_port = service.split(':')
+                set_append(d, 'FILES_%s' % pkg, os.path.join(
+                    syscnfdir,
+                    'avahi',
+                    'services',
+                    '%s.service' % svc_name))
+}
+
+python discovery_services_postinstall() {
+    avahi_enabled = bb.utils.contains(
+            'DISTRO_FEATURES', 'avahi', 'true', 'false', d)
+    slp_enabled = False # later
+
+    if not avahi_enabled and slp_enabled:
+        return
+
+    service_dir = d.getVar('AVAHI_SERVICES_DIR', True)
+
+    if not os.path.exists(service_dir):
+        os.makedirs(service_dir)
+
+    def register_service_avahi(d, service_name, service_type, service_port):
+        service_file = os.path.join(
+            service_dir,
+            '%s.service' % service_name)
+        with open(service_file, 'w') as fd:
+            fd.write('<?xml version="1.0" ?>\n')
+            fd.write('<!DOCTYPE service-group SYSTEM "avahi-service.dtd">\n')
+            fd.write('<service-group>\n')
+            fd.write('        <name>"%s"</name>\n' % service_name)
+            fd.write('        <service>\n')
+            fd.write('                <type>"%s"</type>\n' % service_type)
+            fd.write('                <port>"%s"</port>\n' % service_port)
+            fd.write('        </service>\n')
+            fd.write('</service-group>\n')
+
+    def register_services(d,pkg):
+        for service in listvar_to_list(d, 'REGISTERED_SERVICES_%s' % pkg):
+            svc_info = service.split(":")
+            try:
+                svc_name, svc_type, svc_port = svc_info
+                svc_type = "_" + svc_name + "._" + svc_type
+            except:
+                continue
+            if avahi_enabled:
+                register_service_avahi(d, svc_name, svc_type, svc_port)
+
+    for pkg in listvar_to_list(d, 'DISCOVERY_SVC_PACKAGES'):
+        register_services(d, pkg)
+
+}
+do_install[postfuncs] += "discovery_services_postinstall"
+
+
diff --git a/meta-phosphor/common/recipes-core/dropbear/dropbear_%.bbappend b/meta-phosphor/common/recipes-core/dropbear/dropbear_%.bbappend
index 52a38bd..51a98d4 100644
--- a/meta-phosphor/common/recipes-core/dropbear/dropbear_%.bbappend
+++ b/meta-phosphor/common/recipes-core/dropbear/dropbear_%.bbappend
@@ -1,3 +1,7 @@
+inherit obmc-phosphor-discovery-service
+
 FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
 SRC_URI += "file://dropbearkey.service \
 	    file://0001-dropbear-Add-c-command-option-to-force-a-specific-co.patch"
+
+REGISTERED_SERVICES_${PN} += "ssh:tcp:22 sftp:tcp:22"
diff --git a/meta-phosphor/common/recipes-phosphor/console/obmc-console.bb b/meta-phosphor/common/recipes-phosphor/console/obmc-console.bb
index 36c7403..f6c1db2 100644
--- a/meta-phosphor/common/recipes-phosphor/console/obmc-console.bb
+++ b/meta-phosphor/common/recipes-phosphor/console/obmc-console.bb
@@ -6,6 +6,7 @@
 inherit obmc-phosphor-license
 inherit obmc-phosphor-systemd
 inherit autotools
+inherit obmc-phosphor-discovery-service
 
 TARGET_CFLAGS   += "-fpic -O2"
 
@@ -16,6 +17,8 @@
 
 SRCREV = "44580de4e2170c8ee06dbf401315d3acfcf52b22"
 
+REGISTERED_SERVICES_${PN} += "obmc_console:tcp:2200"
+
 SYSTEMD_SERVICE_${PN} = " \
         ${PN}.service \
         ${PN}-ssh.socket \
diff --git a/meta-phosphor/common/recipes-phosphor/interfaces/phosphor-rest.bb b/meta-phosphor/common/recipes-phosphor/interfaces/phosphor-rest.bb
index a8f82cb..8df4ca0 100644
--- a/meta-phosphor/common/recipes-phosphor/interfaces/phosphor-rest.bb
+++ b/meta-phosphor/common/recipes-phosphor/interfaces/phosphor-rest.bb
@@ -8,6 +8,7 @@
 inherit allarch
 inherit obmc-phosphor-systemd
 inherit setuptools
+inherit obmc-phosphor-discovery-service
 
 RRECOMMENDS_${PN} += "virtual-obmc-wsgihost"
 
@@ -29,3 +30,4 @@
 SYSTEMD_SERVICE_${PN} = ""
 SYSTEMD_OVERRIDE_${PN} += "rest-dbus.conf:obmc-mapper.target.d/rest-dbus.conf"
 SYSTEMD_ENVIRONMENT_FILE_${PN} += "obmc/wsgi_app"
+REGISTERED_SERVICES_${PN} += "phosphor_rest:tcp:443"
diff --git a/meta-phosphor/conf/distro/openbmc-phosphor.conf b/meta-phosphor/conf/distro/openbmc-phosphor.conf
index 0b62783..2f2916c 100644
--- a/meta-phosphor/conf/distro/openbmc-phosphor.conf
+++ b/meta-phosphor/conf/distro/openbmc-phosphor.conf
@@ -29,6 +29,7 @@
         obmc-settings-mgmt \
         obmc-phosphor-system-mgmt \
         obmc-host-ipmi \
+        avahi \
         ${DISTRO_FEATURES_LIBC} \
         "