<commit_before># Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

from cloudify import ctx
from cloudify import exceptions as cfy_exc
from cloudify.decorators import operation
from vcloud_plugin_common import (with_vca_client, wait_for_task,
                                  get_vcloud_config, get_mandatory)
import collections
from network_plugin import (check_ip, is_valid_ip_range, is_separate_ranges,
                            is_ips_in_same_subnet, save_gateway_configuration,
                            get_network_name, is_network_exists)


VCLOUD_NETWORK_NAME = 'vcloud_network_name'
ADD_POOL = 1
DELETE_POOL = 2


@operation
@with_vca_client
def create(vca_client, **kwargs):
    vdc_name = get_vcloud_config()['vdc']
    if ctx.node.properties['use_external_resource']:
        network_name = ctx.node.properties['resource_id']
        if not is_network_exists(vca_client, network_name):
            raise cfy_exc.NonRecoverableError(
                "Can't find external resource: {0}".format(network_name))
        ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
        ctx.logger.info(
            "External resource {0} has been used".format(network_name))
        return
    net_prop = ctx.node.properties["network"]
    network_name = get_network_name(ctx.node.properties)
    if network_name in _get_network_list(vca_client,
                                         get_vcloud_config()['vdc']):
        raise cfy_exc.NonRecoverableError(
            "Network {0} already exists, but parameter "
            "'use_external_resource' is 'false' or absent"
            .format(network_name))

    ip = _split_adresses(net_prop['static_range'])
    gateway_name = net_prop['edge_gateway']
    if not vca_client.get_gateway(vdc_name, gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))
    start_address = ip.start
    end_address = ip.end
    gateway_ip = net_prop["gateway_ip"]
    netmask = net_prop["netmask"]
    dns_list = net_prop["dns"]
    dns1 = dns_list[0]
    dns2 = dns_list[1] if len(dns_list) > 1 else ""
    dns_suffix = net_prop.get("dns_suffix")
    success, result = vca_client.create_vdc_network(
        vdc_name, network_name, gateway_name, start_address,
        end_address, gateway_ip, netmask, dns1, dns2, dns_suffix)
    if success:
        ctx.logger.info("Network {0} has been successfully created."
                        .format(network_name))
    else:
        raise cfy_exc.NonRecoverableError(
            "Could not create network {0}: {1}".format(network_name, result))
    wait_for_task(vca_client, result)
    ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
    _dhcp_operation(vca_client, network_name, ADD_POOL)


@operation
@with_vca_client
def delete(vca_client, **kwargs):
    if ctx.node.properties['use_external_resource'] is True:
        del ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME]
        ctx.logger.info("Network was not deleted - external resource has"
                        " been used")
        return
    network_name = get_network_name(ctx.node.properties)
    _dhcp_operation(vca_client, network_name, DELETE_POOL)
    success, task = vca_client.delete_vdc_network(
        get_vcloud_config()['vdc'], network_name)
    if success:
        ctx.logger.info(
            "Network {0} has been successful deleted.".format(network_name))
    else:
        raise cfy_exc.NonRecoverableError(
            "Could not delete network {0}".format(network_name))
    wait_for_task(vca_client, task)


@operation
@with_vca_client
def creation_validation(vca_client, **kwargs):
    network_name = get_network_name(ctx.node.properties)
    ctx.logger.info("Validation cloudify.vcloud.nodes.Network node: {0}"
                    .format(network_name))
    if is_network_exists(vca_client, network_name):
        if ctx.node.properties.get('use_external_resource'):
            # TODO: check: default gateway must exists
            return
        else:
            raise cfy_exc.NonRecoverableError(
                "Network already exsists: {0}".format(network_name))

    net_prop = get_mandatory(ctx.node.properties, "network")
    gateway_name = get_mandatory(net_prop, 'edge_gateway')
    if not vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))

    static_ip = _split_adresses(get_mandatory(net_prop, 'static_range'))
    check_ip(static_ip.start)
    check_ip(static_ip.end)
    dns_list = get_mandatory(net_prop, "dns")
    if dns_list:
        check_ip(dns_list[0])
        if len(dns_list) > 1:
            check_ip(dns_list[1])
    else:
        raise cfy_exc.NonRecoverableError(
            "DNS servers not defined")

    gateway_ip = check_ip(get_mandatory(net_prop, "gateway_ip"))
    netmask = check_ip(get_mandatory(net_prop, "netmask"))

    ips = [gateway_ip, static_ip.start, static_ip.end]
    dhcp = net_prop.get("dhcp")
    if dhcp:
        dhcp_range = get_mandatory(net_prop["dhcp"], "dhcp_range")
        if not dhcp_range:
            raise cfy_exc.NonRecoverableError(
                "Parameter 'dhcp_range' not defined")
        dhcp_ip = _split_adresses(dhcp_range)
        if not is_separate_ranges(static_ip, dhcp_ip):
            raise cfy_exc.NonRecoverableError(
                "Static_range and dhcp_range is overlapped.")
        ips.extend([dhcp_ip.start, dhcp_ip.end])
    if not is_ips_in_same_subnet(ips, netmask):
            raise cfy_exc.NonRecoverableError(
                "IP addresses in divverent subnets.")


def _dhcp_operation(vca_client, network_name, operation):
    dhcp_settings = ctx.node.properties['network'].get('dhcp')
    if dhcp_settings is None:
        return
    gateway_name = ctx.node.properties["network"]['edge_gateway']
    gateway = vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name)
    if not gateway:
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found!".format(gateway_name))

    if operation == ADD_POOL:<commit_msg>CFY-2545: Refactoring<commit_after># Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

from cloudify import ctx
from cloudify import exceptions as cfy_exc
from cloudify.decorators import operation
from vcloud_plugin_common import (with_vca_client, wait_for_task,
                                  get_vcloud_config, get_mandatory)
import collections
from network_plugin import (check_ip, is_valid_ip_range, is_separate_ranges,
                            is_ips_in_same_subnet, save_gateway_configuration,
                            get_network_name, is_network_exists)


VCLOUD_NETWORK_NAME = 'vcloud_network_name'
ADD_POOL = 1
DELETE_POOL = 2


@operation
@with_vca_client
def create(vca_client, **kwargs):
    vdc_name = get_vcloud_config()['vdc']
    if ctx.node.properties['use_external_resource']:
        network_name = ctx.node.properties['resource_id']
        if not is_network_exists(vca_client, network_name):
            raise cfy_exc.NonRecoverableError(
                "Can't find external resource: {0}".format(network_name))
        ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
        ctx.logger.info(
            "External resource {0} has been used".format(network_name))
        return
    net_prop = ctx.node.properties["network"]
    network_name = get_network_name(ctx.node.properties)
    if network_name in _get_network_list(vca_client,
                                         get_vcloud_config()['vdc']):
        raise cfy_exc.NonRecoverableError(
            "Network {0} already exists, but parameter "
            "'use_external_resource' is 'false' or absent"
            .format(network_name))

    ip = _split_adresses(net_prop['static_range'])
    gateway_name = net_prop['edge_gateway']
    if not vca_client.get_gateway(vdc_name, gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))
    start_address = ip.start
    end_address = ip.end
    gateway_ip = net_prop["gateway_ip"]
    netmask = net_prop["netmask"]
    dns1 = ""
    dns2 = ""
    dns_list = net_prop.get("dns")
    if dns_list:
        dns1 = dns_list[0]
        if len(dns_list) > 1:
            dns2 = dns_list[1]
    dns_suffix = net_prop.get("dns_suffix")
    success, result = vca_client.create_vdc_network(
        vdc_name, network_name, gateway_name, start_address,
        end_address, gateway_ip, netmask, dns1, dns2, dns_suffix)
    if success:
        ctx.logger.info("Network {0} has been successfully created."
                        .format(network_name))
    else:
        raise cfy_exc.NonRecoverableError(
            "Could not create network {0}: {1}".format(network_name, result))
    wait_for_task(vca_client, result)
    ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name
    _dhcp_operation(vca_client, network_name, ADD_POOL)


@operation
@with_vca_client
def delete(vca_client, **kwargs):
    if ctx.node.properties['use_external_resource'] is True:
        del ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME]
        ctx.logger.info("Network was not deleted - external resource has"
                        " been used")
        return
    network_name = get_network_name(ctx.node.properties)
    _dhcp_operation(vca_client, network_name, DELETE_POOL)
    success, task = vca_client.delete_vdc_network(
        get_vcloud_config()['vdc'], network_name)
    if success:
        ctx.logger.info(
            "Network {0} has been successful deleted.".format(network_name))
    else:
        raise cfy_exc.NonRecoverableError(
            "Could not delete network {0}".format(network_name))
    wait_for_task(vca_client, task)


@operation
@with_vca_client
def creation_validation(vca_client, **kwargs):
    network_name = get_network_name(ctx.node.properties)
    ctx.logger.info("Validation cloudify.vcloud.nodes.Network node: {0}"
                    .format(network_name))
    if is_network_exists(vca_client, network_name):
        if ctx.node.properties.get('use_external_resource'):
            # TODO: check: default gateway must exists
            return
        else:
            raise cfy_exc.NonRecoverableError(
                "Network already exsists: {0}".format(network_name))

    net_prop = get_mandatory(ctx.node.properties, "network")
    gateway_name = get_mandatory(net_prop, 'edge_gateway')
    if not vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name):
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found".format(gateway_name))

    static_ip = _split_adresses(get_mandatory(net_prop, 'static_range'))
    check_ip(static_ip.start)
    check_ip(static_ip.end)
    dns_list = net_prop.get("dns")
    if dns_list:
        for ip in dns_list:
            check_ip(ip)
    gateway_ip = check_ip(get_mandatory(net_prop, "gateway_ip"))
    netmask = check_ip(get_mandatory(net_prop, "netmask"))

    ips = [gateway_ip, static_ip.start, static_ip.end]
    dhcp = net_prop.get("dhcp")
    if dhcp:
        dhcp_range = get_mandatory(net_prop["dhcp"], "dhcp_range")
        if not dhcp_range:
            raise cfy_exc.NonRecoverableError(
                "Parameter 'dhcp_range' not defined")
        dhcp_ip = _split_adresses(dhcp_range)
        if not is_separate_ranges(static_ip, dhcp_ip):
            raise cfy_exc.NonRecoverableError(
                "Static_range and dhcp_range is overlapped.")
        ips.extend([dhcp_ip.start, dhcp_ip.end])
    if not is_ips_in_same_subnet(ips, netmask):
            raise cfy_exc.NonRecoverableError(
                "IP addresses in divverent subnets.")


def _dhcp_operation(vca_client, network_name, operation):
    dhcp_settings = ctx.node.properties['network'].get('dhcp')
    if dhcp_settings is None:
        return
    gateway_name = ctx.node.properties["network"]['edge_gateway']
    gateway = vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name)
    if not gateway:
        raise cfy_exc.NonRecoverableError(
            "Gateway {0} not found!".format(gateway_name))

    if operation == ADD_POOL:
{'fraction_non_alphanumeric': 0.09261895841139003, 'fraction_numerical': 0.0051704758336455605, 'mean_word_length': 3.1732332707942463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5868432', 'n_tokens_mistral': 4308, 'n_tokens_neox': 4209, 'n_words': 903}