#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2021, 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.                                             #
#--------------------------------------------------------------------------- #

###############################################################################
# This script is used to register a new IP network in the IPAM. The network may
# be selected by a pool of free networks or if an specific network is requested
# its availability maybe checked by the IPAM driver.
#
# The IPAM driver must return an OpenNebula AddressRange definition, potentially
# augmented with network specific variables to be used by VMs (e.g. GATEWAYS,
# MASK...)
#
# STDIN Input:
#   - Base64 encoded XML with AR request
#
# XML format
#  <IPAM_DRIVER_ACTION_DATA>
#    <AR>
#      <TYPE>Type of the Ip (public/global)</TYPE>
#      <SIZE>Number of IPs to allocate</SIZE>
#      <PROVISION_ID>ID</PROVISION_ID>
#    </AR>
#  </IPAM_DRIVER_ACTION_DATA>
#
# The response MUST include IPAM_MAD, TYPE, IP and SIZE attributes, example:
#   - A basic network definition
#       AR = [
#         IPAM_MAD = "equinix",
#         TYPE = "IP4",
#         IP   = "10.0.0.1",
#         SIZE = "255",
#         DEPLOY_ID = "..",
#       ]
#
#   - A complete network definition. Custom attributes (free form, key-value)
#     can be added, named cannot be repeated.
#       AR = [
#         IPAM_MAD = "equinix",
#         TYPE = "IP4",
#         IP   = "10.0.0.2",
#         SIZE = "200",
#         DEPLOY_ID = "..",
#         NETWORK_ADDRESS   = "10.0.0.0",
#         NETWORK_MASK      = "255.255.255.0",
#         GATEWAY           = "10.0.0.1",
#         DNS               = "10.0.0.1",
#         IPAM_ATTR         = "10.0.0.240",
#         OTHER_IPAM_ATTR   = ".mydoamin.com"
#       ]
################################################################################

ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)

if !ONE_LOCATION
    LIB_LOCATION      = '/usr/lib/one'
    EQUINIX_LOCATION  = '/usr/lib/one/ruby/vendors/packethost/lib'
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    LIB_LOCATION      = ONE_LOCATION + '/lib'
    EQUINIX_LOCATION  = ONE_LOCATION + '/lib/ruby/vendors/packethost/lib'
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
if File.directory?(GEMS_LOCATION)
    real_gems_path = File.realpath(GEMS_LOCATION)
    if !defined?(Gem) || Gem.path != [real_gems_path]
        $LOAD_PATH.reject! {|l| l =~ /vendor_ruby/ }

        # Suppress warnings from Rubygems
        # https://github.com/OpenNebula/one/issues/5379
        begin
            verb = $VERBOSE
            $VERBOSE = nil
            require 'rubygems'
            Gem.use_paths(real_gems_path)
        ensure
            $VERBOSE = verb
        end
    end
end
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << EQUINIX_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << LIB_LOCATION + '/oneprovision/lib'

require 'packet'
require 'base64'
require 'nokogiri'
require 'opennebula'
require 'oneprovision'
require 'ipaddr'
require 'digest'

IP_TYPE = %w[public_ipv4 global_ipv4]

DEFAULT_PRIVATE_CIDR = '172.16.0.0/12'

# IP Address class
class IPAddr

    # Add ^ operator to the IPAddr class
    def ^(other)
        clone.set(@addr ^ other.to_i)
    end

    # Add prefix method, to work with older ipadddr & ruby
    def prefix
        case @family
        when Socket::AF_INET
            n = IN4MASK ^ @mask_addr
            i = 32
        when Socket::AF_INET6
            n = IN6MASK ^ @mask_addr
            i = 128
        else
            raise AddressFamilyError, 'unsupported address family'
        end
        while n>0
            n >>= 1
            i -= 1
        end
        i
    end

end

begin
    data = Nokogiri::XML(Base64.decode64(STDIN.read))

    # --------------------------------------------------------------------------
    # Get connection details for the provider
    # --------------------------------------------------------------------------
    provision_id = data.xpath('//AR/PROVISION_ID').text

    if provision_id.empty?
        STDERR.puts 'Missing provision id in address range'
        exit(-1)
    end

    one       = OpenNebula::Client.new
    provision = OneProvision::Provision.new_with_id(provision_id, one)
    rc        = provision.info

    if OpenNebula.is_error?(rc)
        STDERR.puts rc.message
        exit(-1)
    end

    provider = provision.provider
    connect  = provider.body['connection']

    pk_token    = connect['token']
    pk_project  = connect['project']
    pk_facility = connect['facility']

    # --------------------------------------------------------------------------
    # Connect to Equinix and allocate a new IP
    # --------------------------------------------------------------------------
    private_cidr = data.xpath('//AR/PRIVATE_CIDR').text

    if private_cidr.empty?
        private_cidr = DEFAULT_PRIVATE_CIDR
    end

    cidr = IPAddr.new(private_cidr)
    mask = 0x0FFFFFFFF >> cidr.prefix

    equinix            = Packet::Client.new
    equinix.auth_token = pk_token

    ip = Packet::Ip.new
    ip.project_id = pk_project
    ip.facility   = pk_facility
    ip.type       = data.xpath('//AR/EQUINIX_IP_TYPE').text
    ip.quantity   = data.xpath('//AR/SIZE').text.to_i

    if ip.quantity != 1
        STDERR.puts 'Only address ranges of size 1 are supported'
        exit(-1)
    end

    if ip.type.empty?
        ip.type = IP_TYPE[0]
    elsif !IP_TYPE.include?(ip.type)
        STDERR.puts "Type #{ip.type} not supported. " \
                    "Must be: #{IP_TYPE.join(', ')}"
        exit(-1)
    end

    equinix.create_ip(ip)

    ipmd5 = Digest::MD5.hexdigest(ip.address.to_s).to_i(16) & mask
    eip   = IPAddr.new(ipmd5, Socket::AF_INET)

    ipvm = (eip & mask) | cidr
    ipgw = ipvm ^ 1

    puts <<-EOF
        AR = [
            TYPE     = "IP4",
            IP       = "#{ipvm}",
            SIZE     = "#{ip.quantity}",
            IPAM_MAD = "equinix",
            GATEWAY  = "#{ipgw}",
            EXTERNAL_IP  = "#{ip.address}",
            NETWORK_MASK = "255.255.255.254",
            EQUINIX_IP_ID = "#{ip.id}",
            PROVISION_ID = "#{provision_id}"
        ]
    EOF
rescue StandardError => e
    STDERR.puts e.to_s
    exit(-1)
end
