# 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.

- name: Remove old OVS
  apt:
    name: "{{ item }}"
    state: absent
  with_items:
    - openvswitch-switch
    - openvswitch-common
  when: enable_fabric

- name: Install default OVS
  apt:
    name: "{{ item }}"
    state: present
  with_items: "{{ ovs_packages }}"
  when: not enable_fabric

- name: (fabric) Download OVS 2.5.3 packages
  get_url:
    url: "{{ item.url }}"
    checksum: "{{ item.checksum }}"
    dest: "/tmp/{{ item.name }}"
  with_items: "{{ ovs_fabric_packages }}"
  when: enable_fabric

- name: (fabric) Install OVS 2.5.3
  apt:
    deb: "/tmp/{{ item.name }}"
    state: present
  with_items: "{{ ovs_fabric_packages }}"
  when: enable_fabric

- name: Create OVS bridges for CiaB
  openvswitch_bridge:
    bridge: "{{ item }}"
    state: present
  with_items: "{{ ovs_bridges }}"

- name: Create patch ports
  openvswitch_port:
    bridge: "{{ item.bridge }}"
    port: "{{ item.port }}"
    set: "Interface {{ item.port }} type=patch"
  with_items: "{{ ovs_patch_ports }}"

- name: Create peers
  openvswitch_port:
    bridge: "{{ item.bridge }}"
    port: "{{ item.port }}"
    set: "Interface {{ item.port }} options:peer={{ item.peer }}"
  with_items: "{{ ovs_patch_ports }}"

# "set" argument to openvswitch_bridge wasn't working...
- name: Set bridges to OpenFlow 1.3
  command: ovs-vsctl set bridge {{ item }} protocols=OpenFlow13
  when: enable_fabric
  with_items: "{{ ovs_bridges }}"
  tags:
    - skip_ansible_lint # running a sub job

- name: Add ONOS fabric controller
  command: ovs-vsctl set-controller {{ item }} tcp:{{ ovs_controller_ip }}:{{ ovs_controller_port }}
  when: enable_fabric
  with_items: "{{ ovs_bridges }}"
  tags:
    - skip_ansible_lint # running a sub job
