# Project Clearwater - IMS in the Cloud
# Copyright (C) 2015  Metaswitch Networks Ltd
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version, along with the "Special Exception" for use of
# the program along with SSL, set forth below. This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details. You should have received a copy of the GNU General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
# The author can be reached by email at clearwater@metaswitch.com or by
# post at Metaswitch Networks Ltd, 100 Church St, Enfield EN2 6BQ, UK
#
# Special Exception
# Metaswitch Networks Ltd  grants you permission to copy, modify,
# propagate, and distribute a work formed by combining OpenSSL with The
# Software, or a work derivative of such a combination, even if such
# copying, modification, propagation, or distribution would otherwise
# violate the terms of the GPL. You must comply with the GPL in all
# respects for all of the code used other than OpenSSL.
# "OpenSSL" means OpenSSL toolkit software distributed by the OpenSSL
# Project and licensed under the OpenSSL Licenses, or a work based on such
# software and licensed under the OpenSSL Licenses.
# "OpenSSL Licenses" means the OpenSSL License and Original SSLeay License
# under which the OpenSSL Project distributes the OpenSSL toolkit software,
# as those licenses appear in the file LICENSE-OPENSSL.

heat_template_version: 2015-04-30

description: >
  Base Project Clearwater Nazgul deployment on ONAP (Open Network Automation Platform)

parameters:
# Metadata required by ONAP
  vnf_name:
    type: string
    label: VNF name
    description: Unique name for this VNF instance
  vnf_id:
    type: string
    label: VNF ID
    description: The VNF ID provided by ONAP
  vf_module_id:
    type: string
    label: VNF module ID
    description: The VNF module ID provided by ONAP

# flavor parameters, naming required by ONAP
  bono_flavor_name:
    type: string
    description: VM flavor for bono VMs
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  sprout_flavor_name:
    type: string
    description: VM flavor for sprout VMs
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  vellum_flavor_name:
    type: string
    description: VM flavor for homestead VMs
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  homer_flavor_name:
    type: string
    description: VM flavor for homer VMs
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  dime_flavor_name:
    type: string
    description: VM flavor for dime VMs
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  ellis_flavor_name:
    type: string
    description: VM flavor for ellis VM
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  dns_flavor_name:
    type: string
    description: VM flavor for dns VM
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name
  robot_flavor_name:
    type: string
    description: VM flavor for robot_test VM
    constraints:
      - custom_constraint: nova.flavor
        description: Must be a valid flavor name

# image parameters, naming required by ONAP
  bono_image_name:
    type: string
    description: Name of image for bono VMs
  sprout_image_name:
    type: string
    description: Name of image for sprout VMs
  vellum_image_name:
    type: string
    description: Name of image for homestead VMs
  homer_image_name:
    type: string
    description: Name of image for homer VMs
  dime_image_name:
    type: string
    description: Name of image for dime VMs
  ellis_image_name:
    type: string
    description: Name of image for ellis VM
  dns_image_name:
    type: string
    description: Name of image for dns VMs
  robot_image_name:
    type: string
    description: Name of image for robot_test VMs

# overall clearwater parameters, naming required by ONAP
  clearwater_key_name:
    type: string
    label: openSSH Key name
    description: openSSH key name
  clearwater_pub_key:
    type: string
    label: Public key
    description: Public key to be installed on the compute instance
  repo_url:
    type: string
    description: URL for Clearwater repository
  zone:
    type: string
    description: DNS zone
  dn_range_start:
    type: string
    description: First directory number in pool
    constraints:
      - allowed_pattern: "[0-9]+"
        description: Must be numeric
  dn_range_length:
    type: string
    description: Number of directory numbers to add to pool
    constraints:
      - allowed_pattern: "[0-9]+"
        description: Must be numeric
  dnssec_key:
    type: string
    description: DNSSEC private key (Base64-encoded)
    constraints:
      - allowed_pattern: "[0-9A-Za-z+/=]+"
        description: Must be Base64-encoded
# names parameters
  bono_name_0:
    type: string
    description: The VM name
  sprout_name_0:
    type: string
    description: The VM name
  vellum_name_0:
    type: string
    description: The VM name
  homer_name_0:
    type: string
    description: The VM name
  dime_name_0:
    type: string
    description: The VM name
  ellis_name_0:
    type: string
    description: The VM name
  dns_name_0:
    type: string
    description: The VM name


# Network parameters, naming required by ONAP
  admin_plane_net_name:
    type: string
    label: external management network
    description: The external management network


resources:

  clearwater_random_str:
    type: OS::Heat::RandomString
    properties:
      length: 4

  clearwater_instantiated_key_name:
    type: OS::Nova::KeyPair
    properties:
      name:
        str_replace:
          template: pre_base_rand
          params:
            pre: key_
            base: { get_param: vnf_name }
            rand: { get_resource: clearwater_random_str }
      public_key: { get_param: clearwater_pub_key }
      save_private_key: false

  dns:
    type: dns.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      dns_flavor_name: { get_param: dns_flavor_name }
      dns_image_name: { get_param: dns_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      zone: { get_param: zone }
      dnssec_key: { get_param: dnssec_key }
      dns_name_0: { get_param: dns_name_0 }


  ellis:
    type: ellis.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      ellis_flavor_name: { get_param: ellis_flavor_name }
      ellis_image_name: { get_param: ellis_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dn_range_start: { get_param: dn_range_start }
      dn_range_length: { get_param: dn_range_length }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: "" #for ellis etcd_ip is empty
      ellis_name_0: { get_param: ellis_name_0 }

  bono:
    type: bono.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      bono_flavor_name: { get_param: bono_flavor_name }
      bono_image_name: { get_param: bono_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: { get_attr: [ ellis, ellis_ip ] }
      bono_name_0: { get_param: bono_name_0 }

  sprout:
    type: sprout.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      sprout_flavor_name: { get_param: sprout_flavor_name }
      sprout_image_name: { get_param: sprout_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: { get_attr: [ ellis, ellis_ip ] }
      sprout_name_0: { get_param: sprout_name_0 }


  homer:
    type: homer.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      homer_flavor_name: { get_param: homer_flavor_name }
      homer_image_name: { get_param: homer_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: { get_attr: [ ellis, ellis_ip ] }
      homer_name_0: { get_param: homer_name_0 }

  vellum:
    type: vellum.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      vellum_flavor_name: { get_param: vellum_flavor_name }
      vellum_image_name: { get_param: vellum_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: { get_attr: [ ellis, ellis_ip ] }
      vellum_name_0:  { get_param: vellum_name_0 }

  dime:
    type: dime.yaml
    properties:
      vnf_id: { get_param: vnf_id }
      vf_module_id: { get_param: vf_module_id }
      vnf_name: { get_param: vnf_name }
      public_net_id: { get_param: admin_plane_net_name }
      dime_flavor_name: { get_param: dime_flavor_name }
      dime_image_name: { get_param: dime_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      repo_url: { get_param: repo_url }
      zone: { get_param: zone }
      dns_ip: { get_attr: [ dns, dns_ip ] }
      dnssec_key: { get_param: dnssec_key }
      etcd_ip: { get_attr: [ ellis, ellis_ip ] }
      dime_name_0: { get_param: dime_name_0 }

  robot_0_security_group:
    type: OS::Neutron::SecurityGroup
    properties:
      description: security group
      name:
        str_replace:
          template: pre_base_rand
          params:
            pre: robot_sg_
            base: { get_param: vnf_name }
            rand: { get_resource: clearwater_random_str }
      rules: [
        {remote_ip_prefix: 0.0.0.0/0, protocol: tcp, port_range_min: 22, port_range_max: 22},
        {remote_ip_prefix: 0.0.0.0/0, protocol: udp, port_range_min: 161, port_range_max: 162},
        {remote_ip_prefix: 0.0.0.0/0, protocol: tcp, port_range_min: 2380, port_range_max: 2380},
        {remote_ip_prefix: 0.0.0.0/0, protocol: tcp, port_range_min: 4000, port_range_max: 4000},
        {remote_ip_prefix: 0.0.0.0/0, protocol: tcp, port_range_min: 443, port_range_max: 443},
        {remote_ip_prefix: 0.0.0.0/0, protocol: icmp}]

  robot_0_admin_plane_port_0:
    type: OS::Neutron::Port
    properties:
      name:
        str_replace:
          template: pre_base_rand
          params:
            pre: admin_port_0_
            base: { get_param: vnf_name }
            rand: { get_resource: clearwater_random_str }
      network: { get_param: admin_plane_net_name }
      security_groups: [{ get_resource: robot_0_security_group }]

  robot_server_0:
    type: OS::Nova::Server
    properties:
      name:
        str_replace:
          template: pre_base_rand
          params:
            pre: robot_server_0_
            base: { get_param: vnf_name }
            rand: { get_resource: clearwater_random_str }
      flavor: { get_param: robot_flavor_name }
      image: { get_param: robot_image_name }
      key_name: { get_resource: clearwater_instantiated_key_name }
      networks:
        - port: { get_resource: robot_0_admin_plane_port_0 }
      metadata: {vnf_id: { get_param: vnf_id }, vf_module_id: { get_param: vf_module_id }, vnf_name: { get_param: vnf_name }}
      user_data_format: RAW
      user_data:
        str_replace:
          params:
            __zone__: { get_param: zone }
            __DNS_IP_ADDR__: { get_attr: [ dns, dns_ip ] }

          template: |
            #!/bin/bash

            ## activate debug
            set -x

            ## install 'clearwater-live-test' in ubuntu home directory
            ## without this all is installed in root dir
            mkdir /home/ubuntu -p
            cd /home/ubuntu

            sudo apt-get update
            sudo apt-get install build-essential git --yes
            curl -sSL https://rvm.io/mpapis.asc | gpg --import -
            curl -L https://get.rvm.io | bash -s stable
            source /etc/profile.d/rvm.sh
            rvm autolibs enable
            rvm install 1.9.3
            rvm use 1.9.3

            git config --global url."https://".insteadOf git://
            git config --global url."https://github.com/Metaswitch".insteadOf "git@github.com:Metaswitch"
            git clone -b stable https://github.com/Metaswitch/clearwater-live-test.git --recursive
            cd clearwater-live-test
            sudo apt-get install bundler --yes
            sudo bundle install

            ##update dns
            echo "nameserver __DNS_IP_ADDR__" >> /etc/resolvconf/resolv.conf.d/head
            resolvconf -u

            echo "To start live-test run: rake test[__zone__] SIGNUP_CODE=secret"

            #rake test[vimstest.onap.org] SIGNUP_CODE=secret PROXY=84.39.37.62 ELLIS=84.39.34.60