From d4e665129411a052df997e634fb2ec4b8e548a65 Mon Sep 17 00:00:00 2001
From: Aleksandr Kurbatov <aleksandr.kurbatov@GL1342.local>
Date: Tue, 2 Jul 2024 19:42:47 +0100
Subject: [PATCH] `iptrunk_migration` updates related to BFD

When iptrunk_type == Leased, update is needed to the BFD config on the
__remaining_side__.
---
 .../iptrunk_migration/tasks/bfd_update.yaml   | 24 +++++++++++++++++++
 .../tasks/compile_template.yaml               | 10 ++++++++
 .../roles/iptrunk_migration/tasks/main.yml    | 12 ++++++++--
 .../tasks/set_netconf_connection.yaml         |  2 +-
 .../templates/juniper/bfd_update.j2           |  2 ++
 .../roles/iptrunk_migration/vars/main.yml     |  4 ++++
 6 files changed, 51 insertions(+), 3 deletions(-)
 create mode 100644 geant/gap_ansible/roles/iptrunk_migration/tasks/bfd_update.yaml
 create mode 100644 geant/gap_ansible/roles/iptrunk_migration/templates/juniper/bfd_update.j2

diff --git a/geant/gap_ansible/roles/iptrunk_migration/tasks/bfd_update.yaml b/geant/gap_ansible/roles/iptrunk_migration/tasks/bfd_update.yaml
new file mode 100644
index 00000000..1a31076c
--- /dev/null
+++ b/geant/gap_ansible/roles/iptrunk_migration/tasks/bfd_update.yaml
@@ -0,0 +1,24 @@
+---
+- name: Update Trunk interface BFD on  "{{ inventory_hostname }}" [CHECK ONLY][JUNIPER]
+  junipernetworks.junos.junos_config:
+    update: 'replace'
+    src: "/var/tmp/ansible_run_{{ opid }}/{{ config_object }}.conf"
+    src_format: set
+    check_commit: true
+  diff: true
+  when: >
+    verb == "update" and
+    remaining_side.vendor == "juniper" and
+    dry_run | ansible.builtin.bool
+
+- name: Update Trunk interface BFD on "{{ inventory_hostname }}" [AND COMMIT][JUNIPER]
+  junipernetworks.junos.junos_config:
+    update: 'replace'
+    src: "/var/tmp/ansible_run_{{ opid }}/{{ config_object }}.conf"
+    src_format: set
+    comment: "{{ commit_comment }}"
+  diff: true
+  when: >
+    verb == "update" and
+    remaining_side.vendor == "juniper" and
+    not (dry_run | ansible.builtin.bool)
diff --git a/geant/gap_ansible/roles/iptrunk_migration/tasks/compile_template.yaml b/geant/gap_ansible/roles/iptrunk_migration/tasks/compile_template.yaml
index da370805..80e59fe7 100644
--- a/geant/gap_ansible/roles/iptrunk_migration/tasks/compile_template.yaml
+++ b/geant/gap_ansible/roles/iptrunk_migration/tasks/compile_template.yaml
@@ -40,6 +40,16 @@
   delegate_to: localhost
   when: (verb == "deploy") and inventory_hostname == new_node.router.router_fqdn
 
+- name: Print template for the remaining node in "/var/tmp/ansible_run_{{ opid }}/{{ config_object }}.conf"
+  ansible.builtin.template:
+    src: "{{ remaining_side.vendor }}/{{ config_object }}.j2"
+    dest: "/var/tmp/ansible_run_{{ opid }}/{{ config_object }}.conf"
+    lstrip_blocks: true
+    trim_blocks: true
+    mode: '0755'
+  delegate_to: localhost
+  when: (verb == "update") and inventory_hostname == remaining_side.name
+
 - name: Set netconf connection for trunk nodes
   ansible.builtin.include_tasks: set_netconf_connection.yaml
   when: verb == "delete" or verb == "deactivate"
diff --git a/geant/gap_ansible/roles/iptrunk_migration/tasks/main.yml b/geant/gap_ansible/roles/iptrunk_migration/tasks/main.yml
index 9474d27a..1ca1cd48 100644
--- a/geant/gap_ansible/roles/iptrunk_migration/tasks/main.yml
+++ b/geant/gap_ansible/roles/iptrunk_migration/tasks/main.yml
@@ -35,11 +35,11 @@
 
 - name: Calculate local side
   ansible.builtin.include_tasks: calculate_local_side.yaml
-  when: (verb == "delete" or verb == "deactivate") and inventory_hostname != new_node.router.router_fqdn
+  when: (verb in ["delete", "deactivate", "update"]) and inventory_hostname != new_node.router.router_fqdn
 
 - name: Set netconf connection for trunk nodes
   ansible.builtin.include_tasks: set_netconf_connection.yaml
-  when: (verb == "delete" or verb == "deactivate") and inventory_hostname != new_node.router.router_fqdn
+  when: (verb in ["delete", "deactivate", "update"]) and inventory_hostname != new_node.router.router_fqdn
 
 - name: Set ansible_host connection for new side
   ansible.builtin.set_fact:
@@ -80,3 +80,11 @@
 - name: Include the deployment tasks if specified
   ansible.builtin.include_tasks: deploy_object.yaml
   when: verb == "deploy" and inventory_hostname == new_node.router.router_fqdn
+
+- name: Compile update for the remaining node (BFD update)
+  ansible.builtin.include_tasks: compile_template.yaml
+  when: verb == "update" and inventory_hostname == remaining_side.name
+
+- name: Include update for the remaining node (BFD update)
+  ansible.builtin.include_tasks: bfd_update.yaml
+  when: verb == "update" and inventory_hostname == remaining_side.name
diff --git a/geant/gap_ansible/roles/iptrunk_migration/tasks/set_netconf_connection.yaml b/geant/gap_ansible/roles/iptrunk_migration/tasks/set_netconf_connection.yaml
index 47f7abad..c5f88705 100644
--- a/geant/gap_ansible/roles/iptrunk_migration/tasks/set_netconf_connection.yaml
+++ b/geant/gap_ansible/roles/iptrunk_migration/tasks/set_netconf_connection.yaml
@@ -3,4 +3,4 @@
     ansible_host: "{{ local_side.iptrunk_side_node.router_site.site_ts_address }}"
     ansible_port: "{{ local_side.iptrunk_side_node.router_ts_port }}"
   when:
-    ( local_side.iptrunk_side_node.router_access_via_ts | ansible.builtin.bool ) is true
+    local_side.iptrunk_side_node.router_access_via_ts | ansible.builtin.bool
diff --git a/geant/gap_ansible/roles/iptrunk_migration/templates/juniper/bfd_update.j2 b/geant/gap_ansible/roles/iptrunk_migration/templates/juniper/bfd_update.j2
new file mode 100644
index 00000000..69dc5039
--- /dev/null
+++ b/geant/gap_ansible/roles/iptrunk_migration/templates/juniper/bfd_update.j2
@@ -0,0 +1,2 @@
+
+set interfaces {{ remaining_side.ae_name }} aggregated-ether-options bfd-liveness-detection neighbor {{ new_node.router.router_lo_ipv4_address }}
diff --git a/geant/gap_ansible/roles/iptrunk_migration/vars/main.yml b/geant/gap_ansible/roles/iptrunk_migration/vars/main.yml
index 565ba579..36c89b85 100644
--- a/geant/gap_ansible/roles/iptrunk_migration/vars/main.yml
+++ b/geant/gap_ansible/roles/iptrunk_migration/vars/main.yml
@@ -6,6 +6,7 @@ verbs:
   - "deactivate"
   - "deploy"
   - "delete"
+  - "update"
 
 config_objects:
   - "deactivate"
@@ -13,6 +14,7 @@ config_objects:
   - "trunk_interface"
   - "isis_interface"
   - "trunk_deprovision"
+  - "bfd_update"
 
 bfd_min_interval: 3000
 mtu_phy: 9212
@@ -46,4 +48,6 @@ new_side:
 
 remaining_side:
   name: "{{ wfo_trunk.iptrunk.iptrunk_sides[1 - (replace_index | int)].iptrunk_side_node.router_fqdn }}"
+  vendor: "{{ wfo_trunk.iptrunk.iptrunk_sides[1 - (replace_index | int)].iptrunk_side_node.vendor }}"
   loopback: "{{ wfo_trunk.iptrunk.iptrunk_sides[1 - (replace_index | int)].iptrunk_side_node.router_lo_ipv4_address }}"
+  ae_name: "{{ wfo_trunk.iptrunk.iptrunk_sides[1 - (replace_index | int)].iptrunk_side_ae_iface }}"
-- 
GitLab