From 6b7f148f11167b9b062548a99ece7356fa9181af Mon Sep 17 00:00:00 2001
From: Aleksandr Kurbatov <aleksandr.kurbatov@GL1342-AKURBATOV.local>
Date: Wed, 21 Aug 2024 16:25:58 +0100
Subject: [PATCH] Updates to `promote_p_to_pe` role

---
 .../tasks/check_routing_instances.yaml        | 22 +++++++++++++++
 ...mpile_pe_base_config.yaml => compile.yaml} | 10 +++++++
 ...deploy_pe_base_config.yaml => deploy.yaml} | 28 +++++++++++++++++++
 .../roles/promote_p_to_pe/tasks/main.yml      | 21 +++++++++-----
 .../tasks/merge_variables.yaml                | 13 +++++++++
 .../templates/routers/nokia/filter_log.j2     | 18 ++++++++++++
 .../templates/routers/nokia/pe_base_config.j2 |  1 +
 .../templates/routers/nokia/pe_base_vprn.j2   |  2 +-
 .../router/delete_default_static_routes.j2    |  1 +
 .../templates/routers/nokia/router/vprn.j2    |  2 --
 .../templates/routers/nokia/sdp_mesh.j2       |  4 +--
 .../templates/routers/nokia/service_vprn.j2   | 12 ++++++++
 .../roles/promote_p_to_pe/vars/main.yml       |  1 +
 13 files changed, 123 insertions(+), 12 deletions(-)
 create mode 100644 geant/gap_ansible/roles/promote_p_to_pe/tasks/check_routing_instances.yaml
 rename geant/gap_ansible/roles/promote_p_to_pe/tasks/{compile_pe_base_config.yaml => compile.yaml} (81%)
 rename geant/gap_ansible/roles/promote_p_to_pe/tasks/{deploy_pe_base_config.yaml => deploy.yaml} (75%)
 create mode 100644 geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/filter_log.j2
 create mode 100644 geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/service_vprn.j2

diff --git a/geant/gap_ansible/roles/promote_p_to_pe/tasks/check_routing_instances.yaml b/geant/gap_ansible/roles/promote_p_to_pe/tasks/check_routing_instances.yaml
new file mode 100644
index 00000000..653d21d0
--- /dev/null
+++ b/geant/gap_ansible/roles/promote_p_to_pe/tasks/check_routing_instances.yaml
@@ -0,0 +1,22 @@
+- name: Set variable to connect to nokia
+  ansible.builtin.set_fact:
+    ansible_network_os: nokia.sros.md
+    ansible_connection: netconf
+
+- name: Check VPRN BGP summary
+  block:
+    - name: Get BGP summary
+      ansible.netcommon.netconf_rpc:
+        rpc: action
+        xmlns: "urn:ietf:params:xml:ns:yang:1"
+        content: |
+            <global-operations xmlns="urn:nokia.com:sros:ns:yang:sr:oper-global">
+              <md-cli-raw-command>
+              <md-cli-input-line>show router service-name "{{ vprn.name | quote }}" bgp summary</md-cli-input-line>
+              </md-cli-raw-command>
+            </global-operations>
+        display: json
+      register: out
+    - name: Show BGP summary for VPRN "{{ vprn.name | quote }}"
+      ansible.builtin.debug:
+        msg: "{{out.output['rpc-reply']['nokiaoper:results']['nokiaoper:md-cli-output-block'].split('\n')}}"
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/tasks/compile_pe_base_config.yaml b/geant/gap_ansible/roles/promote_p_to_pe/tasks/compile.yaml
similarity index 81%
rename from geant/gap_ansible/roles/promote_p_to_pe/tasks/compile_pe_base_config.yaml
rename to geant/gap_ansible/roles/promote_p_to_pe/tasks/compile.yaml
index d374f30c..6208e177 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/tasks/compile_pe_base_config.yaml
+++ b/geant/gap_ansible/roles/promote_p_to_pe/tasks/compile.yaml
@@ -44,6 +44,16 @@
     mode: '0755'
   delegate_to: localhost
 
+- name: Print the template in "/var/tmp/ansible_run_{{ opid }}/delete_default_static_routes.conf"
+  when: verb == "delete_default_routes"
+  ansible.builtin.template:
+    src: "routers/{{ subscription.router.vendor }}/router/delete_default_static_routes.j2"
+    dest: "/var/tmp/ansible_run_{{ opid }}/delete_default_static_routes.conf"
+    lstrip_blocks: true
+    trim_blocks: true
+    mode: '0755'
+  delegate_to: localhost
+
 - name: Set back ansible_host to target terminal server if needed
   when:
     subscription.router.router_access_via_ts | ansible.builtin.bool
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy_pe_base_config.yaml b/geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy.yaml
similarity index 75%
rename from geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy_pe_base_config.yaml
rename to geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy.yaml
index 5e2f1fe0..dbae83ee 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy_pe_base_config.yaml
+++ b/geant/gap_ansible/roles/promote_p_to_pe/tasks/deploy.yaml
@@ -39,6 +39,21 @@
   diff: true
   register: output
   check_mode: true
+
+- name: Delete default routes on "{{ inventory_hostname }}" [CHECK ONLY][NOKIA]
+  when: >-
+      dry_run | ansible.builtin.bool
+      and verb == "delete_default_routes"
+  geant.gap_ansible.nokia_netconf_config:
+    format: xml
+    default_operation: merge
+    content: "{{ lookup('ansible.builtin.template', 'routers/{{ subscription.router.vendor }}/router/delete_default_static_routes.j2') }}"
+    commit: true
+    validate: true
+  diff: true
+  register: output
+  check_mode: true
+
 # - name: Fail if there is any diff
 #   ansible.builtin.fail:
 #     msg: Base config drift detected!!!
@@ -85,3 +100,16 @@
     commit_comment: "{{ commit_comment }}"
   diff: true
   check_mode: false
+
+- name: Delete default routes on "{{ inventory_hostname }}" [AND COMMIT][NOKIA]
+  when: >-
+      not (dry_run | ansible.builtin.bool)
+      and verb == "delete_default_routes"
+  geant.gap_ansible.nokia_netconf_config:
+    format: xml
+    default_operation: merge
+    content: "{{ lookup('ansible.builtin.template', 'routers/{{ subscription.router.vendor }}/router/delete_default_static_routes.j2') }}"
+    commit: true
+    commit_comment: "{{ commit_comment }}"
+  diff: true
+  check_mode: false
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/tasks/main.yml b/geant/gap_ansible/roles/promote_p_to_pe/tasks/main.yml
index 02a46992..9c352c0e 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/tasks/main.yml
+++ b/geant/gap_ansible/roles/promote_p_to_pe/tasks/main.yml
@@ -49,13 +49,20 @@
   ansible.builtin.include_tasks: configure_isis_overload.yaml
 
 - name: Include templates compilation
-  when: verb in [ 'deploy_pe_base_config', 'update_sdp_mesh' , 'deploy_routing_instances' ]
-  ansible.builtin.include_tasks: compile_pe_base_config.yaml
+  when: verb in [ 'deploy_pe_base_config', 'update_sdp_mesh' , 'deploy_routing_instances', 'delete_default_routes' ]
+  ansible.builtin.include_tasks: compile.yaml
 
 - name: Include deploy if selected
-  when: verb in [ 'deploy_pe_base_config', 'update_sdp_mesh', 'deploy_routing_instances' ]
-  ansible.builtin.include_tasks: deploy_pe_base_config.yaml
+  when: verb in [ 'deploy_pe_base_config', 'update_sdp_mesh', 'deploy_routing_instances', 'delete_default_routes' ]
+  ansible.builtin.include_tasks: deploy.yaml
 
-- name: Include deletion of default static routes
-  when: verb == "delete_default_routes"
-  ansible.builtin.include_tasks: delete_default_routes.yaml
+# - name: Include deletion of default static routes
+#   when: verb == "delete_default_routes"
+#   ansible.builtin.include_tasks: delete_default_routes.yaml
+#
+- name: Check VPRNs
+  when: verb == "check_base_ris"
+  loop: "{{ pe_vprns }}"
+  loop_control:
+    loop_var: vprn
+  ansible.builtin.include_tasks: check_routing_instances.yaml
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/tasks/merge_variables.yaml b/geant/gap_ansible/roles/promote_p_to_pe/tasks/merge_variables.yaml
index 30bb080f..7ad3d7a9 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/tasks/merge_variables.yaml
+++ b/geant/gap_ansible/roles/promote_p_to_pe/tasks/merge_variables.yaml
@@ -24,3 +24,16 @@
     - name: Select SDP params for l2ciruit-type
       ansible.builtin.set_fact:
         sdp_type: "{{ (sdp_types | selectattr('id', 'equalto', '1'))[0] }}"
+
+- name: Prepare PE-specific base config vars
+  when: verb == "deploy_routing_instances"
+  block:
+    - name: Combine P and PE prefix-lists
+      ansible.builtin.set_fact:
+        nokia_prefix_lists: "{{ [nokia_prefix_lists, nokia_pe_prefix_lists, nokia_pe_ias_prefix_lists] | community.general.lists_mergeby('name',
+                                                                                              list_merge='append') }}"
+
+- name: Select SDP params for l2ciruit (epipe) type
+  when: verb == "update_sdp_mesh"
+  ansible.builtin.set_fact:
+    sdp_type: "{{ (sdp_types | selectattr('id', 'equalto', '1'))[0] }}"
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/filter_log.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/filter_log.j2
new file mode 100644
index 00000000..7ab3675a
--- /dev/null
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/filter_log.j2
@@ -0,0 +1,18 @@
+
+<filter xmlns="urn:nokia.com:sros:ns:yang:sr:conf" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nokia-attr="urn:nokia.com:sros:ns:yang:sr:attributes">
+    {% if pe_filter_logs is defined %}
+      {% for log in pe_filter_logs %}
+    <log alu:operation="replace">
+        <log-id>{{ log.id }}</log-id>
+        <description>{{ log.description }}</description>
+        <destination>
+          {% if log.destination == 'syslog' %}
+            <syslog>
+                <name>1</name>
+            </syslog>
+          {% endif %}
+        </destination>
+    </log>
+      {% endfor %}
+    {% endif %}
+</filter>
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_config.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_config.j2
index feebe23a..c3c587bb 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_config.j2
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_config.j2
@@ -1,5 +1,6 @@
 <config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:alu="urn:ietf:params:xml:ns:netconf:base:1.0">
     <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
+       {% include 'filter_log.j2' %}
        {% include 'ip_prefix_list.j2' %}
        {% include 'filters/cpm_filters.j2' %}
        {% include 'policy_options/policy_options.j2' %}
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_vprn.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_vprn.j2
index 1ab52618..f8467b33 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_vprn.j2
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/pe_base_vprn.j2
@@ -1,5 +1,5 @@
 <config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:alu="urn:ietf:params:xml:ns:netconf:base:1.0">
     <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
-       {% include 'router/vprn.j2' %}
+       {% include 'service_vprn.j2' %}
    </configure>
 </config>
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/delete_default_static_routes.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/delete_default_static_routes.j2
index 31db5e03..f3d8d46b 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/delete_default_static_routes.j2
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/delete_default_static_routes.j2
@@ -6,6 +6,7 @@
           {% for route in nokia_static_routes %}
             <route alu:operation="delete">
               <ip-prefix>{{ route.ip_prefix }}</ip-prefix>
+              <route-type>{{ route.route_type }}</route-type>
             </route>
           {% endfor %}
           </static-routes>
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/vprn.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/vprn.j2
index fa0ebb64..c6c04964 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/vprn.j2
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/router/vprn.j2
@@ -1,4 +1,3 @@
-<service xmlns="urn:nokia.com:sros:ns:yang:sr:conf" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nokia-attr="urn:nokia.com:sros:ns:yang:sr:attributes">
     {% for vprn in pe_vprns %}
     <vprn alu:operation="replace">
         <service-name>{{ vprn.name }}</service-name>
@@ -56,5 +55,4 @@
         {% endif %}
     </vprn>
     {% endfor %}
-</service>
 
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/sdp_mesh.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/sdp_mesh.j2
index 45be0ab7..df67f22d 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/sdp_mesh.j2
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/sdp_mesh.j2
@@ -1,7 +1,7 @@
     <service xmlns="urn:nokia.com:sros:ns:yang:sr:conf" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nokia-attr="urn:nokia.com:sros:ns:yang:sr:attributes">
-        <sdp>
             {% for pe_fqdn, pe_addr in pe_router_list.items() %}
             {% set sdp_id = pe_addr.lo4 | replace(sdp_prefix_regex, '') | replace('.', '') + sdp_type.id %}
+        <sdp>
             <sdp-id>{{ sdp_id }}</sdp-id>
             <admin-state>enable</admin-state>
             <description>SDP_{{ sdp_id }}</description>
@@ -14,7 +14,7 @@
             <far-end>
                 <ip-address>{{ pe_addr.lo4 }}</ip-address>
             </far-end>
-            {% endfor %}
         </sdp>
+            {% endfor %}
     </service>
     
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/service_vprn.j2 b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/service_vprn.j2
new file mode 100644
index 00000000..d14800d4
--- /dev/null
+++ b/geant/gap_ansible/roles/promote_p_to_pe/templates/routers/nokia/service_vprn.j2
@@ -0,0 +1,12 @@
+
+<service xmlns="urn:nokia.com:sros:ns:yang:sr:conf" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nokia-attr="urn:nokia.com:sros:ns:yang:sr:attributes">
+    <md-auto-id>
+        <service-id-range alu:operation="replace">
+            <start>{{ pe_service.md_auto_id.service_id_range.start | default('10001') }}</start>
+            <end>{{ pe_service.md_auto_id.service_id_range.end | default('19999') }}</end>
+        </service-id-range>
+    </md-auto-id>
+    {% include 'router/vprn.j2' %}
+</service>
+    {% include 'ip_prefix_list.j2' %}
+
diff --git a/geant/gap_ansible/roles/promote_p_to_pe/vars/main.yml b/geant/gap_ansible/roles/promote_p_to_pe/vars/main.yml
index dd26c7d3..33851df6 100644
--- a/geant/gap_ansible/roles/promote_p_to_pe/vars/main.yml
+++ b/geant/gap_ansible/roles/promote_p_to_pe/vars/main.yml
@@ -8,6 +8,7 @@ verbs:
   - delete_default_routes # Delete default static routes configured as part P base-config
   - deploy_routing_instances # Deploy standard PE VPRNs
   - update_sdp_mesh # Upadate SDP mesh on other Nokia PE routers with this promoted router
+  - check_base_ris # Check BGP summary for base VPRNs
 
 lo_ipv4_address: "{{ subscription.router.router_lo_ipv4_address }}"
 lo_ipv6_address: "{{ subscription.router.router_lo_ipv6_address }}"
-- 
GitLab