#!/bin/bash

function gen_config {
    echo "Generating sample config"
    DEMO_NET=$(openstack network list | awk '/demo-net/ { print $2 }')
    IMAGE_ID=$(openstack image list | awk '/cirros/ { print $2 }')
    cat > ./kolla-sample-vim.yaml <<EOF
auth_url: ${OS_AUTH_URL}
username: ${OS_USERNAME}
password: ${OS_PASSWORD}
project_name: ${OS_PROJECT_NAME}
project_domain_name: ${OS_PROJECT_DOMAIN_NAME}
user_domain_name: ${OS_USER_DOMAIN_NAME}
EOF
    cat > ./kolla-sample-vnfd.yaml <<EOF
tosca_definitions_version: tosca_simple_profile_for_nfv_1_0_0

description: Demo example

metadata:
  template_name: sample-tosca-vnfd

topology_template:
  node_templates:
    VDU1:
      type: tosca.nodes.nfv.VDU.Tacker
      capabilities:
        nfv_compute:
          properties:
            num_cpus: 1
            mem_size: 512 MB
            disk_size: 0 GB
      properties:
        image: ${IMAGE_ID}
        availability_zone: nova
        mgmt_driver: noop
        user_data_format: RAW
        user_data: |
          #!/bin/sh
          echo 1 > /proc/sys/net/ipv4/ip_forward

    CP11:
      type: tosca.nodes.nfv.CP.Tacker
      properties:
        management: true
        order: 0
        anti_spoofing_protection: false
      requirements:
        - virtualLink:
            node: VL1
        - virtualBinding:
            node: VDU1

    VL1:
      type: tosca.nodes.nfv.VL
      properties:
        network_name: ${DEMO_NET}
        vendor: Tacker
EOF
}

function deploy {
    echo "Registering sample VIM"
    openstack vim register --config-file ./kolla-sample-vim.yaml --description "kolla sample vim" --is-default kolla-sample-vim
    echo "Creating sample VNFD"
    openstack vnf descriptor create --vnfd-file ./kolla-sample-vnfd.yaml kolla-sample-vnfd
    echo "Creating sample VNF"
    VNFD_ID=$(openstack vnf descriptor list | awk '/kolla-sample-vnfd/ { print $2 }')
    openstack vnf create --vnfd-id ${VNFD_ID} kolla-sample-vnf
}

gen_config
deploy
