---
- name: Get container facts
  kolla_container_facts:
    name:
      - opendaylight
  register: container_facts

- name: Checking free port for opendaylight_clustering
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_clustering_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_restconf
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_restconf_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_restconf_backup
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_restconf_port_backup }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_karaf_ssh
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_karaf_ssh_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_openflow
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_openflow_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_ovsdb
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_ovsdb_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_jetty_conf_port
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_jetty_conf_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_jetty_conf2_port
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_jetty_conf2_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_tomcat_port
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_tomcat_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking free port for opendaylight_tomcat_redirect_port
  wait_for:
    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
    port: "{{ opendaylight_tomcat_redirect_port }}"
    connect_timeout: 1
    state: stopped
  when:
    - inventory_hostname in groups['opendaylight']
    - container_facts['opendaylight'] is not defined

- name: Checking available opendaylight nodes in inventory
  fail:
    msg: "Either 1 or 3 nodes required in inventory for OpenDaylight clustering"
  when: groups['opendaylight'] | length == 2
