<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2015 Intel Corporation.
# Copyright 2015 <NAME> <isaku.yamahata at intel com>
#                               <isaku.yamahata at gmail com>
# 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.
#
# @author: <NAME>, Intel Corporation.
# shamelessly many codes are stolen from gbp simplechain_driver.py

import time
import yaml

from heatclient import client as heat_client
from heatclient import exc as heatException
from oslo_config import cfg

from tacker.common import log
from tacker.openstack.common import jsonutils
from tacker.openstack.common import log as logging
from tacker.vm.drivers import abstract_driver


LOG = logging.getLogger(__name__)
CONF = cfg.CONF
OPTS = [
    cfg.StrOpt('heat_uri',
               default='http://localhost:8004/v1',
               help=_("Heat server address to create services "
                      "specified in the service chain.")),
    cfg.IntOpt('stack_retries',
               default=10,
               help=_("Number of attempts to retry for stack deletion")),
    cfg.IntOpt('stack_retry_wait',
               default=5,
               help=_("Wait time between two successive stack delete "
                      "retries")),
]
CONF.register_opts(OPTS, group='servicevm_heat')
STACK_RETRIES = cfg.CONF.servicevm_heat.stack_retries
STACK_RETRY_WAIT = cfg.CONF.servicevm_heat.stack_retry_wait

HEAT_TEMPLATE_BASE = """
heat_template_version: 2013-05-23
"""


class DeviceHeat(abstract_driver.DeviceAbstractDriver):

    """Heat driver of hosting device."""

    def __init__(self):
        super(DeviceHeat, self).__init__()

    def get_type(self):
        return 'heat'

    def get_name(self):
        return 'heat'

    def get_description(self):
        return 'Heat infra driver'

    @log.log
    def create(self, plugin, context, device):
        LOG.debug(_('device %s'), device)
        heatclient_ = HeatClient(context)
        attributes = device['device_template']['attributes'].copy()
        vnfd_yaml = attributes.pop('vnfd', None)
        fields = dict((key, attributes.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in attributes)
        for key in ('files', 'parameters'):
            if key in attributes:
                fields[key] = jsonutils.loads(attributes.pop(key))
        fields.setdefault('parameters', {}).update(attributes)

        # overwrite parameters with given kwargs for device creation
        kwargs = device['kwargs'].copy()
        fields.update(dict((key, kwargs.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in kwargs))
        for key in ('files', 'parameters'):
            if key in kwargs:
                kwargs[key] = jsonutils.loads(kwargs.pop(key))
        fields['parameters'].update(kwargs)
        LOG.debug('vnfd_yaml %s', vnfd_yaml)
        if vnfd_yaml is not None:
            assert 'template' not in fields
            assert 'template_url' not in fields
            template_dict = yaml.load(HEAT_TEMPLATE_BASE)

            vnfd_dict = yaml.load(vnfd_yaml)
            LOG.debug('vnfd_dict %s', vnfd_dict)
            KEY_LIST = (('description', 'description'),
                        )
            for (key, vnfd_key) in KEY_LIST:
                if vnfd_key in vnfd_dict:
                    template_dict[key] = vnfd_dict[vnfd_key]

            for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items():
                template_dict.setdefault('resources', {})[vdu_id] = {
                    "type": "OS::Nova::Server"
                }
                resource_dict = template_dict['resources'][vdu_id]
                KEY_LIST = (('image', 'vm_image'),
                            ('flavor', 'instance_type'))
                resource_dict['properties'] = {}
                properties = resource_dict['properties']
                for (key, vdu_key) in KEY_LIST:
                    properties[key] = vdu_dict[vdu_key]
                if 'network_interfaces' in vdu_dict:
                    properties['networks'] = (
                        vdu_dict['network_interfaces'].values())
                if ('placement_policy' in vdu_dict and
                    'availability_zone' in vdu_dict['placement_policy']):
                    properties['availability_zone'] = vdu_dict[
                        'placement_policy']['availability_zone']

                # monitoring_policy = vdu_dict.get('monitoring_policy', None)
                # failure_policy = vdu_dict.get('failure_policy', None)

            fields['template'] = yaml.dump(template_dict)

        if 'stack_name' not in fields:
            name = (__name__ + '_' + self.__class__.__name__ + '-' +
                    device['id'])
            fields['stack_name'] = name

        # service context is ignored
        LOG.debug(_('service_context: %s'), device.get('service_context', []))

        LOG.debug(_('fields: %s'), fields)
        stack = heatclient_.create(fields)
        return stack['stack']['id']

    def create_wait(self, plugin, context, device_id):
        heatclient_ = HeatClient(context)

        stack = heatclient_.get(device_id)
        status = stack.stack_status
        stack_retries = STACK_RETRIES
        while status == 'CREATE_IN_PROGRESS' and stack_retries > 0:
            time.sleep(STACK_RETRY_WAIT)
            try:
                stack = heatclient_.get(device_id)<commit_msg>heat driver: teach config drive<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2015 Intel Corporation.
# Copyright 2015 <NAME> <isaku.yamahata at intel com>
#                               <isaku.yamahata at gmail com>
# 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.
#
# @author: <NAME>, Intel Corporation.
# shamelessly many codes are stolen from gbp simplechain_driver.py

import time
import yaml

from heatclient import client as heat_client
from heatclient import exc as heatException
from oslo_config import cfg

from tacker.common import log
from tacker.openstack.common import jsonutils
from tacker.openstack.common import log as logging
from tacker.vm.drivers import abstract_driver


LOG = logging.getLogger(__name__)
CONF = cfg.CONF
OPTS = [
    cfg.StrOpt('heat_uri',
               default='http://localhost:8004/v1',
               help=_("Heat server address to create services "
                      "specified in the service chain.")),
    cfg.IntOpt('stack_retries',
               default=10,
               help=_("Number of attempts to retry for stack deletion")),
    cfg.IntOpt('stack_retry_wait',
               default=5,
               help=_("Wait time between two successive stack delete "
                      "retries")),
]
CONF.register_opts(OPTS, group='servicevm_heat')
STACK_RETRIES = cfg.CONF.servicevm_heat.stack_retries
STACK_RETRY_WAIT = cfg.CONF.servicevm_heat.stack_retry_wait

HEAT_TEMPLATE_BASE = """
heat_template_version: 2013-05-23
"""


class DeviceHeat(abstract_driver.DeviceAbstractDriver):

    """Heat driver of hosting device."""

    def __init__(self):
        super(DeviceHeat, self).__init__()

    def get_type(self):
        return 'heat'

    def get_name(self):
        return 'heat'

    def get_description(self):
        return 'Heat infra driver'

    @log.log
    def create(self, plugin, context, device):
        LOG.debug(_('device %s'), device)
        heatclient_ = HeatClient(context)
        attributes = device['device_template']['attributes'].copy()
        vnfd_yaml = attributes.pop('vnfd', None)
        fields = dict((key, attributes.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in attributes)
        for key in ('files', 'parameters'):
            if key in attributes:
                fields[key] = jsonutils.loads(attributes.pop(key))
        fields.setdefault('parameters', {}).update(attributes)

        # overwrite parameters with given kwargs for device creation
        kwargs = device['kwargs'].copy()
        config_yaml = kwargs.pop('config', None)
        fields.update(dict((key, kwargs.pop(key)) for key
                      in ('stack_name', 'template_url', 'template')
                      if key in kwargs))
        for key in ('files', 'parameters'):
            if key in kwargs:
                kwargs[key] = jsonutils.loads(kwargs.pop(key))
        fields['parameters'].update(kwargs)
        LOG.debug('vnfd_yaml %s', vnfd_yaml)
        if vnfd_yaml is not None:
            assert 'template' not in fields
            assert 'template_url' not in fields
            template_dict = yaml.load(HEAT_TEMPLATE_BASE)

            vnfd_dict = yaml.load(vnfd_yaml)
            LOG.debug('vnfd_dict %s', vnfd_dict)
            KEY_LIST = (('description', 'description'),
                        )
            for (key, vnfd_key) in KEY_LIST:
                if vnfd_key in vnfd_dict:
                    template_dict[key] = vnfd_dict[vnfd_key]

            for vdu_id, vdu_dict in vnfd_dict.get('vdus', {}).items():
                template_dict.setdefault('resources', {})[vdu_id] = {
                    "type": "OS::Nova::Server"
                }
                resource_dict = template_dict['resources'][vdu_id]
                KEY_LIST = (('image', 'vm_image'),
                            ('flavor', 'instance_type'))
                resource_dict['properties'] = {}
                properties = resource_dict['properties']
                for (key, vdu_key) in KEY_LIST:
                    properties[key] = vdu_dict[vdu_key]
                if 'network_interfaces' in vdu_dict:
                    properties['networks'] = (
                        vdu_dict['network_interfaces'].values())
                if ('placement_policy' in vdu_dict and
                    'availability_zone' in vdu_dict['placement_policy']):
                    properties['availability_zone'] = vdu_dict[
                        'placement_policy']['availability_zone']
                if 'config' in vdu_dict:
                    properties['config_drive'] = True
                    properties.setdefault('metadata', {}).update(
                        vdu_dict['config'])

                # monitoring_policy = vdu_dict.get('monitoring_policy', None)
                # failure_policy = vdu_dict.get('failure_policy', None)

            if config_yaml is not None:
                config_dict = yaml.load(config_yaml)
                resources = template_dict.setdefault('resources', {})
                for vdu_id, vdu_dict in config_dict.get('vdus', {}).items():
                    if vdu_id not in resources:
                        continue
                    config = vdu_dict.get('config', None)
                    if not config:
                        continue
                    properties = resources[vdu_id].setdefault('properties', {})
                    properties['config_drive'] = True
                    properties.setdefault('metadata', {}).update(config)

            fields['template'] = yaml.dump(template_dict)

        if 'stack_name' not in fields:
            name = (__name__ + '_' + self.__class__.__name__ + '-' +
                    device['id'])
            fields['stack_name'] = name

        # service context is ignored
        LOG.debug(_('service_context: %s'), device.get('service_context', []))

        LOG.debug(_('fields: %s'), fields)
        LOG.debug(_('template: %s'), fields['template'])
        stack = heatclient_.create(fields)
        return stack['stack']['id']

    def create_wait(self, plugin, context, device_id):
        heatclient_ = HeatClient(context)

        stack = heatclient_.get(device_id)
        status = stack.stack_status
        stack_retries = STACK_RETRIES
        while status == 'CREATE_IN_PROGRESS' and stack_retries > 0:
            time.sleep(STACK_RETRY_WAIT)
            try:
                stack = heatclient_.get(device_id)
{'fraction_non_alphanumeric': 0.10485347985347986, 'fraction_numerical': 0.004884004884004884, 'mean_word_length': 2.204156479217604, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015961', 'n_tokens_mistral': 3720, 'n_tokens_neox': 3533, 'n_words': 928}