#!/bin/bash

# **** License ****
# Copyright (c) 2017-2019, AT&T Intellectual Property. All rights reserved.
# Copyright (c) 2015 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
#
# **** End License ****


# This script performs configuration of a dataplane interface based on
# the contents the machine.id injected by the nova(openstack)
# on VMware.  The script configures the first dataplane
# interface during the first boot with the valids from the config.
#
#
# In all cases, the parameter values are checked against the current
# configuration and only added if not present.
#

# Only if on vmware host.
! /usr/bin/vmware-checkvm > /dev/null 2>&1 && exit 0


#
# Required to run this as a login shell under configd. 
# So do that.
#
/opt/vyatta/sbin/lu -user configd -- /bin/vcli <<"EOF"


VMTOOLSD=/usr/bin/vmtoolsd
MACHINE_ID=/tmp/ovf_env.xml
COMMIT=0

# Always first interface
intfs=( $(cli-shell-api getTmplAllowed interfaces dataplane | tr -d \''\') )
IFNAME=${intfs[0]}
[[ -z "$IFNAME" ]] && logx "Unable to obtain interface name" 1

noninteractive

# Logging function
function log {
	echo "$1"
	logger "auto-config: " "$1"
}

# Log and exit.
function logx {
	log "$1 exit: $2"
	/bin/rm -f $CONFIG
	exit $2
}

function mask2cidr {
    nbits=0
    arr=$(echo $1 | tr "." "\n")
    for dec in $arr ; do
        case $dec in
            255) let nbits+=8;;
            254) let nbits+=7 ; break ;;
            252) let nbits+=6 ; break ;;
            248) let nbits+=5 ; break ;;
            240) let nbits+=4 ; break ;;
            224) let nbits+=3 ; break ;;
            192) let nbits+=2 ; break ;;
            128) let nbits+=1 ; break ;;
            0);;
            *) logx "mask2cidr:Error: $dec is not recognised" 1
        esac
    done
    return "$nbits"
}

# Lookup
function lookup {
       for i in $(list $1)
       do
               [ $i == $2 ] && return 0
       done
       return 1
}

function lookup_exist {
       for i in $(list $1)
       do
               return 0
       done
       return 1
}



# check/set addr
function check_addr {

       if ( lookup "interfaces dataplane $IFNAME address" $1 )
	then
		logx "$1 exists on $IFNAME, ignoring and exiting" 0
	else
		log "set $IFNAME address to $1"
		output=$(set interfaces dataplane $IFNAME address $1)
		[ $? != 0 ] && logx "set interface $IFNAME to $1 failed:  $output" 1
		COMMIT=1
	fi
}

# check route
function check_route {

	if ( lookup "protocols static $1 $2 next-hop" $3)
	then
		logx "Route $2 exists, ignoring and exiting" 0
		return
	else
		log "set $1 $2 next-hop $3"
		output=$(set protocols static $1 $2 next-hop $3)
		[ $? != 0 ] && logx "set $1 $2 next-hop $3 failed: $output" 1
		COMMIT=1
	fi
}

function commit_and_save {
        if [ "$COMMIT" == "1" ]
        then
                log "commiting changes"
                output=$(commit)
                [ $? != 0 ] && logx "commit failed: $output" 1
                save
        fi
}



#
# Check if its openstack deployed image
#
log "Checking if openstack deployed image"
$VMTOOLSD --cmd 'machine.id.get' > $MACHINE_ID
[[ ! -r $MACHINE_ID || ! -s $MACHINE_ID ]] && logx "No MACHINE ID data, exiting..." 0

OAM_INTERFACE=`cat $MACHINE_ID | awk -F'#' '{print $1}'`
MAC_MACHINE_ID=`echo $OAM_INTERFACE | awk -F';' '{print $1}'`
MAC=`ip addr show dev $IFNAME | grep 'link/ether' | awk '{print $2}'`
[[ "$MAC_MACHINE_ID" == "" || "$MAC" != "$MAC_MACHINE_ID" ]] && logx "Exiting" 0

#PARSE
IPV4=`echo $OAM_INTERFACE | awk -F';' '{print $2}'`
IPV4_NETMASK=`echo $OAM_INTERFACE | awk -F';' '{print $3}'`
IPV4_GATEWAY=`echo $OAM_INTERFACE | awk -F';' '{print $4}'`


log "Confirmed Openstack Orchestrated Image: configuring $IFNAME"

# Configuration Mode
configure


# IPV4 address
mask2cidr $IPV4_NETMASK
IPV4_CIDR=$?
[ $IPV4 ] && [ $IPV4_NETMASK ]  && check_addr "$IPV4/$IPV4_CIDR"

# SETTING IPV4 DEFAULT ROUTE
[ $IPV4_GATEWAY ] && check_route "route" 0.0.0.0/0 $IPV4_GATEWAY


commit_and_save
end configure
logx "openstack init finished" 0

# End of document
EOF
