#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# 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.                                             #
#--------------------------------------------------------------------------- #

source $(dirname $0)/kvmrc
source $(dirname $0)/../../scripts_common.sh

DOMAIN=$1

get_nic_information "ATTACH='YES'"

# defaults
[ -z "${MODEL}" ] && [ -n "${DEFAULT_ATTACH_NIC_MODEL}" ] && \
    MODEL="${DEFAULT_ATTACH_NIC_MODEL}"

[ -z "${FILTER}" ] && [ -n "${DEFAULT_ATTACH_NIC_FILTER}" ] && \
    FILTER="${DEFAULT_ATTACH_NIC_FILTER}"

# functions
function xml_esc
{
   R=${1//\'/&apos;}
   R=${R//\"/&quot;}
   R=${R//\&/&amp;}
   R=${R//\</&lt;}
   R=${R//\>/&gt;}
   echo "${R}"
}

# interface XML
DEVICE=''

if [ -z "${BRIDGE}" ]; then
    DEVICE+="<interface type='ethernet'>"
else
    DEVICE+="<interface type='bridge'>"
    [ "${VN_MAD}" = 'ovswitch' ] && DEVICE+=" <virtualport type='openvswitch'/>"
    DEVICE+=" <source bridge='$(xml_esc "${BRIDGE}")'/>"
fi

[ -n "${MAC}" ] && DEVICE+=" <mac address='$(xml_esc "${MAC}")'/>"
[ -n "${NIC_TARGET}" ] && DEVICE+=" <target dev='$(xml_esc "${NIC_TARGET}")'/>"
[ -n "${ORDER}" ] && DEVICE+=" <boot order='$(xml_esc "${ORDER}")'/>"
[ -n "${SCRIPT}" ] && DEVICE+=" <script path='$(xml_esc "${SCRIPT}")'/>"
[ -n "${MODEL}" ] && DEVICE+=" <model type='$(xml_esc "${MODEL}")'/>"

if [ -n "${IP}" ] && [ -n "${FILTER}" ]; then
    DEVICE+=" <filterref filter='$(xml_esc "${FILTER}")'>"
    DEVICE+=" <parameter name='IP' value='$(xml_esc "${IP}")'/>"
    [ -n "${VROUTER_IP}" ] && DEVICE+=" <parameter name='IP' value='$(xml_esc "${VROUTER_IP}")'/>"
    DEVICE+=" </filterref>"
fi

if [ -n "${INBOUND_AVG_BW}${INBOUND_PEAK_BW}${INBOUND_PEAK_KB}" ] || \
   [ -n "${OUTBOUND_AVG_BW}${OUTBOUND_PEAK_BW}${OUTBOUND_PEAK_KB}" ];
then
    DEVICE+=" <bandwidth>"

    if [ -n "${INBOUND_AVG_BW}${INBOUND_PEAK_BW}${INBOUND_PEAK_KB}" ]; then
        DEVICE+=" <inbound"

        [ -n "${INBOUND_AVG_BW}" ] && \
            DEVICE+=" average='$(xml_esc "${INBOUND_AVG_BW}")'"
        [ -n "${INBOUND_PEAK_BW}" ] && \
            DEVICE+=" peak='$(xml_esc "${INBOUND_PEAK_BW}")'"
        [ -n "${INBOUND_PEAK_KB}" ] && \
            DEVICE+=" burst='$(xml_esc "${INBOUND_PEAK_KB}")'"

        DEVICE+=" />"
    fi

    if [ -n "${OUTBOUND_AVG_BW}${OUTBOUND_PEAK_BW}${OUTBOUND_PEAK_KB}" ]; then
        DEVICE+=" <outbound"

        [ -n "${OUTBOUND_AVG_BW}" ] && \
            DEVICE+=" average='$(xml_esc "${OUTBOUND_AVG_BW}")'"
        [ -n "${OUTBOUND_PEAK_BW}" ] && \
            DEVICE+=" peak='$(xml_esc "${OUTBOUND_PEAK_BW}")'"
        [ -n "${OUTBOUND_PEAK_KB}" ] && \
            DEVICE+=" burst='$(xml_esc "${OUTBOUND_PEAK_KB}")'"

        DEVICE+=" />"
    fi

    DEVICE+=" </bandwidth>"
fi

DEVICE+=" </interface>"

# device attach
ATTACH_CMD=$(cat <<EOF
virsh --connect $LIBVIRT_URI attach-device $DOMAIN <(
cat <<EOT
$DEVICE
EOT
)
EOF
)

multiline_exec_and_log "$ATTACH_CMD" \
    "Could not attach NIC ${NIC_ID} ($MAC) to $DOMAIN"
