---
- name: Configure time synchronization with multiple PTP domains
  hosts: all
  gather_facts: true
  vars:
    timesync_ptp_domains:
      - number: 0
        interfaces: "{{ [ ansible_default_ipv4['interface'] ] }}"
        transport: L2
      - number: 1
        interfaces: "{{ [ ansible_default_ipv4['interface'] ] }}"
        delay: 0.001
        transport: UDPv4
        hybrid_e2e: true
        udp_ttl: 2
    timesync_step_threshold: 0.0001
    timesync_min_sources: 2
  roles:
    - redhat.rhel_system_roles.timesync

  tasks:
    - name: Run test
      tags: tests::verify
      block:
        - name: Flush handlers
          meta: flush_handlers

        - name: Ensure ethtool is installed
          package:
            name: ethtool
            state: present

        - name: Check if SW/HW timestamping is supported
          command: ethtool -T "{{ ansible_default_ipv4['interface'] }}"
          register: ethtool
          ignore_errors: true # noqa ignore-errors
          changed_when: false

        - name: Run test
          when: "'ware-transmit' in ethtool.stdout"
          block:
            - name: Wait for services to start
              wait_for:
                timeout: 2

            - name: Get list of currently used time sources
              shell: chronyc -n sources || ntpq -pn
              register: sources
              changed_when: false

            - name: Check time sources
              assert:
                that:
                  - "'PTP0' in sources.stdout"
                  - "'PTP1' in sources.stdout"

            - name: Run pmc
              vars:
              # wokeignore:rule=master
                __sock: /var/run/timemaster/ptp4l.0.socket
              command: >-
                pmc -u -b 0 -d 0 -s "{{ __sock }}" 'GET DOMAIN'
                'GET PORT_DATA_SET'
              register: pmc
              changed_when: false

            - name: Check PTP domain
              assert:
                that:
                  - "'domainNumber 0' in pmc.stdout"

            - name: Check PTP port state
              assert:
                that:
                  - pmc.stdout is search("portState\s+LISTENING")
              when:
                - ansible_distribution not in ['RedHat', 'CentOS'] or ansible_distribution_version is not version('8.3', '<')

            - name: Run pmc
              vars:
              # wokeignore:rule=master
                __sock: /var/run/timemaster/ptp4l.1.socket
              command: >-
                pmc -u -b 0 -d 1 -s "{{ __sock }}" 'GET DOMAIN'
                'GET PORT_DATA_SET'
              register: pmc
              changed_when: false

            - name: Check PTP domain
              assert:
                that:
                  - "'domainNumber 1' in pmc.stdout"

            - name: Check PTP port state
              assert:
                that:
                  - pmc.stdout is search("portState\s+LISTENING")
              when:
                - ansible_distribution not in ['RedHat', 'CentOS'] or ansible_distribution_version is not version('8.3', '<')
