Simulate vtep when baremetal_tor is defined

This patch allows to connect ironic VMs to the ovs switch
and setup vtep simulator that will allow to use it by
openctonrail.

Change-Id: I03d793cef99ac4486848b87c0024be5ca3849f12
diff --git a/baremetal_simulator/files/openvswitch/openvswitch-vtep.default b/baremetal_simulator/files/openvswitch/openvswitch-vtep.default
new file mode 100644
index 0000000..a10b0ca
--- /dev/null
+++ b/baremetal_simulator/files/openvswitch/openvswitch-vtep.default
@@ -0,0 +1,5 @@
+# This is a POSIX shell fragment                -*- sh -*-
+
+# ENABLE_OVS_VTEP: Whether to start ovs-vtep.
+ENABLE_OVS_VTEP="true"
+OVSDB_SERVER_OPTS="--remote=ptcp:6632"
diff --git a/baremetal_simulator/files/openvswitch/openvswitch-vtep.init b/baremetal_simulator/files/openvswitch/openvswitch-vtep.init
new file mode 100644
index 0000000..681d886
--- /dev/null
+++ b/baremetal_simulator/files/openvswitch/openvswitch-vtep.init
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+### BEGIN INIT INFO
+# Provides:          openvswitch-vtep
+# Required-Start:    $network $named $remote_fs $syslog
+# Required-Stop:     $remote_fs
+# Default-Start:     2 3 4 5
+# Default-Stop:      0 1 6
+# Short-Description: Open vSwitch VTEP emulator
+# Description:       Initializes the Open vSwitch VTEP emulator
+### END INIT INFO
+
+_SYSTEMCTL_SKIP_REDIRECT=yes
+
+. /lib/lsb/init-functions
+
+# Include defaults if available
+default=/etc/default/openvswitch-vtep
+if [ -f $default ] ; then
+    . $default
+fi
+
+start () {
+    if [ "$ENABLE_OVS_VTEP" = "false" ]; then
+        exit 0
+    fi
+
+    update-rc.d -f openvswitch-switch remove >/dev/null 2>&1
+    /etc/init.d/openvswitch-switch stop
+
+    mkdir -p "/var/run/openvswitch"
+
+    if [ ! -e "/etc/openvswitch/conf.db" ]; then
+        ovsdb-tool create /etc/openvswitch/conf.db /usr/share/openvswitch/vswitch.ovsschema
+    fi
+
+    if [ ! -e "/etc/openvswitch/vtep.db" ]; then
+        ovsdb-tool create /etc/openvswitch/vtep.db /usr/share/openvswitch/vtep.ovsschema
+    fi
+
+    if [ ! -e "/etc/openvswitch/ovsclient-cert.pem" ]; then
+        export RANDFILE="/root/.rnd"
+        cd /etc/openvswitch && ovs-pki req ovsclient && ovs-pki self-sign ovsclient
+    fi
+
+    ovsdb-server --pidfile --detach --log-file --remote \
+        punix:/var/run/openvswitch/db.sock \
+        --remote=db:hardware_vtep,Global,managers \
+        --private-key=/etc/openvswitch/ovsclient-privkey.pem \
+        --certificate=/etc/openvswitch/ovsclient-cert.pem \
+        --bootstrap-ca-cert=/etc/openvswitch/vswitchd.cacert \
+        /etc/openvswitch/conf.db /etc/openvswitch/vtep.db \
+        $OVSDB_SERVER_OPTS
+
+    modprobe openvswitch
+
+    ovs-vswitchd --pidfile --detach --log-file \
+        unix:/var/run/openvswitch/db.sock
+}
+
+stop () {
+    /etc/init.d/openvswitch-switch stop
+}
+
+case $1 in
+    start)
+        start
+        ;;
+    stop)
+        stop
+        ;;
+    restart|force-reload)
+        stop
+        start
+        ;;
+    *)
+        echo "Usage: $0 {start|stop|restart|force-reload}" >&2
+        exit 1
+        ;;
+esac
+
+exit 0
diff --git a/baremetal_simulator/init.sls b/baremetal_simulator/init.sls
index 1cfdfa4..36247e6 100644
--- a/baremetal_simulator/init.sls
+++ b/baremetal_simulator/init.sls
@@ -11,6 +11,64 @@
     - require:
       - simulator_pkgs
 
+{%- if baremetal_simulator.get('tor', {}).get('enabled', False) %}
+
+openvswitch-vtep-pkg:
+  pkg.installed:
+    - name: openvswitch-vtep
+
+openvswitch-vtep.init:
+  file.managed:
+    - name: /etc/init.d/openvswitch-vtep
+    - source: salt://baremetal_simulator/files/openvswitch/openvswitch-vtep.init
+    - require:
+      - openvswitch-vtep-pkg
+
+openvswitch-vtep.default:
+  file.managed:
+    - name: /etc/default/openvswitch-vtep
+    - source: salt://baremetal_simulator/files/openvswitch/openvswitch-vtep.default
+    - require:
+      - openvswitch-vtep-pkg
+
+openvswitch-vtep:
+  service.running:
+    - watch:
+      - file: /etc/init.d/openvswitch-vtep
+      - file: /etc/default/openvswitch-vtep
+    - require:
+      - openvswitch-vtep.init
+      - openvswitch-vtep.default
+
+br_simulator_ps_present:
+  cmd.run:
+    - name: vtep-ctl add-ps br-simulator
+    - unless: vtep-ctl ps-exists br-simulator
+
+br_simulator_vtep_tunnel:
+  cmd.run:
+    - name: vtep-ctl set Physical_Switch br-simulator tunnel_ips={{ baremetal_simulator.tor.tunnel_ips }}
+    - unless: vtep-ctl show br-baremetal | grep -q "tunnel_ips.*{{ baremetal_simulator.tor.tunnel_ips }}"
+    - require:
+      - br_simulator_ps_present
+
+br_simulator_vtep_management:
+  cmd.run:
+    - name: vtep-ctl set Physical_Switch br-simulator management_ips={{ baremetal_simulator.tor.tunnel_ips }}
+    - unless: vtep-ctl show br-baremetal | grep -q "management_ips.*{{ baremetal_simulator.tor.tunnel_ips }}"
+    - require:
+      - br_simulator_ps_present
+
+br_simulator_vtep:
+  cmd.run:
+    - name: /usr/share/openvswitch/scripts/ovs-vtep --log-file=/var/log/openvswitch/ovs-vtep.log --pidfile=/var/run/openvswitch/ovs-vtep.pid --detach br-simulator
+    - unless: ps xafu |grep -v grep |grep -q "ovs-vtep .* br-simulator"
+    - require:
+      - br_simulator_vtep_management
+      - br_simulator_vtep_tunnel
+
+{%- endif %}
+
 libvirt-bin:
   service.running:
     - watch:
diff --git a/metadata/service/tor.yml b/metadata/service/tor.yml
new file mode 100644
index 0000000..c0b5308
--- /dev/null
+++ b/metadata/service/tor.yml
@@ -0,0 +1,6 @@
+parameters:
+  baremetal_simulator:
+    tor:
+      enabled: true
+      management_ips: ${_param:single_baremetal_address}
+      tunnel_ips: ${_param:single_baremetal_address}