CORD-2461 Fix loading ONOS netcfg via TOSCA

Change-Id: Ic524aff06f3f35f5b4e5df88ceed3b82798420c5
diff --git a/cord-refresh-fabric.yml b/cord-refresh-fabric.yml
index a71ed88..8e3bb00 100644
--- a/cord-refresh-fabric.yml
+++ b/cord-refresh-fabric.yml
@@ -16,18 +16,6 @@
 
 ---
 # Creates a fabric configuration using 'cord generate'
-# Mainly useful for CiaB right now
-
-- name: Prep fabric on head node
-  hosts: head
-  roles:
-    - fabric-head-prep
-
-- name: Prep fabric on compute nodes
-  hosts: compute
-  remote_user: ubuntu
-  roles:
-    - fabric-compute-prep
 
 - name: Refresh fabric config
   hosts: head
diff --git a/roles/cord-profile/defaults/main.yml b/roles/cord-profile/defaults/main.yml
index 97e4a52..a82a911 100644
--- a/roles/cord-profile/defaults/main.yml
+++ b/roles/cord-profile/defaults/main.yml
@@ -147,9 +147,9 @@
 management_hosts_net_range_xos_low: "{{ vtn_net_management_host_range_xos_low }}"
 management_hosts_net_range_xos_high: "{{ vtn_net_management_host_range_xos_high }}"
 
-# used in fabric.yaml.j2
+# used in fabric-service.yaml.j2
 use_fabric: False
-fabric_network_cfg_json: "/opt/cord_profile/fabric-network-cfg.json"
+fabric_network_cfg_json: {}
 
 # base URL of maven repo where ONOS apps are retrieved
 onos_mavenrepo_url: "{{ 'http://mavenrepo.' ~ site_suffix ~ ':8080' }}"
diff --git a/roles/fabric-pingtest/tasks/main.yml b/roles/fabric-pingtest/tasks/main.yml
index 7950622..227979f 100644
--- a/roles/fabric-pingtest/tasks/main.yml
+++ b/roles/fabric-pingtest/tasks/main.yml
@@ -18,8 +18,8 @@
   command: ping -c 20 {{ item }}
   with_items:
   - 10.6.1.1
-  - 10.6.1.129
-  - 10.6.1.193
+  - 10.7.1.1
+  - 10.8.1.1
   changed_when: false
   tags:
     - skip_ansible_lint # running a sub job
diff --git a/roles/fabric-refresh/defaults/main.yml b/roles/fabric-refresh/defaults/main.yml
new file mode 100644
index 0000000..aefd2bb
--- /dev/null
+++ b/roles/fabric-refresh/defaults/main.yml
@@ -0,0 +1,23 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+head_cord_profile_dir: "/opt/cord_profile"
+cord_use_case: rcord
+
+config_credentials_dir: "{{ playbook_dir }}/credentials"
+
+xos_admin_user: "xosadmin@opencord.org"
+xos_admin_pass: "{{ lookup('file', config_credentials_dir ~ '/' ~ xos_admin_user) }}"
+
+xos_tosca_url: "http://xos-tosca/xos-tosca"
diff --git a/roles/fabric-refresh/tasks/main.yml b/roles/fabric-refresh/tasks/main.yml
index d46ebc6..f069814 100644
--- a/roles/fabric-refresh/tasks/main.yml
+++ b/roles/fabric-refresh/tasks/main.yml
@@ -12,24 +12,59 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-- name: Install httpie
-  pip:
-    name: httpie
-    state: present
-  become: yes
-
-# This step could be implemented more cleanly (i.e., broken into multiple
-# tasks, use "uri" module).  It was done this way to mirror what is
-# in the documentation (quickstart_physical.md).
-- name: Rebuild and reinstall ONOS fabric configuration
-  shell: "{{ item }}"
-  args:
-    chdir: /opt/cord_profile
-  with_items:
-    - "cord generate > fabric-network-cfg.json"
-    - "http -a onos:rocks DELETE http://onos-fabric:8181/onos/v1/network/configuration/"
-    - "docker-compose -p {{ cord_profile }} exec -T xos_ui python /opt/xos/tosca/run.py xosadmin@opencord.org /opt/cord_profile/fabric-service.yaml"
-    - "http -a onos:rocks POST http://onos-fabric:8181/onos/v1/applications/org.onosproject.vrouter/active"
-    - "http -a onos:rocks POST http://onos-fabric:8181/onos/v1/applications/org.onosproject.segmentrouting/active"
+- name: Generate fabric-network-cfg.json
+  shell: cord generate
+  register: cord_generate
   tags:
-    - skip_ansible_lint # running a sub job
+    - skip_ansible_lint
+
+- name: Set cord_profile_src_dir
+  set_fact:
+    cord_profile_src_dir: "../../orchestration/profiles/{{ cord_use_case }}"
+
+- name: Set fabric_network_cfg_json
+  set_fact:
+    fabric_network_cfg_json: "{{ cord_generate.stdout }}"
+
+- name: Regenerate fabric-service.yaml
+  template:
+    src: "{{ cord_profile_src_dir }}/templates/fabric-service.yaml.j2"
+    dest: "{{ head_cord_profile_dir }}/fabric-service.yaml"
+
+- name: Slurp fabric-service.yaml
+  slurp:
+    src: "{{ head_cord_profile_dir }}/fabric-service.yaml"
+  register: slurpfile
+
+- name: Delete old ONOS fabric configuration
+  uri:
+    url: "http://onos-fabric:8181/onos/v1/network/configuration/"
+    method: DELETE
+    user: onos
+    password: rocks
+    status_code: 200, 204
+
+- name: Upload new fabric configuration to XOS
+  uri:
+    url: "{{ xos_tosca_url }}/run"
+    method: POST
+    headers:
+      xos-username: "{{ xos_admin_user }}"
+      xos-password: "{{ xos_admin_pass }}"
+    body: "{{ slurpfile['content'] | b64decode }}"
+    status_code: 200
+  register: result
+  until: result | success
+  retries: 5
+  delay: 5
+
+- name: Re-activate ONOS apps
+  uri:
+    url: "{{ item }}"
+    method: POST
+    user: onos
+    password: rocks
+    status_code: 200
+  with_items:
+    - "http://onos-fabric:8181/onos/v1/applications/org.onosproject.vrouter/active"
+    - "http://onos-fabric:8181/onos/v1/applications/org.onosproject.segmentrouting/active"