so_manager.py 41.2 KB
Newer Older
Balazs's avatar
Balazs committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
# Copyright 2014-2015 Zuercher Hochschule fuer Angewandte Wissenschaften
# Copyright (c) 2013-2015, Intel Performance Learning Solutions Ltd, Intel Corporation.
# 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 distutils import dir_util
import json
from mako.template import Template  # XXX unneeded
import os
import random
import shutil
import tempfile
import time
from threading import Thread
from urlparse import urlparse
import uuid

from occi.core_model import Resource, Link
from sm.config import CONFIG
from sm.log import LOG
from sm.retry_http import http_retriable_request


__author__ = 'andy'

HTTP = 'http://'
WAIT = int(CONFIG.get('cloud_controller', 'wait_time', 2000))
ATTEMPTS = int(CONFIG.get('cloud_controller', 'max_attempts', 5))


class ServiceParameters:
    def __init__(self):
        self.service_params = {}
        service_params_file_path = CONFIG.get('service_manager', 'service_params', '')
        if len(service_params_file_path) > 0:
            try:
                with open(service_params_file_path) as svc_params_content:
                    self.service_params = json.load(svc_params_content)
                    svc_params_content.close()
            except ValueError:  # as e:
                LOG.error("Invalid JSON sent as service config file")
            except IOError:  # as e:
                LOG.error('Cannot find the specified parameters file: ' + service_params_file_path)
        else:
            LOG.warn("No service parameters file found in config file, setting internal params to empty.")

    def service_parameters(self, state='', content_type='text/occi'):
        # takes the internal parameters defined for the lifecycle phase...
        #       and combines them with the client supplied parameters
        if content_type == 'text/occi':
            params = []
            # get the state specific internal parameters
            try:
                params = self.service_params[state]
            except KeyError:  # as err:
                LOG.warn('The requested states parameters are not available: "' + state + '"')

            # get the client supplied parameters if any
            try:
                for param in self.service_params['client_params']:
                    params.append(param)
            except KeyError:  # as err:
                LOG.info('No client params')

            header = ''
            for param in params:
                if param['type'] == 'string':
                    value = '"' + param['value'] + '"'
                else:
                    value = str(param['value'])

                header = header + param['name'] + '=' + value + ', '

            return header[0:-2]
        else:
            LOG.error('Content type not supported: ' + content_type)

    def add_client_params(self, params={}):
        # adds user supplied parameters from the instantiation request of a service
        client_params = []

        for k, v in params.items():
            param_type = 'number'
            if (v.startswith('"') or v.startswith('\'')) and (v.endswith('"') or v.endswith('\'')):
                param_type = 'string'
                v = v[1:-1]
            param = {'name': k, 'value': v, 'type': param_type}

            client_params.append(param)

        self.service_params['client_params'] = client_params


if __name__ == '__main__':
    sp = ServiceParameters()
    cp = {
        'test': '1',
        'test.test': '"astring"'
    }
    sp.add_client_params(cp)

    p = sp.service_parameters('initialise')
    print p


class AsychExe(Thread):
    """
    Only purpose of this thread is to execute a list of tasks sequentially
    as a background "thread".
    """
    def __init__(self, tasks, registry=None):
        super(AsychExe, self).__init__()
        self.registry = registry
        self.tasks = tasks

    def run(self):
        super(AsychExe, self).run()
        LOG.debug('Starting AsychExe thread')

        for task in self.tasks:
            entity, extras = task.run()
            if self.registry:
                LOG.debug('Updating entity in registry')
                self.registry.add_resource(key=entity.identifier, resource=entity, extras=extras)


class Task:

    def __init__(self, entity, extras, state):
        self.entity = entity
        self.extras = extras
        self.state = state
        self.start_time = ''

    def run(self):
        raise NotImplemented()


# instantiate container
class InitSO(Task):

    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, state='initialise')
        self.nburl = CONFIG.get('cloud_controller', 'nb_api', '')
        if self.nburl[-1] == '/':
            self.nburl = self.nburl[0:-1]
        LOG.info('CloudController Northbound API: ' + self.nburl)
        if len(entity.attributes) > 0:
            LOG.info('Client supplied parameters: ' + entity.attributes.__repr__())
            # XXX check that these parameters are valid according to the kind specification
            self.extras['srv_prms'].add_client_params(entity.attributes)
        else:
            LOG.warn('No client supplied parameters.')

    def run(self):
        #LOG.debug('INIT SO START')
        self.start_time = time.time()
        self.extras['occi.init.starttime'] = self.start_time
        if not self.entity.extras:
            self.entity.extras = {}
        ops_version = self.__detect_ops_version()
        self.entity.extras['ops_version'] = ops_version

        self.entity.attributes['mcn.service.state'] = 'initialise'

        # create an app for the new SO instance
        LOG.debug('Creating SO container...')
        self.__create_app()

        # adding tenant to entity.extras for future checks later when retrieving resource
        self.entity.extras['tenant_name'] = self.extras['tenant_name']
        return self.entity, self.extras

    def __detect_ops_version(self):
        # make a call to the cloud controller and based on the app kind, heuristically select version
        version = 'v2'
        heads = {
            'Content-Type': 'text/occi',
            'Accept': 'text/occi'
            }
        url = self.nburl + '/-/'
        LOG.debug('Requesting CC Query Interface: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        r = http_retriable_request('GET', url, headers=heads, authenticate=True)
        if r.headers['category'].find('occi.app.image') > -1 and r.headers['category'].find('occi.app.env') > -1:
            LOG.info('Found occi.app.image and occi.app.env - this is OpenShift V3')
            version = 'v3'
        else:
            LOG.info('This is OpenShift V2')
        return version

    def __create_app(self):

        # will generate an appname 24 chars long - compatible with v2 and v3
        # e.g. soandycd009b39c28790f3
        app_name = 'so' + self.entity.kind.term[0:4] + \
                   ''.join(random.choice('0123456789abcdef') for _ in range(16))
        heads = {'Content-Type': 'text/occi'}

        url = self.nburl + '/app/'

        if self.entity.extras['ops_version'] == 'v2':
            heads['category'] = 'app; scheme="http://schemas.ogf.org/occi/platform#", ' \
                                'python-2.7; scheme="http://schemas.openshift.com/template/app#", ' \
                                'small; scheme="http://schemas.openshift.com/template/app#"'
            heads['X-OCCI-Attribute'] = str('occi.app.name=' + app_name)
            LOG.debug('Ensuring SM SSH Key...')
            self.__ensure_ssh_key()
        elif self.entity.extras['ops_version'] == 'v3':

            # for OpSv3 bundle location is the repo id of the container image
            bundle_loc = CONFIG.get('service_manager', 'bundle_location', '')
            if bundle_loc == '':
                LOG.error('No bundle_location parameter supplied in sm.cfg')
                raise Exception('No bundle_location parameter supplied in sm.cfg')
            if bundle_loc.startswith('/'):
                LOG.warn('Bundle location does not look like an image reference!')
            LOG.debug('Bundle to execute: ' + bundle_loc)

            design_uri = CONFIG.get('service_manager', 'design_uri', '')
            if design_uri == '':
                raise Exception('No design_uri parameter supplied in sm.cfg')
            LOG.debug('Design URI: ' + design_uri)

            heads['category'] = 'app; scheme="http://schemas.ogf.org/occi/platform#"'
            # TODO provide a means to provide additional docker env params
            attrs = 'occi.app.name="' + app_name + '", ' + \
                    'occi.app.image="' + bundle_loc + '", ' + \
                    'occi.app.env="DESIGN_URI=' + design_uri + '"'
            heads['X-OCCI-Attribute'] = str(attrs)
        else:
            LOG.error('Unknown OpenShift version. ops_version: ' + self.entity.extras['ops_version'])
            raise Exception('Unknown OpenShift version. ops_version: ' + self.entity.extras['ops_version'])

        LOG.debug('Requesting container to execute SO Bundle: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        r = http_retriable_request('POST', url, headers=heads, authenticate=True)

        loc = r.headers.get('Location', '')
        if loc == '':
            LOG.error("No OCCI Location attribute found in request")
            raise AttributeError("No OCCI Location attribute found in request")

        self.entity.attributes['occi.so.url'] = loc

        app_uri_path = urlparse(loc).path
        LOG.debug('SO container created: ' + app_uri_path)

        LOG.debug('Updating OCCI entity.identifier from: ' + self.entity.identifier + ' to: ' +
                  app_uri_path.replace('/app/', self.entity.kind.location))
        self.entity.identifier = app_uri_path.replace('/app/', self.entity.kind.location)

        LOG.debug('Setting occi.core.id to: ' + app_uri_path.replace('/app/', ''))
        self.entity.attributes['occi.core.id'] = app_uri_path.replace('/app/', '')

        # its a bit wrong to put this here, but we do not have the required information before.
        # this keeps things consistent as the timing is done right
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'].split('/'),
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'init',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        # OpSv2 only: get git uri. this is where our bundle is pushed to
        # XXX this is fugly
        # TODO use the same name for the app URI
        if self.entity.extras['ops_version'] == 'v2':
            self.entity.extras['repo_uri'] = self.__git_uri(app_uri_path)
        elif self.entity.extras['ops_version'] == 'v3':
            self.entity.extras['loc'] = self.__git_uri(app_uri_path)

    def __git_uri(self, app_uri_path):
        url = self.nburl + app_uri_path
        headers = {'Accept': 'text/occi'}
        LOG.debug('Requesting container\'s URL ' + url)
        LOG.info('Sending headers: ' + headers.__repr__())
        r = http_retriable_request('GET', url, headers=headers, authenticate=True)

        attrs = r.headers.get('X-OCCI-Attribute', '')
        if attrs == '':
            raise AttributeError("No occi attributes found in request")

        repo_uri = ''
        for attr in attrs.split(', '):
            if attr.find('occi.app.repo') != -1:
                repo_uri = attr.split('=')[1][1:-1]  # scrubs trailing wrapped quotes
                break
            elif attr.find('occi.app.url') != -1:
                repo_uri = attr.split('=')[1][1:-1]  # scrubs trailing wrapped quotes
                break
        if repo_uri == '':
            raise AttributeError("No occi.app.repo or occi.app.url attribute found in request")

        LOG.debug('SO container URL: ' + repo_uri)

        return repo_uri

    def __ensure_ssh_key(self):
        url = self.nburl + '/public_key/'
        heads = {'Accept': 'text/occi'}
        resp = http_retriable_request('GET', url, headers=heads, authenticate=True)
        locs = resp.headers.get('x-occi-location', '')
        # Split on spaces, test if there is at least one key registered
        if len(locs.split()) < 1:
            LOG.debug('No SM SSH registered. Registering default SM SSH key.')
            occi_key_name, occi_key_content = self.__extract_public_key()

            create_key_headers = {'Content-Type': 'text/occi',
                                  'Category': 'public_key; scheme="http://schemas.ogf.org/occi/security/credentials#"',
                                  'X-OCCI-Attribute': 'occi.key.name="' + occi_key_name + '", occi.key.content="' +
                                                      occi_key_content + '"'
                                  }
            http_retriable_request('POST', url, headers=create_key_headers, authenticate=True)
        else:
            LOG.debug('Valid SM SSH is registered with OpenShift.')

    def __extract_public_key(self):

        ssh_key_file = CONFIG.get('service_manager', 'ssh_key_location', '')
        if ssh_key_file == '':
            raise Exception('No ssh_key_location parameter supplied in sm.cfg')
        LOG.debug('Using SSH key file: ' + ssh_key_file)

        with open(ssh_key_file, 'r') as content_file:
            content = content_file.read()
            content = content.split()

            if content[0] == 'ssh-dsa':
                raise Exception("The supplied key is not a RSA ssh key. Location: " + ssh_key_file)

            key_content = content[1]
            key_name = 'servicemanager'

            if len(content) == 3:
                key_name = content[2]

            return key_name, key_content


# instantiate SO
class ActivateSO(Task):
    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, state='activate')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
            if os.system('which git') != 0:
                raise EnvironmentError('Git is not available.')
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']

    def __is_complete(self, url):
        # XXX copy/paste code - merge the two places!
        heads = {
                'Content-type': 'text/occi',
                'Accept': 'application/occi+json',
                'X-Auth-Token': self.extras['token'],
                'X-Tenant-Name': self.extras['tenant_name'],
            }

        LOG.info('Checking app state at: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())

        r = http_retriable_request('GET', url, headers=heads, authenticate=True)
        attrs = json.loads(r.content)

        if len(attrs['attributes']) > 0:
            attr_hash = attrs['attributes']
            app_state = ''
            try:
                app_state = attr_hash['occi.app.state']
            except KeyError:
                pass

            LOG.info('Current service state: ' + str(app_state))
            if app_state == 'active':
                # check if it returns something valid instead of 503
                try:
                    tmpUrl = 'http://' + attr_hash['occi.app.url']
                except KeyError:
                    LOG.info(('App is not ready. app url is not yet set.'))
                    return False
                r = http_retriable_request('GET', tmpUrl, headers=heads, authenticate=True)
                if r.status_code == 200:
                    LOG.info('App is ready')
                    elapsed_time = time.time() - self.extras['occi.init.starttime']
                    del self.extras['occi.init.starttime']

                    infoDict = {
                                'so_id': self.entity.attributes['occi.core.id'],
                                'sm_name': self.entity.kind.term,
                                'so_phase': 'init',
                                'phase_event': 'done',
                                'response_time': elapsed_time,
                                'tenant': self.extras['tenant_name']
                                }
                    tmpJSON = json.dumps(infoDict)
                    LOG.debug(tmpJSON)
                    return True
                else:
                    LOG.info('App is not ready. app url returned: ' + r.status_code)
            else:
                LOG.info('App is not ready. Current state state: ' + app_state)
                return False



    def run(self):

        # this is wrong but required...
        if self.entity.extras['ops_version'] == 'v3':
            url = self.entity.attributes['occi.so.url']
            while not self.__is_complete(url):
                time.sleep(3)


        LOG.debug('ACTIVATE SO START')

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'activate',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)
        if self.entity.extras['ops_version'] == 'v2':
            # get the code of the bundle and push it to the git facilities
            # offered by OpenShift
            LOG.debug('Deploying SO Bundle to: ' + self.repo_uri)
            self.__deploy_app()

        LOG.debug('Activating the SO...')
        self.__init_so()

        self.entity.attributes['mcn.service.state'] = 'activate'

        return self.entity, self.extras

    def __deploy_app(self):
        """
            Deploy the local SO bundle
            assumption here
            - a git repo is returned
            - the bundle is not managed by git
        """
        # create temp dir...and clone the remote repo provided by OpS
        tmp_dir = tempfile.mkdtemp()
        LOG.debug('Cloning git repository: ' + self.repo_uri + ' to: ' + tmp_dir)
        cmd = ' '.join(['git', 'clone', self.repo_uri, tmp_dir])
        os.system(cmd)

        # Get the SO bundle
        bundle_loc = CONFIG.get('service_manager', 'bundle_location', '')
        if bundle_loc == '':
            raise Exception('No bundle_location parameter supplied in sm.cfg')
        LOG.debug('Bundle to add to repo: ' + bundle_loc)
        dir_util.copy_tree(bundle_loc, tmp_dir)

        self.__add_openshift_files(bundle_loc, tmp_dir)

        # add & push to OpenShift
        os.system(' '.join(['cd', tmp_dir, '&&', 'git', 'add', '-A']))
        os.system(' '.join(['cd', tmp_dir, '&&', 'git', 'commit', '-m', '"deployment of SO for tenant ' +
                            self.extras['tenant_name'] + '"', '-a']))
        LOG.debug('Pushing new code to remote repository...')
        os.system(' '.join(['cd', tmp_dir, '&&', 'git', 'push']))

        shutil.rmtree(tmp_dir)

    def __add_openshift_files(self, bundle_loc, tmp_dir):
        # put OpenShift stuff in place
        # build and pre_start_python comes from 'support' directory in bundle
        LOG.debug('Adding OpenShift support files from: ' + bundle_loc + '/support')

        # 1. Write build
        LOG.debug('Writing build to: ' + os.path.join(tmp_dir, '.openshift', 'action_hooks', 'build'))
        shutil.copyfile(bundle_loc+'/support/build', os.path.join(tmp_dir, '.openshift', 'action_hooks', 'build'))

        # 1. Write pre_start_python
        LOG.debug('Writing pre_start_python to: ' +
                  os.path.join(tmp_dir, '.openshift', 'action_hooks', 'pre_start_python'))

        pre_start_template = Template(filename=bundle_loc+'/support/pre_start_python')
        design_uri = CONFIG.get('service_manager', 'design_uri', '')
        content = pre_start_template.render(design_uri=design_uri)
        LOG.debug('Writing pre_start_python content as: ' + content)
        pre_start_file = open(os.path.join(tmp_dir, '.openshift', 'action_hooks', 'pre_start_python'), "w")
        pre_start_file.write(content)
        pre_start_file.close()

        os.system(' '.join(['chmod', '+x', os.path.join(tmp_dir, '.openshift', 'action_hooks', '*')]))

    # example request to the SO
    # curl -v -X PUT http://localhost:8051/orchestrator/default \
    #   -H 'Content-Type: text/occi' \
    #   -H 'Category: orchestrator; scheme="http://schemas.mobile-cloud-networking.eu/occi/service#"' \
    #   -H 'X-Auth-Token: '$KID \
    #   -H 'X-Tenant-Name: '$TENANT
    def __init_so(self):
        url = HTTP + self.host + '/orchestrator/default'
        heads = {
            'Category': 'orchestrator; scheme="http://schemas.mobile-cloud-networking.eu/occi/service#"',
            'Content-Type': 'text/occi',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name'],
        }

        occi_attrs = self.extras['srv_prms'].service_parameters(self.state)
        if len(occi_attrs) > 0:
            LOG.info('Adding service-specific parameters to call... X-OCCI-Attribute: ' + occi_attrs)
            heads['X-OCCI-Attribute'] = occi_attrs

        LOG.debug('Initialising SO with: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        http_retriable_request('PUT', url, headers=heads)
        elapsed_time = time.time() - self.start_time
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'activate',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)
        #LOG.debug('ACTIVATE SO DONE, elapsed: %f' % elapsed_time)


class DeploySO(Task):
    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, state='deploy')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']

    # example request to the SO
    # curl -v -X POST http://localhost:8051/orchestrator/default?action=deploy \
    #   -H 'Content-Type: text/occi' \
    #   -H 'Category: deploy; scheme="http://schemas.mobile-cloud-networking.eu/occi/service#"' \
    #   -H 'X-Auth-Token: '$KID \
    #   -H 'X-Tenant-Name: '$TENANT
    def run(self):
        # Deployment is done without any control by the client...
        # otherwise we won't be able to hand back a working service!
        #LOG.debug('DEPLOY SO START')

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'deploy',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        #LOG.debug('Deploying the SO bundle...')
        url = HTTP + self.host + '/orchestrator/default'
        params = {'action': 'deploy'}
        heads = {
            'Category': 'deploy; scheme="http://schemas.mobile-cloud-networking.eu/occi/service#"',
            'Content-Type': 'text/occi',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name']}
        occi_attrs = self.extras['srv_prms'].service_parameters(self.state)
        if len(occi_attrs) > 0:
            LOG.info('Adding service-specific parameters to call... X-OCCI-Attribute:' + occi_attrs)
            heads['X-OCCI-Attribute'] = occi_attrs
        LOG.debug('Deploying SO with: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        http_retriable_request('POST', url, headers=heads, params=params)

        # also sleep here to keep phases consistent during greenfield
        while not self.deploy_complete(url):
                    time.sleep(7)

        self.entity.attributes['mcn.service.state'] = 'deploy'
        LOG.debug('SO Deployed ')
        elapsed_time = time.time() - self.start_time
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'deploy',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)
        #LOG.debug('DEPLOY SO DONE, elapsed: %f' % elapsed_time)
        return self.entity, self.extras

    def deploy_complete(self, url):
        # XXX fugly - code copied from Resolver
        heads = {
            'Content-type': 'text/occi',
            'Accept': 'application/occi+json',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name'],
        }

        LOG.info('checking service state at: ' + url)
        LOG.info('sending headers: ' + heads.__repr__())

        r = http_retriable_request('GET', url, headers=heads)
        attrs = json.loads(r.content)

        if len(attrs['attributes']) > 0:
            attr_hash = attrs['attributes']
            stack_state = ''
            try:
                stack_state = attr_hash['occi.mcn.stack.state']
            except KeyError:
                pass

            LOG.info('Current service state: ' + str(stack_state))
            if stack_state == 'CREATE_COMPLETE' or stack_state == 'UPDATE_COMPLETE':
                LOG.info('Stack is ready')
                return True
            else:
                LOG.info('Stack is not ready. Current state state: ' + stack_state)
                return False


class ProvisionSO(Task):
    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, state='provision')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']

    def run(self):
        # this can only run until the deployment has complete!
        # this will block until run() returns
        #LOG.debug('PROVISION SO START')

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'provision',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        url = HTTP + self.host + '/orchestrator/default'

        # with stuff like this, we need to have a callback mechanism... this will block otherwise
        while not self.deploy_complete(url):
            time.sleep(13)

        params = {'action': 'provision'}
        heads = {
            'Category': 'provision; scheme="http://schemas.mobile-cloud-networking.eu/occi/service#"',
            'Content-Type': 'text/occi',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name']}
        occi_attrs = self.extras['srv_prms'].service_parameters(self.state)
        if len(occi_attrs) > 0:
            LOG.info('Adding service-specific parameters to call... X-OCCI-Attribute: ' + occi_attrs)
            heads['X-OCCI-Attribute'] = occi_attrs
        LOG.debug('Provisioning SO with: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        http_retriable_request('POST', url, headers=heads, params=params)

        elapsed_time = time.time() - self.start_time
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'provision',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)
        #LOG.debug('PROVISION SO DONE, elapsed: %f' % elapsed_time)
        self.entity.attributes['mcn.service.state'] = 'provision'
        return self.entity, self.extras

    def deploy_complete(self, url):
        # XXX fugly - code copied from Resolver
        heads = {
            'Content-type': 'text/occi',
            'Accept': 'application/occi+json',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name'],
        }

        LOG.info('checking service state at: ' + url)
        LOG.info('sending headers: ' + heads.__repr__())

        r = http_retriable_request('GET', url, headers=heads)
        attrs = json.loads(r.content)

        if len(attrs['attributes']) > 0:
            attr_hash = attrs['attributes']
            stack_state = ''
            try:
                stack_state = attr_hash['occi.mcn.stack.state']
            except KeyError:
                pass

            LOG.info('Current service state: ' + str(stack_state))
            if stack_state == 'CREATE_COMPLETE' or stack_state == 'UPDATE_COMPLETE':
                LOG.info('Stack is ready')
                return True
            elif stack_state == 'CREATE_FAILED':
                raise RuntimeError('Heat stack creation failed.')
            else:
                LOG.info('Stack is not ready. Current state state: ' + stack_state)
                return False


class RetrieveSO(Task):

    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, 'retrieve')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']
        self.registry = self.extras['registry']

    def run(self):
        # example request to the SO
        # curl -v -X GET http://localhost:8051/orchestrator/default \
        #   -H 'X-Auth-Token: '$KID \
        #   -H 'X-Tenant-Name: '$TENANT

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'retrieve',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        if self.entity.attributes['mcn.service.state'] in ['activate', 'deploy', 'provision', 'update']:
            heads = {
                'Content-Type': 'text/occi',
                'Accept': 'text/occi',
                'X-Auth-Token': self.extras['token'],
                'X-Tenant-Name': self.extras['tenant_name']}
            LOG.info('Getting state of service orchestrator with: ' + self.host + '/orchestrator/default')
            LOG.info('Sending headers: ' + heads.__repr__())
            r = http_retriable_request('GET', HTTP + self.host + '/orchestrator/default', headers=heads)

            attrs = r.headers['x-occi-attribute'].split(', ')
            for attr in attrs:
                kv = attr.split('=')
                if kv[0] != 'occi.core.id':
                    if kv[1].startswith('"') and kv[1].endswith('"'):
                        kv[1] = kv[1][1:-1]  # scrub off quotes
                    self.entity.attributes[kv[0]] = kv[1]
                    LOG.debug('OCCI Attribute: ' + kv[0] + ' --> ' + kv[1])

            # Assemble the SIG
            svcinsts = ''
            try:
                svcinsts = self.entity.attributes['mcn.so.svcinsts']
                del self.entity.attributes['mcn.so.svcinsts']  # remove this, not be be used anywhere else
            except KeyError:
                LOG.warn('There was no service instance endpoints - ignore if not a composition.')
                pass

            if self.registry is None:
                LOG.error('No registry!')

            if len(svcinsts) > 0:
                svcinsts = svcinsts.split()  # all instance EPs
                for svc_loc in svcinsts:
                    # TODO get the service instance resource representation
                    # source resource is self.entity
                    compos = svc_loc.split('/')
                    key = '/' + compos[3] + '/' + compos[4]
                    target = Resource(key, Resource.kind, [])  # target resource
                    target.attributes['mcn.sm.endpoint'] = svc_loc
                    self.registry.add_resource(key, target, None)

                    key = '/link/'+str(uuid.uuid4())
                    link = Link(key, Link.kind, [], self.entity, target)
                    self.registry.add_resource(key, link, None)
                    self.entity.links.append(link)
        else:
            LOG.debug('Cannot GET entity as it is not in the activated, deployed or provisioned, updated state')

        elapsed_time = time.time() - self.start_time
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'retrieve',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        return self.entity, self.extras


# can only be executed when provisioning is complete
class UpdateSO(Task):
    def __init__(self, entity, extras, updated_entity):
        Task.__init__(self, entity, extras, state='update')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']
        self.new = updated_entity

    def run(self):
        # take parameters from EEU and send them down to the SO instance
        # Trigger update on SO + service instance:
        #
        # $ curl -v -X POST http://localhost:8051/orchestrator/default \
        #       -H 'Content-Type: text/occi' \
        #       -H 'X-Auth-Token: '$KID \
        #       -H 'X-Tenant-Name: '$TENANT \
        #       -H 'X-OCCI-Attribute: occi.epc.attr_1="foo"'

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'update',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        url = HTTP + self.host + '/orchestrator/default'
        heads = {
            'Content-Type': 'text/occi',
            'X-Auth-Token': self.extras['token'],
            'X-Tenant-Name': self.extras['tenant_name']}

        occi_attrs = self.extras['srv_prms'].service_parameters(self.state)

        if len(occi_attrs) > 0:
            LOG.info('Adding service-specific parameters to call... X-OCCI-Attribute:' + occi_attrs)
            heads['X-OCCI-Attribute'] = occi_attrs

        if len(self.new.attributes) > 0:
            LOG.info('Adding updated parameters... X-OCCI-Attribute: ' + self.new.attributes.__repr__())
            for k, v in self.new.attributes.items():
                occi_attrs = occi_attrs + ', ' + k + '=' + v
                self.entity.attributes[k] = v
            heads['X-OCCI-Attribute'] = occi_attrs

        LOG.debug('Updating (Provisioning) SO with: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())

        http_retriable_request('POST', url, headers=heads)

        self.entity.attributes['mcn.service.state'] = 'update'

        #start thread here
        thread = Thread(target = deploy_complete, args = (url, self.start_time, self.extras, self.entity ))
        thread.start()

        return self.entity, self.extras


def deploy_complete(url, start_time, extras, entity):

    done = False

    while done == False:
        # XXX fugly - code copied from Resolver
        heads = {
            'Content-type': 'text/occi',
            'Accept': 'application/occi+json',
            'X-Auth-Token': extras['token'],
            'X-Tenant-Name': extras['tenant_name'],
        }

        LOG.info('checking service state at: ' + url)
        LOG.info('sending headers: ' + heads.__repr__())

        r = http_retriable_request('GET', url, headers=heads)
        attrs = json.loads(r.content)

        if len(attrs['attributes']) > 0:
            attr_hash = attrs['attributes']
            stack_state = ''
            try:
                stack_state = attr_hash['occi.mcn.stack.state']
            except KeyError:
                pass

            LOG.info('Current service state: ' + str(stack_state))
            if stack_state == 'CREATE_COMPLETE' or stack_state == 'UPDATE_COMPLETE':
                LOG.info('Stack is ready')
                elapsed_time = time.time() - start_time
                infoDict = {
                    'so_id': entity.attributes['occi.core.id'],
                    'sm_name': entity.kind.term,
                    'so_phase': 'update',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': extras['tenant_name']
                    }
                tmpJSON = json.dumps(infoDict)
                LOG.debug(tmpJSON)
                done = True
            else:
                LOG.info('Stack is not ready. Current state state: ' + stack_state)
                done = False
                time.sleep(3)


class DestroySO(Task):
    def __init__(self, entity, extras):
        Task.__init__(self, entity, extras, state='destroy')
        if self.entity.extras['ops_version'] == 'v2':
            self.repo_uri = self.entity.extras['repo_uri']
            self.host = urlparse(self.repo_uri).netloc.split('@')[1]
        elif self.entity.extras['ops_version'] == 'v3':
            self.host = self.entity.extras['loc']
        self.nburl = CONFIG.get('cloud_controller', 'nb_api', '')

    def run(self):
        # 1. dispose the active SO, essentially kills the STG/ITG
        # 2. dispose the resources used to run the SO
        # example request to the SO
        # curl -v -X DELETE http://localhost:8051/orchestrator/default \
        #   -H 'X-Auth-Token: '$KID \
        #   -H 'X-Tenant-Name: '$TENANT

        self.start_time = time.time()
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'destroy',
                    'phase_event': 'start',
                    'response_time': 0,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        url = HTTP + self.host + '/orchestrator/default'
        heads = {'X-Auth-Token': self.extras['token'],
                 'X-Tenant-Name': self.extras['tenant_name']}
        occi_attrs = self.extras['srv_prms'].service_parameters(self.state)
        if len(occi_attrs) > 0:
            LOG.info('Adding service-specific parameters to call... X-OCCI-Attribute:' + occi_attrs)
            heads['X-OCCI-Attribute'] = occi_attrs
        LOG.info('Disposing service orchestrator with: ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())

        http_retriable_request('DELETE', url, headers=heads)

        url = self.nburl + self.entity.identifier.replace('/' + self.entity.kind.term + '/', '/app/')
        heads = {'Content-Type': 'text/occi',
                 'X-Auth-Token': self.extras['token'],
                 'X-Tenant-Name': self.extras['tenant_name']}
        LOG.info('Disposing service orchestrator container via CC... ' + url)
        LOG.info('Sending headers: ' + heads.__repr__())
        http_retriable_request('DELETE', url, headers=heads, authenticate=True)

        elapsed_time = time.time() - self.start_time
        infoDict = {
                    'so_id': self.entity.attributes['occi.core.id'],
                    'sm_name': self.entity.kind.term,
                    'so_phase': 'destroy',
                    'phase_event': 'done',
                    'response_time': elapsed_time,
                    'tenant': self.extras['tenant_name']
                    }
        tmpJSON = json.dumps(infoDict)
        LOG.debug(tmpJSON)

        return self.entity, self.extras