Add neutron/upgrade tasks

This commit adds neutron/upgrade:
  pre.sls: Stop API/Render Configuration
  upgrade.sls: Install latest package
  post.sls: Start service

Change-Id: I5c1bfd819e7d9fa2ec2ba937e4ee644cd4b77370
Related-Prod: PROD-21927
diff --git a/_states/neutronv2.py b/_states/neutronv2.py
index 42e202b..0c00c22 100644
--- a/_states/neutronv2.py
+++ b/_states/neutronv2.py
@@ -147,6 +147,49 @@
         return _failed('find', name, 'agent')
 
 
+def agents_disabled(name, cloud_name, **kwargs):
+    """
+    :param name: agent host name
+    :param kwargs:
+        :param description: agent description
+        :param admin_state_up: administrative state of the agent
+    """
+    agents = _neutronv2_call(
+        'agent_list', host=name, cloud_name=cloud_name)['agents']
+
+    changes = {}
+    for agent in agents:
+      if agent['admin_state_up'] == True:
+        try:
+          changes[agent['id']] = _neutronv2_call('agent_update', agent_id=agent['id'],
+                                                 cloud_name=cloud_name, admin_state_up=False)
+        except Exception:
+          return _failed('update', name, 'agent')
+    return _succeeded('update', name, 'agent',changes)
+
+
+def agents_enabled(name, cloud_name, **kwargs):
+    """
+    :param name: agent host name
+    :param kwargs:
+        :param description: agent description
+        :param admin_state_up: administrative state of the agent
+    """
+    agents = _neutronv2_call(
+        'agent_list', host=name, cloud_name=cloud_name)['agents']
+
+    changes = {}
+    for agent in agents:
+      if agent['admin_state_up'] == False:
+        try:
+          changes[agent['id']] = _neutronv2_call('agent_update', agent_id=agent['id'],
+                                                 cloud_name=cloud_name, admin_state_up=True)
+        except Exception:
+          return _failed('update', name, 'agent')
+
+    return _succeeded('update', name, 'agent', changes)
+
+
 def _succeeded(op, name, resource, changes=None):
     msg_map = {
         'create': '{0} {1} created',
diff --git a/neutron/map.jinja b/neutron/map.jinja
index e8becfc..13b4bd6 100644
--- a/neutron/map.jinja
+++ b/neutron/map.jinja
@@ -2,7 +2,8 @@
     'cacert_file': salt['grains.filter_by']({
         'Debian': '/etc/ssl/certs/ca-certificates.crt',
         'RedHat': '/etc/pki/tls/certs/ca-bundle.crt'
-    })}
+    }),
+    'enabled': false }
 %}
 
 {% set compute = salt['grains.filter_by']({
@@ -151,10 +152,12 @@
 
 {% set client = salt['grains.filter_by']({
     'Debian': {
-        'pkgs': ['python-neutronclient']
+        'pkgs': ['python-neutronclient'],
+        'enabled': false
     },
     'RedHat': {
-        'pkgs': ['python-neutronclient']
+        'pkgs': ['python-neutronclient'],
+        'enabled': false
     },
 }, merge=pillar.neutron.get('client', {})) %}
 
diff --git a/neutron/meta/salt.yml b/neutron/meta/salt.yml
index ca0502a..a6fba47 100644
--- a/neutron/meta/salt.yml
+++ b/neutron/meta/salt.yml
@@ -1,11 +1,5 @@
-orchestrate:
-  server:
-    priority: 580
-    batch: 1
-    require:
-    - salt: keystone.server
-  compute:
-    priority: 590
-    require:
-    - salt: neutron.server
-
+orchestration:
+  upgrade:
+    applications:
+      neutron:
+        priority: 1150
diff --git a/neutron/upgrade/pkgs_latest.sls b/neutron/upgrade/pkgs_latest.sls
new file mode 100644
index 0000000..6023128
--- /dev/null
+++ b/neutron/upgrade/pkgs_latest.sls
@@ -0,0 +1,62 @@
+{%- from "neutron/map.jinja" import server,client,gateway,compute,fwaas with context %}
+
+neutron_task_pkg_latest:
+  test.show_notification:
+    - text: "Running neutron.upgrade.pkg_latest"
+
+policy-rc.d_present:
+  file.managed:
+    - name: /usr/sbin/policy-rc.d
+    - mode: 755
+    - contents: |
+        #!/bin/sh
+        exit 101
+
+{%- set npkgs = [] %}
+{%- if server.enabled %}
+  {%- do npkgs.extend(server.pkgs) %}
+  {% if server.backend.engine == "contrail" %}
+    {%- do npkgs.append('neutron-plugin-contrail') %}
+  {% elif server.backend.engine == "ml2" %}
+    {%- do npkgs.extend(server.pkgs_ml2) %}
+  {%- elif server.backend.get('opendaylight', False) %}
+    {%- do npkgs.append('python-networking-odl') %}
+  {%- elif server.backend.engine == "ovn" %}
+    {%- do npkgs.extend(server.pkgs_ovn) %}
+  {%- elif server.backend.engine == "midonet" %}
+    {%- if server.version == "kilo" %}
+      {%- do npkgs.extend(['python-neutron-plugin-midonet', 'python-neutron-lbaas']) %}
+    {%- else %}
+      {%- do npkgs.extend(['python-networking-midonet', 'python-neutron-lbaas', 'python-neutron-fwaas']) %}
+    {%- endif %}
+  {% elif server.backend.engine == "vmware" %}
+    {%- do npkgs.append('python-vmware-nsx') %}
+  {%- endif %}
+  {% if server.get('bgp_vpn', {}).get('enabled', False) %}
+    {%- do npkgs.extend(server.pkgs_bagpipe) %}
+  {%- endif %}
+  {%- if fwaas.get('enabled', False) %}
+    {%- do npkgs.extend(fwaas.pkgs) %}
+  {%- endif %}
+{%- endif %}
+{%- if gateway.enabled is defined and gateway.enabled %}
+  {%- do npkgs.extend(gateway.pkgs) %}
+{%- endif %}
+{%- if compute.enabled is defined and compute.enabled %}
+  {%- do npkgs.extend(compute.pkgs) %}
+{%- endif %}
+{%- if client.enabled is defined and client.enabled %}
+  {%- do npkgs.extend(client.pkgs) %}
+{%- endif %}
+
+neutron_pkg_latest:
+  pkg.latest:
+    - names: {{ npkgs|unique }}
+    - require:
+      - file: policy-rc.d_present
+    - require_in:
+      - file: policy-rc.d_absent
+
+policy-rc.d_absent:
+  file.absent:
+    - name: /usr/sbin/policy-rc.d
diff --git a/neutron/upgrade/post/init.sls b/neutron/upgrade/post/init.sls
new file mode 100644
index 0000000..4f06e22
--- /dev/null
+++ b/neutron/upgrade/post/init.sls
@@ -0,0 +1,11 @@
+{%- from "neutron/map.jinja" import server,gateway with context %}
+
+neutron_post:
+  test.show_notification:
+    - text: "Running neutron.upgrade.post"
+
+{%- if gateway.get('enabled') %}
+keystone_os_client_config_absent:
+  file.absent:
+    - name: /etc/openstack/clouds.yml
+{%- endif %}
diff --git a/neutron/upgrade/pre/init.sls b/neutron/upgrade/pre/init.sls
new file mode 100644
index 0000000..55e654d
--- /dev/null
+++ b/neutron/upgrade/pre/init.sls
@@ -0,0 +1,24 @@
+{%- from "neutron/map.jinja" import server,gateway with context %}
+
+include:
+ - neutron.upgrade.verify.api
+
+neutron_pre:
+  test.show_notification:
+    - text: "Running neutron.upgrade.pre"
+
+{%- if gateway.get('enabled') %}
+{# Get os client config from mine #}
+
+{%- set os_content = salt['mine.get']('I@keystone:client:os_client_config:enabled:true', 'keystone_os_client_config', 'compound').values()[0] %}
+
+keystone_os_client_config:
+  file.managed:
+    - name: /etc/openstack/clouds.yml
+    - contents: |
+        {{ os_content |yaml(False)|indent(8) }}
+    - user: 'root'
+    - group: 'root'
+    - makedirs: True
+
+{%- endif %}
diff --git a/neutron/upgrade/render_config.sls b/neutron/upgrade/render_config.sls
new file mode 100644
index 0000000..1f5acaa
--- /dev/null
+++ b/neutron/upgrade/render_config.sls
@@ -0,0 +1,61 @@
+{%- from "neutron/map.jinja" import server,gateway,compute,fwaas with context %}
+
+neutron_render_config:
+  test.show_notification:
+    - text: "Running neutron.upgrade.render_config"
+
+{%- if server.enabled %}
+  {%- set conf_mapping = [['/etc/neutron/neutron.conf', 'salt://neutron/files/' + server.version + '/neutron-server.conf'],
+                          ['/etc/neutron/api-paste.ini','salt://neutron/files/' + server.version + '/api-paste.ini']] %}
+{%- elif gateway.enabled %}
+  {%- set conf_mapping = [['/etc/neutron/neutron.conf', 'salt://neutron/files/' + gateway.version + '/neutron-generic.conf']] %}
+{%- elif compute.enabled %}
+  {%- set conf_mapping = [['/etc/neutron/neutron.conf', 'salt://neutron/files/' + compute.version + '/neutron-generic.conf']] %}
+{%- endif %}
+
+{%- if server.enabled %}
+  {% if server.backend.engine == "contrail" %}
+    {%- do conf_mapping.append(['/etc/neutron/plugins/opencontrail/ContrailPlugin.ini', "salt://neutron/files/" + server.version + "/ContrailPlugin.ini"]) %}
+  {% elif server.backend.engine == "ml2" %}
+    {%- do conf_mapping.append(['/etc/neutron/plugins/ml2/ml2_conf.ini', "salt://neutron/files/" + server.version + "/ml2_conf.ini"]) %}
+  {%- elif server.backend.engine == "midonet" %}
+    {%- do conf_mapping.append(['/etc/neutron/plugins/midonet/midonet.ini', "salt://neutron/files/" + server.version + "/midonet.ini"]) %}
+  {% elif server.backend.engine == "vmware" %}
+    {%- do conf_mapping.append(['/etc/neutron/plugins/vmware/nsx.ini', "salt://neutron/files/" + server.version + "/plugins/nsx.ini"]) %}
+  {%- endif %}
+  {%- if fwaas.get('enabled', False) %}
+    {%- do conf_mapping.append(['/etc/neutron/fwaas_driver.ini', "salt://neutron/files/" + fwaas.version + "/fwaas_driver.ini"]) %}
+  {%- endif %}
+  {%- if server.get('l2gw', {}).get('enabled', False) %}
+    {%- do conf_mapping.append(['/etc/neutron/l2gw_plugin.ini', "salt://neutron/files/" + server.version + "/l2gw/l2gw_plugin.ini"]) %}
+  {%- endif %}
+{%- elif gateway.enabled %}
+  {%- do conf_mapping.extend([['/etc/neutron/l3_agent.ini', "salt://neutron/files/" + gateway.version + "/l3_agent.ini"],
+                             ['/etc/neutron/plugins/ml2/openvswitch_agent.ini', "salt://neutron/files/" + gateway.version + "/openvswitch_agent.ini"],
+                             ['/etc/neutron/dhcp_agent.ini', "salt://neutron/files/" + gateway.version + "/dhcp_agent.ini"],
+                             ['/etc/neutron/metadata_agent.ini',"salt://neutron/files/" + gateway.version + "/metadata_agent.ini"]]) %}
+{%- elif compute.enabled %}
+  {% if compute.get('bgp_vpn', {}).get('enabled', False) and server.bgp_vpn.driver == "bagpipe" %}
+    {%- do conf_mapping.append(['/etc/bagpipe-bgp/bgp.conf', "salt://neutron/files/" + compute.version + "/bagpipe-bgp.conf"]) %}
+  {%- endif %}
+  {% if compute.backend.engine == "ml2" %}
+    {%- if compute.dvr %}
+      {%- do conf_mapping.extend([['/etc/neutron/l3_agent.ini', "salt://neutron/files/" + compute.version + "/l3_agent.ini"],
+                                 ['/etc/neutron/metadata_agent.ini',"salt://neutron/files/" + compute.version + "/metadata_agent.ini"]]) %}
+    {%- endif %}
+    {% if compute.get('dhcp_agent_enabled', False) %}
+      {%- do conf_mapping.extend([['/etc/neutron/dhcp_agent.ini', "salt://neutron/files/" + compute.version + "/dhcp_agent.ini"]]) %}
+    {%- endif %}
+    {% if compute.backend.sriov is defined %}
+      {%- do conf_mapping.extend([['/etc/neutron/plugins/ml2/sriov_agent.ini', "salt://neutron/files/" + compute.version + "/sriov_agent.ini"]]) %}
+    {%- endif %}
+  {%- endif %}
+{%- endif %}
+
+
+{%- for file, source in conf_mapping %}
+{{ file }}:
+  file.managed:
+  - source: {{ source }}
+  - template: jinja
+{%- endfor %}
diff --git a/neutron/upgrade/service_running.sls b/neutron/upgrade/service_running.sls
new file mode 100644
index 0000000..d2ac334
--- /dev/null
+++ b/neutron/upgrade/service_running.sls
@@ -0,0 +1,36 @@
+{%- from "neutron/map.jinja" import server,gateway,compute with context %}
+
+neutron_service_running:
+  test.show_notification:
+    - text: "Running neutron.upgrade.service_running"
+
+{%- set nservices = [] %}
+
+{%- if server.enabled %}
+  {%- do nservices.extend(server.services) %}
+  {% if server.backend.engine == "contrail" %}
+    {%- do nservices.append('neutron-server') %}
+  {%- endif %}
+{%- endif %}
+{%- if gateway.enabled is defined and gateway.enabled%}
+  {%- do nservices.extend(gateway.services) %}
+{%- endif %}
+{%- if compute.enabled is defined and compute.enabled%}
+  {%- do nservices.extend(compute.services) %}
+  {%- if compute.dvr %}
+    {%- do nservices.extend(['neutron-l3-agent', 'neutron-metadata-agent']) %}
+  {%- endif %}
+  {% if compute.get('dhcp_agent_enabled', False) %}
+    {%- do nservices.append('neutron-dhcp-agent') %}
+  {%- endif %}
+  {% if compute.backend.sriov is defined %}
+    {%- do nservices.append('neutron-sriov-agent') %}
+  {%- endif %}
+{%- endif %}
+
+{%- for nservice in nservices|unique %}
+neutron_service_{{ nservice }}_running:
+  service.running:
+  - enable: True
+  - name: {{ nservice }}
+{%- endfor %}
diff --git a/neutron/upgrade/service_stopped.sls b/neutron/upgrade/service_stopped.sls
new file mode 100644
index 0000000..6e33e5c
--- /dev/null
+++ b/neutron/upgrade/service_stopped.sls
@@ -0,0 +1,37 @@
+{%- from "neutron/map.jinja" import server,gateway,compute with context %}
+
+neutron_service_stopped:
+  test.show_notification:
+    - text: "Running neutron.upgrade.service_stopped"
+
+{%- set nservices = [] %}
+
+{%- if server.enabled %}
+  {%- do nservices.extend(server.services) %}
+  {% if server.backend.engine == "contrail" %}
+    {%- do nservices.append('neutron-server') %}
+  {%- endif %}
+{%- endif %}
+{%- if gateway.enabled is defined and gateway.enabled %}
+  {%- do nservices.extend(gateway.services) %}
+{%- endif %}
+{%- if compute.enabled is defined and compute.enabled %}
+  {%- do nservices.extend(compute.services) %}
+  {%- if compute.dvr %}
+    {%- do nservices.extend(['neutron-l3-agent', 'neutron-metadata-agent']) %}
+  {%- endif %}
+  {% if compute.get('dhcp_agent_enabled', False) %}
+    {%- do nservices.append('neutron-dhcp-agent') %}
+  {%- endif %}
+  {% if compute.backend.sriov is defined %}
+    {%- do nservices.append('neutron-sriov-agent') %}
+  {%- endif %}
+{%- endif %}
+
+
+{%- for nservice in nservices|unique %}
+neutron_service_{{ nservice }}_stopped:
+  service.dead:
+  - name: {{ nservice }}
+  - enable: false
+{%- endfor %}
diff --git a/neutron/upgrade/upgrade/init.sls b/neutron/upgrade/upgrade/init.sls
new file mode 100644
index 0000000..4ca8dea
--- /dev/null
+++ b/neutron/upgrade/upgrade/init.sls
@@ -0,0 +1,16 @@
+{%- from "neutron/map.jinja" import server with context %}
+
+neutron_upgrade:
+  test.show_notification:
+    - text: "Running neutron.upgrade.upgrade"
+
+include:
+ - neutron.upgrade.upgrade.pre
+ - neutron.upgrade.service_stopped
+ - neutron.upgrade.pkgs_latest
+ - neutron.upgrade.render_config
+{%- if server.get('enabled') %}
+ - neutron.db.offline_sync
+{%- endif %}
+ - neutron.upgrade.service_running
+ - neutron.upgrade.upgrade.post
diff --git a/neutron/upgrade/upgrade/post.sls b/neutron/upgrade/upgrade/post.sls
new file mode 100644
index 0000000..9c1d20f
--- /dev/null
+++ b/neutron/upgrade/upgrade/post.sls
@@ -0,0 +1,10 @@
+{%- from "neutron/map.jinja" import server,gateway with context %}
+
+{%- if gateway.get('enabled') %}
+{% set host_id = salt['network.get_hostname']() %}
+
+neutron_agent_enabled:
+  neutronv2.agents_enabled:
+    - name: {{ host_id }}
+    - cloud_name: admin_identity
+{%- endif %}
diff --git a/neutron/upgrade/upgrade/pre.sls b/neutron/upgrade/upgrade/pre.sls
new file mode 100644
index 0000000..9a4a105
--- /dev/null
+++ b/neutron/upgrade/upgrade/pre.sls
@@ -0,0 +1,11 @@
+{%- from "neutron/map.jinja" import server,gateway with context %}
+
+{%- if gateway.get('enabled') %}
+{% set host_id = salt['network.get_hostname']() %}
+
+neutron_agent_disable:
+  neutronv2.agents_disabled:
+    - name: {{ host_id }}
+    - cloud_name: admin_identity
+
+{%- endif %}
diff --git a/neutron/upgrade/verify/api.sls b/neutron/upgrade/verify/api.sls
new file mode 100644
index 0000000..e45df66
--- /dev/null
+++ b/neutron/upgrade/verify/api.sls
@@ -0,0 +1,55 @@
+{%- from "neutron/map.jinja" import server with context %}
+{%- from "keystone/map.jinja" import client as kclient with context %}
+
+neutron_upgrade_verify_api:
+  test.show_notification:
+    - text: "Running neutron.upgrade.verify.api"
+
+{%- if kclient.enabled and kclient.get('os_client_config', {}).get('enabled', False)  %}
+  {%- if server.enabled %}
+    {%- set neutron_test_network = 'Upgrade_TestNetwork' %}
+    {%- set neutron_test_subnet = 'Upgrade_TestSubnet' %}
+
+neutronv2_subnet_list:
+  module.run:
+    - name: neutronv2.subnet_list
+    - kwargs:
+        cloud_name: admin_identity
+
+neutronv2_network_list:
+  module.run:
+    - name: neutronv2.network_list
+    - kwargs:
+        cloud_name: admin_identity
+
+neutronv2_network_present:
+  neutronv2.network_present:
+  - cloud_name: admin_identity
+  - name: {{ neutron_test_network }}
+
+neutronv2_subnet_present:
+  neutronv2.subnet_present:
+  - name: {{ neutron_test_subnet }}
+  - cloud_name: admin_identity
+  - network_id: {{ neutron_test_network }}
+  - ip_version: 4
+  - cidr: 192.168.89.0/24
+  - require:
+    - neutronv2_network_present
+
+neutronv2_subnet_absent:
+  neutronv2.subnet_absent:
+  - cloud_name: admin_identity
+  - name: {{ neutron_test_subnet }}
+  - require:
+    - neutronv2_subnet_present
+
+neutronv2_network_absent:
+  neutronv2.network_absent:
+  - cloud_name: admin_identity
+  - name: {{ neutron_test_network }}
+  - require:
+    - neutronv2_subnet_absent
+
+  {%- endif %}
+{%- endif %}