def get(cls, id):
     db = DatabaseManager()
     try:
         security_group = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     return security_group
Пример #2
0
def checkTopolgoyUniqueness(topology):
    db = DatabaseManager()
    # check names
    logger.debug("Check uniqueness of name of the toplogy \"%s\"." % topology.name)
    for top in db.get_all(Topology):
        if topology.ext_name == top.ext_name and topology.id != top.id:
            raise NotUniqueException("Topology name \"%s\" is already used." % topology.name)
 def get(cls, id):
     db = DatabaseManager()
     try:
         service = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     return service
Пример #4
0
 def get(cls, id):
     db = DatabaseManager()
     try:
         security_group = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     return security_group
Пример #5
0
 def get(cls, id):
     db = DatabaseManager()
     try:
         service = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     return service
Пример #6
0
 def delete(cls, id):
     db = DatabaseManager()
     try:
         security_group_to_remove = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     db.remove(security_group_to_remove)
     return security_group_to_remove
Пример #7
0
 def delete(cls, id):
     db = DatabaseManager()
     try:
         service_to_remove = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     db.remove(service_to_remove)
     return service_to_remove
 def delete(cls, id):
     db = DatabaseManager()
     try:
         service_to_remove = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     db.remove(service_to_remove)
     return service_to_remove
Пример #9
0
def checkSecurityGroupUniqueness(security_group):
    db = DatabaseManager()
    existing_security_groups = db.get_all(SecurityGroup)
    logger.debug("Check uniqueness of name of the security group \"%s\"." % security_group.name)
    for existing_security_group in existing_security_groups:
        if security_group.name == existing_security_group.name and security_group != existing_security_group:
            raise NotUniqueException("SecurityGroup:\"%s\" is already existing." % security_group.name)
    logger.debug("Check rules of security group \"%s\"." % security_group.name)
 def delete(cls, id):
     db = DatabaseManager()
     try:
         security_group_to_remove = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     db.remove(security_group_to_remove)
     return security_group_to_remove
Пример #11
0
def checkKey(key):
    db = DatabaseManager()
    existing_keys = db.get_all(Key)
    if key.name in [existing_key.name for existing_key in existing_keys]:
        logger.debug("key \"%s\" is available." % key)
    else:
        raise NotFoundException(
            "key:\"%s\" is not available. Available keys: %s" % (
                key, [existing_key.name for existing_key in existing_keys]))
Пример #12
0
def checkKey(key):
    db = DatabaseManager()
    existing_keys = db.get_all(Key)
    if key.name in [existing_key.name for existing_key in existing_keys]:
        LOG.debug("key \"%s\" is available." % key)
    else:
        raise NotFoundException(
            "key:\"%s\" is not available. Available keys: %s" %
            (key, [existing_key.name for existing_key in existing_keys]))
Пример #13
0
class Register:

    def __init__(self):
        self.db = DatabaseManager()

    def register_unit(self, unit, ws):
        unit.ws = ws
        unit.state = 'STARTED'
        self.db.update(unit)
Пример #14
0
def checkImage(image):
    db = DatabaseManager()
    existing_images = db.get_all(Image)
    if image.name in [existing_image.name for existing_image in existing_images]:
        logger.debug("image \"%s\" is available." % image)
    else:
        raise NotFoundException(
            "image:\"%s\" is not available. Available images: %s" % (
                image, [existing_image.name for existing_image in existing_images]))
Пример #15
0
def checkTopolgoyUniqueness(topology):
    db = DatabaseManager()
    # check names
    LOG.debug("Check uniqueness of name of the toplogy \"%s\"." %
              topology.name)
    for top in db.get_all(Topology):
        if topology.ext_name == top.ext_name and topology.id != top.id:
            raise NotUniqueException("Topology name \"%s\" is already used." %
                                     topology.name)
Пример #16
0
def checkFlavor(flavor):
    db = DatabaseManager()
    existing_flavors = db.get_all(Flavor)
    if flavor.name in [existing_flavor.name for existing_flavor in existing_flavors]:
        logger.debug("flavor \"%s\" is available." % flavor)
    else:
        raise NotFoundException(
            "flavor:\"%s\" is not available. Available flavors: %s" % (
                flavor, [existing_flavor.name for existing_flavor in existing_flavors]))
Пример #17
0
def checkSecurityGroupUniqueness(security_group):
    db = DatabaseManager()
    existing_security_groups = db.get_all(SecurityGroup)
    LOG.debug("Check uniqueness of name of the security group \"%s\"." %
              security_group.name)
    for existing_security_group in existing_security_groups:
        if security_group.name == existing_security_group.name and security_group != existing_security_group:
            raise NotUniqueException(
                "SecurityGroup:\"%s\" is already existing." %
                security_group.name)
    LOG.debug("Check rules of security group \"%s\"." % security_group.name)
Пример #18
0
 def create(cls, service_args):
     try:
         conf = sys_util().get_sys_conf()
         service_manager = FactoryAgent().get_agent(conf['service_manager'])
         service = service_manager.create(service_args)
         checker = FactoryAgent().get_agent(conf['checker'])
         checker.check(service=service)
         db = DatabaseManager()
         db.persist(service)
     except Exception, msg:
         raise
Пример #19
0
def checkNetwork(network):
    try:
        db = DatabaseManager()
        existing_networks = db.get_all(Network)
        found_private_net = False
        found_subnet = False
        found_public_net = False
        for existing_network in existing_networks:
            if network.private_net == existing_network.ext_id and not found_private_net:
                if existing_network.public == False:
                    logger.debug("private_network \"%s\" is available." % network.private_net)
                    found_private_net = True
                else:
                    raise InvalidInputException(
                        "private_network:\"%s\" is available but it is marked as public and not as private as defined." % network.private_net)
                for subnet in existing_network.subnets:
                    if network.private_subnet == subnet.ext_id and not found_subnet:
                        found_subnet = True
                if found_subnet:
                    logger.debug("private_subnet \"%s\" is available." % network.private_subnet)
                else:
                    raise InvalidInputException("private_subnet:\"%s\" is not available." % network.private_subnet)
            if network.public_net == existing_network.ext_id and not found_public_net:
                if existing_network.public == True:
                    logger.debug("public_network \"%s\" is available." % network.public_net)
                    found_public_net = True
                else:
                    raise InvalidInputException(
                        "network:\"%s\" is available but it is marked as private and not as public as defined." % network.public_net)
        if not network.private_net and not network.private_subnet and not network.public_net:
            logger.debug("Networks were not defined.")
        elif network.private_net and network.private_subnet and network.public_net:
            if found_private_net and found_subnet and found_public_net:
                logger.debug("All defined networks are available for network: %s" % network)
            if not found_private_net:
                raise NotFoundException("Not found private network: %s" % network)
            if not found_subnet:
                raise NotFoundException("Not found private subnet network: %s" % network)
            if not found_public_net:
                raise NotFoundException("Not found public network: %s" % network)
        elif network.private_net and network.private_subnet and not network.public_net:
            if found_private_net and found_subnet and not found_public_net:
                logger.debug("All defined networks are available for network: %s" % network)
            if not found_private_net:
                raise NotFoundException("Not found private network: %s" % network)
            if not found_subnet:
                raise NotFoundException("Not found private subnet network: %s" % network)
        elif not network.private_net and network.public_net:
            raise InvalidInputException("Private net is not defined but the public.")
        else:
            raise InvalidInputException("Error while checking networks.")
    except Exception, exc:
        exc.message = 'Network:\"%s\"->%s' % (network.name, exc.message)
        raise exc
Пример #20
0
def checkServiceUniqueness(service):
    db = DatabaseManager()
    # check uniqueness of service
    logger.debug("Check uniqueness of name of the service %s." % service.service_type)
    if service.service_type:
        for existing_service in db.get_all(Service):
            if service.service_type == existing_service.service_type and service != existing_service:
                raise NotUniqueException(
                    "Service:\"%s\" is already existing." % service.service_type)
    else:
        raise NotDefinedException("service_type is not defined.")
 def create(cls, service_args):
     try:
         conf = sys_util().get_sys_conf()
         service_manager = FactoryAgent().get_agent(conf["service_manager"])
         service = service_manager.create(service_args)
         checker = FactoryAgent().get_agent(conf["checker"])
         checker.check(service=service)
         db = DatabaseManager()
         db.persist(service)
     except Exception, msg:
         raise
Пример #22
0
def checkImage(image):
    db = DatabaseManager()
    existing_images = db.get_all(Image)
    if image.name in [
            existing_image.name for existing_image in existing_images
    ]:
        LOG.debug("image \"%s\" is available." % image)
    else:
        raise NotFoundException(
            "image:\"%s\" is not available. Available images: %s" %
            (image,
             [existing_image.name for existing_image in existing_images]))
Пример #23
0
def checkFlavor(flavor):
    db = DatabaseManager()
    existing_flavors = db.get_all(Flavor)
    if flavor.name in [
            existing_flavor.name for existing_flavor in existing_flavors
    ]:
        LOG.debug("flavor \"%s\" is available." % flavor)
    else:
        raise NotFoundException(
            "flavor:\"%s\" is not available. Available flavors: %s" %
            (flavor,
             [existing_flavor.name for existing_flavor in existing_flavors]))
Пример #24
0
def checkServiceType(service_type):
    db = DatabaseManager()
    services = db.get_all(Service)
    found = False
    for service in services:
        if service.service_type == service_type:
            found = True
            LOG.debug("service_type \"%s\" is available." % service_type)
    if not found:
        raise NotFoundException(
            "service_type:\"%s\" is not available. Available service_types:%s"
            % (service_type, [service.service_type for service in services]))
Пример #25
0
def checkServiceUniqueness(service):
    db = DatabaseManager()
    # check uniqueness of service
    LOG.debug("Check uniqueness of name of the service %s." %
              service.service_type)
    if service.service_type:
        for existing_service in db.get_all(Service):
            if service.service_type == existing_service.service_type and service != existing_service:
                raise NotUniqueException(
                    "Service:\"%s\" is already existing." %
                    service.service_type)
    else:
        raise NotDefinedException("service_type is not defined.")
Пример #26
0
def checkServiceType(service_type):
    db = DatabaseManager()
    services = db.get_all(Service)
    found = False
    for service in services:
        if service.service_type == service_type:
            found = True
            logger.debug("service_type \"%s\" is available." % service_type)
    if not found:
        raise NotFoundException(
            "service_type:\"%s\" is not available. Available service_types:%s" % (
                service_type, [service.service_type for
                               service in services]))
 def create(cls, secgroup_args):
     _sec_rules = secgroup_args.get("rules")
     _new_sec_rules = []
     for _sec_rule_args in _sec_rules:
         _new_sec_rule = Rule(**_sec_rule_args)
         _new_sec_rules.append(_new_sec_rule)
     new_secgroup = SecurityGroup(name=secgroup_args.get('name'), rules=_new_sec_rules)
     conf = sys_util().get_sys_conf()
     checker = FactoryAgent().get_agent(conf['checker'])
     checker.check(security_group=new_secgroup)
     db = DatabaseManager()
     db.persist(new_secgroup)
     return new_secgroup
Пример #28
0
 def __init__(self, topology):
     super(CheckerThread, self).__init__()
     self.heatclient = HeatClient()
     self.topology = topology
     self.db = DatabaseManager()
     self.is_stopped = False
     self.is_dns_configured = False
     self.novac = NovaClient()
     #self.dns_configurator = ImsDnsClient()
     self.neutronc = NeutronClient(
         utilSys.get_endpoint(
             'network',
             region_name=SysUtil().get_sys_conf()['os_region_name']),
         utilSys.get_token())
Пример #29
0
 def create(cls, secgroup_args):
     _sec_rules = secgroup_args.get("rules")
     _new_sec_rules = []
     for _sec_rule_args in _sec_rules:
         _new_sec_rule = Rule(**_sec_rule_args)
         _new_sec_rules.append(_new_sec_rule)
     new_secgroup = SecurityGroup(name=secgroup_args.get('name'),
                                  rules=_new_sec_rules)
     conf = sys_util().get_sys_conf()
     checker = FactoryAgent().get_agent(conf['checker'])
     checker.check(security_group=new_secgroup)
     db = DatabaseManager()
     db.persist(new_secgroup)
     return new_secgroup
 def update(cls, service_args, id):
     db = DatabaseManager()
     try:
         updated_service = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     updated_service.config = service_args.get("config") or updated_service.config
     updated_service.flavor = service_args.get("flavor") or updated_service.flavor
     updated_service.image = service_args.get("image") or updated_service.image
     updated_service.service_type = service_args.get("service_type") or updated_service.service_type
     updated_service.size = service_args.get("size") or updated_service.size
     conf = sys_util().get_sys_conf()
     checker = FactoryAgent().get_agent(conf["checker"])
     checker.check(service=updated_service)
     db.update(updated_service)
     return updated_service
Пример #31
0
 def update(cls, service_args, id):
     db = DatabaseManager()
     try:
         updated_service = db.get_by_id(Service, id)
     except NotFoundException as e:
         raise e
     updated_service.config = service_args.get('config') or updated_service.config
     updated_service.flavor = service_args.get('flavor') or updated_service.flavor
     updated_service.image = service_args.get('image') or updated_service.image
     updated_service.service_type = service_args.get('service_type') or updated_service.service_type
     updated_service.size = service_args.get('size') or updated_service.size
     conf = sys_util().get_sys_conf()
     checker = FactoryAgent().get_agent(conf['checker'])
     checker.check(service=updated_service)
     db.update(updated_service)
     return updated_service
 def update(cls, secgroup_args, id):
     db = DatabaseManager()
     try:
         updated_secgroup = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     _sec_rules = secgroup_args.get('rules')
     _new_sec_rules = []
     for _sec_rule_args in _sec_rules:
         _new_sec_rule = Rule(**_sec_rule_args)
         _new_sec_rules.append(_new_sec_rule)
     updated_secgroup.rules = _new_sec_rules or updated_secgroup.rules
     updated_secgroup.name = secgroup_args.get('name') or updated_secgroup.name
     check(security_group=updated_secgroup)
     db.update(updated_secgroup)
     return updated_secgroup
Пример #33
0
 def update(cls, secgroup_args, id):
     db = DatabaseManager()
     try:
         updated_secgroup = db.get_by_id(SecurityGroup, id)
     except NotFoundException as e:
         raise e
     _sec_rules = secgroup_args.get('rules')
     _new_sec_rules = []
     for _sec_rule_args in _sec_rules:
         _new_sec_rule = Rule(**_sec_rule_args)
         _new_sec_rules.append(_new_sec_rule)
     updated_secgroup.rules = _new_sec_rules or updated_secgroup.rules
     updated_secgroup.name = secgroup_args.get(
         'name') or updated_secgroup.name
     check(security_group=updated_secgroup)
     db.update(updated_secgroup)
     return updated_secgroup
Пример #34
0
 def __init__(self, topology):
     super(CheckerThread, self).__init__()
     self.heatclient = HeatClient()
     self.topology = topology
     self.db = DatabaseManager()
     self.is_stopped = False
     self.is_dns_configured = False
     self.novac = NovaClient()
     #self.dns_configurator = ImsDnsClient()
     self.neutronc = NeutronClient(utilSys.get_endpoint('network', region_name=SysUtil().get_sys_conf()['os_region_name']), utilSys.get_token())
Пример #35
0
    def test_create_service(self):
        db = DatabaseManager()
        if len(db.get_all(Service)) > 0:
            self.fail('The Database must be empty: ' + str())

        connection = httplib.HTTPConnection('%s:8090' % HOST)
        headers = {'Content-type': 'application/json'}

        sent_service = {"service_type": "controller", 'image': "nubomedia-broker", 'flavor': "m1.medium",
                        'size': {'def': 1, 'max': 1, 'min': 1}, 'config': {'hostname': 'ControlServer'}}

        sent_service_json = json.dumps(sent_service)

        connection.request('POST', '/services', sent_service_json, headers)
        resp = connection.getresponse()

        self.assertEqual(resp.status, 200)

        service = db.get_by_service_type(Service, sent_service["service_type"])[0]

        self.assertIsNotNone(service)

        self.assertEqual(service.service_type, sent_service["service_type"])
        self.assertEqual(service.image, sent_service["image"])
        self.assertEqual(service.flavor, sent_service["flavor"])
        self.assertEqual(service.size, sent_service['size'])
        self.assertEqual(service.config, sent_service['config'])

        sent_service['flavor'] = "m1.small"

        sent_service_json = json.dumps(sent_service)

        connection.request('PUT', '/services/' + str(service.id), sent_service_json, headers)
        resp = connection.getresponse()

        self.assertEqual(resp.status, 200)

        service = db.get_by_service_type(Service, sent_service["service_type"])[0]
        self.assertIsNotNone(service)
        self.assertEqual(service.service_type, sent_service["service_type"])
        self.assertEqual(service.image, sent_service["image"])
        self.assertEqual(service.flavor, sent_service["flavor"])
        self.assertEqual(service.size, sent_service['size'])
        self.assertEqual(service.config, sent_service['config'])

        connection.request('DELETE', '/services/' + str(service.id), sent_service_json, headers)

        time.sleep(2)

        self.assertEqual(len(db.get_by_service_type(Service, sent_service["service_type"])), 0)
 def delete_rule(cls, id):
     db = DatabaseManager()
     try:
         db.remove(db.get_by_id(Rule,id))
     except NotFoundException as e:
         raise e
Пример #37
0
class ServiceManager(ABCServiceManager):

    def __init__(self):
        self.db = DatabaseManager()

    def create(self, config):
        ###Service arguments
        service_type = config.get('service_type')
        logger.debug("Parse Service \"%s\"" % service_type)
        ###Parse each item described in config file
        new_service_args={}
        for service_item in config.keys():
            if service_item == "service_type":
                new_service_args['service_type'] = config.get(service_item)
            elif service_item == "image":
                image_name = config.get(service_item)
                image_list = self.db.get_by_name(Image, image_name)
                if len(image_list) == 1:
                    image = image_list[0]
                    new_service_args['image'] = image
                else:
                    raise NotFoundException('image:\"%s\" is not available.' % image_name)
            elif service_item == "flavor":
                flavor_name = config.get(service_item)
                flavor_list = self.db.get_by_name(Flavor, flavor_name)
                if len(flavor_list) == 1:
                    flavor = flavor_list[0]
                    new_service_args['flavor'] = flavor
                else:
                    raise NotFoundException('flavor:\"%s\" is not available.' % flavor_name)
            elif service_item == "key":
                key_name = config.get(service_item)
                key_list = self.db.get_by_name(Key, key_name)
                if len(key_list) == 1:
                    key = key_list[0]
                    new_service_args['key'] = key
                else:
                    raise NotFoundException('key:\"%s\" is not available.' % key_name)
            elif service_item == "size":
                new_service_args['size'] = config.get(service_item)
            elif service_item == "networks":
                networks = []
                _networks = config.get(service_item)
                logger.debug("Fetch SecurityGroups for networks %s." % _networks)
                for _net_inst in _networks:
                    secgroups = []
                    for _secgroup_name in _net_inst.get('security_groups'):
                        lst = self.db.get_by_name(SecurityGroup, _secgroup_name)
                        if len(lst) > 0:
                            _secgroup = lst[0]
                        else:
                            raise NotFoundException('SecurityGroup:\"%s\" is not available.'  % _secgroup_name)
                        secgroups.append(_secgroup)
                    _net_inst['security_groups'] = secgroups
                    networks.append(Network_Instance(**_net_inst))
                new_service_args['networks'] = networks
            elif service_item == "configuration":
                new_service_args['configuration'] = config.get(service_item)
            elif service_item == "requirements":
               requirements = []
               for _req in config.get(service_item):
                   _req_args = _req
                   requirement = Requirement(**_req_args)
                   requirements.append(requirement)
               new_service_args['requirements'] = requirements
            elif service_item == "user_data":
                user_data = []
                for _user_data_item in  config.get(service_item):
                    command = Command(_user_data_item)
                    user_data.append(command)
                new_service_args['user_data'] = user_data
            elif service_item == "version":
                new_service_args['version'] = config.get(service_item)
            elif service_item == "adapter":
                new_service_args['adapter'] = config.get(service_item)
            else:
                raise InvalidInputException("paramter \"%s\" is not provided by Services." % config.get(service_item))


        ###Create Service
        print(new_service_args)
        new_service = Service(**new_service_args)
        logger.debug(new_service)
        return new_service

    def update(self):
        pass

    def dynamic_create(self, dict):
        logger.debug(dict)
        t = Topology(**dict)
        logger.debug(t.__dict__)
        return t

    def get_attrs(self,_class):
        return [k for k in dir(_class) if not k.startswith('__') ]

    def get_service_instance(self, _class, _name):
        return self.db.get_by_name(_class, _name)
    def get_template(topology):
        #name = topology.name
        _template = {}
        _template['heat_template_version'] = '2013-05-23'
        _resources = {}
        _outputs = {}
        LOG.debug("create Template for Topology: %s" % topology.name)

        db = DatabaseManager()

        for service_instance in topology.service_instances:
            _software_config = None
            #Create SoftwareConfig for user_data
            if service_instance.user_data:
                _inputs = []
                if service_instance.requirements:
                    for requirement in service_instance.requirements:
                        _inputs.append(requirement.name)
                _software_config = SoftwareConfig(name='%s-SoftwareConfig' % service_instance.name, config=service_instance.user_data, group=service_instance.name, inputs=_inputs)

            for unit in service_instance.units:
                #Create input values for user_data for SoftwareDeployment
                if service_instance.requirements or service_instance.user_data:
                    _input_values = {}
                    for requirement in service_instance.requirements:
                        try:
                            source_service_instances = db.get_by_name(ServiceInstance,requirement.source)
                        except:
                            LOG.debug('ERROR: Entry %s was not found in Table ServiceInstance' % requirement.source)
                            raise
                        source_units = []
                        if source_service_instances:
                            source_service_instance = source_service_instances[0]
                            source_units = source_service_instance.units
                            LOG.debug(source_units)
                            if source_units:
                                if requirement.parameter == 'private_ip' or requirement.parameter == 'public_ip':
                                    #Get requested network specified in the requirement
                                    _networks = [network for network in source_service_instance.networks if network.name == requirement.obj_name ]
                                    _network = None
                                    if _networks:
                                        _network_id = _networks[0].private_net
                                    else:
                                        LOG.debug('ERROR: obj_name %s was not found in networks of ServiceInstance %s' % (requirement.obj_name,source_service_instance))
                                        raise
                                    #Get network name of the specified network id
                                    _network_names = [network.name for network in db.get_all(Network) if network.ext_id == _network_id]
                                    _network_name = None
                                    if _network_names:
                                        _network_name = _network_names[0]
                                    else:
                                        LOG.debug('ERROR: Cannot find network with id %s in Table Network' % _network_id)
                                    if requirement.parameter == "private_ip":
                                        ip_number = 0
                                    elif requirement.parameter == "public_ip":
                                        ip_number = 1
                                    #Create the variable
                                    _param_params = {}
                                    _first_unit = source_units[0]
                                    _param_template = '$%s' % _first_unit.hostname
                                    _param_params['$%s' % _first_unit.hostname] = {'get_attr': [_first_unit.hostname, 'networks', _network_name, ip_number]}
                                    for source_unit in source_units[1:]:
                                        _param_template += ';$%s' % source_unit.hostname
                                        _param_params['$%s' % source_unit.hostname] = {'get_attr': [source_unit.hostname, 'networks', _network_name, ip_number]}
                                param = {}
                                param[requirement.name] = {}
                                param[requirement.name]['str_replace'] = {}
                                param[requirement.name]['str_replace']['template'] = _param_template
                                param[requirement.name]['str_replace']['params'] = _param_params
                                _input_values.update(param)
                            else:
                                LOG.debug('ERROR: Units for ServiceInstance %s were not found.' % requirement.source)
                                raise Exception
                        else:
                            LOG.debug('ERROR: ServiceInstance %s was not found' % requirement.source)
                            raise Exception
                    _software_deployment = SoftwareDeployment(name='%s-SoftwareDeployment' % unit.hostname, config=_software_config, server=unit, input_values=_input_values)
                    _resources.update(_software_deployment.dump_to_dict())

                #Create Ports and floating IPs for this unit
                _ports = []
                _floating_ips = []
                if service_instance.networks:
                    i=1
                    for network in service_instance.networks:
                        ###Creating Port for this service instance
                        _new_port = None
                        #prepare port args for this service instance
                        _port_args = {}
                        _port_args['name'] = '%s-port-%s' % (unit.hostname, i)
                        _port_args['private_net_id'] = network.private_net
                        _port_args['private_subnet_id'] = network.private_subnet
                        _port_args['fixed_ip'] = network.fixed_ip
                        if network.security_groups:
                            _port_args['security_groups'] = network.security_groups
                        _new_port = Port(**_port_args)
                        _ports.append(_new_port)
                        if network.public_net:
                            _new_floating_ip_args = {}
                            _new_floating_ip_args['name'] = '%s-floating_ip-%s' % (unit.hostname, i)
                            _new_floating_ip_args['floating_network_id'] = network.public_net
                            _new_floating_ip_args['port'] = _new_port.name
                            _new_floating_ip = FloatingIP(**_new_floating_ip_args)
                            _floating_ips.append(_new_floating_ip)
                        ###Adding Security Groups
                        for _security_group in network.security_groups:
                            _new_name=_security_group.name
                            _new_rules=[]
                            _rules=_security_group.rules
                            for _rule in _rules:
                                _name = _rule.name
                                _remote_ip_prefix = _rule.remote_ip_prefix
                                _protocol = _rule.protocol
                                _port_range_max = int(_rule.port_range_max) if _rule.port_range_max else None
                                _port_range_min = int(_rule.port_range_min) if _rule.port_range_min else None
                                _new_rule = Rule(_name, _remote_ip_prefix, _protocol, _port_range_max, _port_range_min)
                                _new_rules.append(_new_rule)
                            _new_security_group = SecurityGroup(name=_new_name, rules=_new_rules)
                            _resources.update(_new_security_group.dump_to_dict())
                        i += 1

                ###Create Server for this service instance
                _new_server = None
                #prepare server args
                _server_args = {}
                _server_args['name'] = "%s" % unit.hostname
                _server_args['hostname'] = "%s" % unit.hostname
                _server_args['flavor'] = service_instance.flavor.name
                _server_args['image'] = service_instance.image.name
                _server_args['key_name'] = service_instance.key.name
                _server_args['network_ports'] = _ports
                _server_args['user_data'] = service_instance.user_data
                _server_args['requirements'] = service_instance.requirements
                _new_server = Server(**_server_args)

                _resources.update(_new_server.dump_to_dict())

                if _software_config:
                    _resources.update(_software_config.dump_to_dict())
                if _ports:
                    for _port in _ports:
                        _resources.update(_port.dump_to_dict())
                if _floating_ips:
                    for _floating_ip in _floating_ips:
                        _resources.update(_floating_ip.dump_to_dict())
        print _resources
        _template['resources'] = _resources

        ###Output section###
        db = DatabaseManager()
        for service_instance in topology.service_instances:
            for network_instance in service_instance.networks:
                if network_instance.public_net:
                    _public_network_names = [_network.name for _network in db.get_all(Network) if
                                             _network.ext_id == network_instance.public_net]
                    _public_network_name = None
                    if _public_network_names:
                        _public_network_name = _public_network_names[0]
                    else:
                        LOG.debug('ERROR: Cannot find network with id %s in Table Network' % network_instance.public_net)
                if network_instance.private_net:
                    _private_network_names = [_network.name for _network in db.get_all(Network) if
                                              _network.ext_id == network_instance.private_net]
                    _private_network_name = None
                    if _private_network_names:
                        _private_network_name = _private_network_names[0]
                    else:
                        LOG.debug('ERROR: Cannot find network with id %s in Table Network' % network_instance.net)
                for unit in service_instance.units:
                    if network_instance.public_net and _public_network_name:
                        output = {}
                        output['value'] = {'get_attr': [unit.hostname, 'networks', _private_network_name, 1]}
                        output['description'] = 'Public IP of %s.' % unit.hostname
                        _outputs['mcn.endpoint.%s' % unit.hostname] = output
                    elif network_instance.private_net and _private_network_name:
                        output = {}
                        output['value'] = {'get_attr': [unit.hostname, 'networks', _private_network_name, 0]}
                        output['description'] = 'Private IP of %s.' % unit.hostname
                        _outputs['mcn.endpoint.%s' % unit.hostname] = output
        _template['outputs'] = _outputs

        yaml.add_representer(unicode, lambda dumper, value: dumper.represent_scalar(u'tag:yaml.org,2002:str', value))
        yaml.add_representer(SysUtil.literal_unicode, SysUtil.literal_unicode_representer)
        LOG.debug((_template))
        #LOG.debug(yaml.dumps(template))
        #f = open('/net/u/mpa/tempalte_file.yaml', 'w')
        #f.write(yaml.dump(template, indent=2))
        return yaml.dump(_template)
Пример #39
0
class CheckerThread(threading.Thread):
    def __init__(self, topology):
        super(CheckerThread, self).__init__()
        self.heatclient = HeatClient()
        self.topology = topology
        self.db = DatabaseManager()
        self.is_stopped = False
        self.is_dns_configured = False
        self.novac = NovaClient()
        #self.dns_configurator = ImsDnsClient()
        self.neutronc = NeutronClient(
            utilSys.get_endpoint(
                'network',
                region_name=SysUtil().get_sys_conf()['os_region_name']),
            utilSys.get_token())

    def run(self):
        while not self.is_stopped:
            self.update_topology_state()
            if self.topology.state == 'DELETED':
                TopologyOrchestrator.delete(self.topology)
                self.is_stopped = True
            for si in self.topology.service_instances:
                if not self.is_stopped:
                    for unit in si.units:
                        if len(unit.ports) == 0:
                            self.set_ips(unit)
            if self.topology.state == 'DEPLOYED' and not self.is_dns_configured:
                #self.configure_dns()
                self.configure_topology()
                self.is_dns_configured = True
            time.sleep(30)

    def configure_dns(self):
        for si in self.topology.service_instances:
            for unit in si.units:
                try:
                    self.dns_configurator.configure_dns_entry(
                        si.service_type, unit.hostname)(unit.ips['mgmt'])
                except:
                    logging.debug(
                        "this service instance is not needed in the dns")

    def configure_topology(self):
        for si in self.topology.service_instances:
            si.adapter_instance = FactoryServiceAdapter.get_agent(
                si.service_type, si.adapter)
            for unit in si.units:
                try:
                    config = {}
                    config['hostname'] = unit.hostname
                    config['ips'] = unit.ips
                    config['floating_ips'] = unit.floating_ips
                    config['hostname'] = unit.hostname
                except:
                    logging.debug(
                        "there was an issue getting the config for the vnf")

                try:
                    logging.info(
                        "sending requests to the adapter %s with config" %
                        config)
                    si.adapter_instance.preinit(config)
                    si.adapter_instance.install(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)

                # add relations
                for ext_service in si.relation:
                    service_list = self.db.get_by_name(ServiceInstance,
                                                       ext_service.name)
                    if len(service_list) == 1:
                        ext_si = service_list[0]
                        for ext_unit in ext_si.units:
                            logging.info(
                                "sending request add_dependency to the adapter %s with config %s and ext_unit %s"
                                % (si.service_type, config, ext_unit))
                            si.adapter_instance.add_dependency(
                                config, ext_unit, ext_si)
                try:
                    # TODO add add_relation methods
                    si.adapter_instance.pre_start(config)
                    si.adapter_instance.start(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)
Пример #40
0
class CheckerThread(threading.Thread):
    def __init__(self, topology):
        super(CheckerThread, self).__init__()
        self.heatclient = HeatClient()
        self.topology = topology
        self.db = DatabaseManager()
        self.is_stopped = False
        self.is_dns_configured = False
        self.novac = NovaClient()
        #self.dns_configurator = ImsDnsClient()
        self.neutronc = NeutronClient(utilSys.get_endpoint('network', region_name=SysUtil().get_sys_conf()['os_region_name']), utilSys.get_token())

    def run(self):
        while not self.is_stopped:
            self.update_topology_state()
            if self.topology.state == 'DELETED':
                TopologyOrchestrator.delete(self.topology)
                self.is_stopped = True
            for si in self.topology.service_instances:
                if not self.is_stopped:
                    for unit in si.units:
                        if len(unit.ports) == 0:
                            self.set_ips(unit)
            if self.topology.state == 'DEPLOYED' and not self.is_dns_configured:
                #self.configure_dns()
                self.configure_topology()
                self.is_dns_configured = True
            time.sleep(30)


    def configure_dns(self):
        for si in self.topology.service_instances:
            for unit in si.units:
                try:
                    self.dns_configurator.configure_dns_entry(si.service_type, unit.hostname)(unit.ips['mgmt'])
                except:
                    logging.debug("this service instance is not needed in the dns")

    def configure_topology(self):
        for si in self.topology.service_instances:
            si.adapter_instance = FactoryServiceAdapter.get_agent(si.service_type, si.adapter)
            for unit in si.units:
                try:
                    config = {}
                    config['hostname'] = unit.hostname
                    config['ips'] = unit.ips
                    config['floating_ips'] = unit.floating_ips
                    config['hostname'] = unit.hostname
                except:
                    logging.debug("there was an issue getting the config for the vnf")

                try:
                    logging.info("sending requests to the adapter %s with config" % config)
                    si.adapter_instance.preinit(config)
                    si.adapter_instance.install(config)
                except Exception,e:
                    logging.error("error while configuring vnf %s" % e)

                # add relations
                for ext_service in si.relation:
                    service_list = self.db.get_by_name(ServiceInstance, ext_service.name)
                    if len(service_list) == 1:
                        ext_si = service_list[0]
                        for ext_unit in ext_si.units:
                            logging.info("sending request add_dependency to the adapter %s with config %s and ext_unit %s" % (si.service_type, config, ext_unit))
                            si.adapter_instance.add_dependency(config, ext_unit, ext_si)
                try:
                    # TODO add add_relation methods
                    si.adapter_instance.pre_start(config)
                    si.adapter_instance.start(config)
                except Exception,e:
                    logging.error("error while configuring vnf %s" % e)
Пример #41
0
 def dump_to_dict(self):
     db = DatabaseManager()
     resource = {}
     server_config = {}
     server_config['type'] = self.type
     properties = {}
     properties['name'] = self.name
     properties['image'] = self.image
     properties['flavor'] = self.flavor
     if self.key_name is not None: properties['key_name'] = self.key_name
     if self.availability_zone is not None: properties['availability_zone'] = self.availability_zone
     if self.network_ports is not None:
         networks = []
         LOG.debug(self.network_ports)
         for network_port in self.network_ports:
             networks.append({'port': { 'get_resource' : network_port.name}})
         properties['networks'] = networks
     if self.user_data:
         properties['user_data_format'] = 'RAW'
         properties['user_data'] = {}
         properties['user_data']['str_replace'] = {}
         properties['user_data']['str_replace']['template'] = ''
         _user_data = ''
         _user_data_list = []
         for command in self.user_data:
             _user_data += "%s\n" % command.command
         properties['user_data']['str_replace']['template'] = SysUtil.literal_unicode((_user_data))
         properties['user_data']['str_replace']['params'] = {'':''}
         if self.requirements:
             params = {}
             for requirement in self.requirements:
                 try:
                     source_service_instances = db.get_by_name(ServiceInstance,requirement.source)
                 except:
                     LOG.debug('ERROR: Entry %s was not found in Table ServiceInstance' % requirement.source)
                     raise
                 source_units = []
                 if source_service_instances:
                     source_service_instance = source_service_instances[0]
                     source_units = source_service_instance.units
                     LOG.debug(source_units)
                     if source_units:
                         if requirement.parameter == 'private_ip' or requirement.parameter == 'public_ip':
                             #Get requested network specified in the requirement
                             _networks = [network for network in source_service_instance.networks if network.name == requirement.obj_name ]
                             _network = None
                             if _networks:
                                 _network_id = _networks[0].private_net
                             else:
                                 LOG.debug('ERROR: obj_name %s was not found in networks of ServiceInstance %s' % (requirement.obj_name,source_service_instance))
                                 raise
                             #Get network name of the specified network id
                             _network_names = [network.name for network in db.get_all(Network) if network.ext_id == _network_id]
                             _network_name = None
                             if _network_names:
                                 _network_name = _network_names[0]
                             else:
                                 LOG.debug('ERROR: Cannot find network with id %s in Table Network' % _network_id)
                             if requirement.parameter == "private_ip":
                                 ip_number = 0
                             elif requirement.parameter == "public_ip":
                                 ip_number = 1
                             #Create the variable
                             _params = {}
                             _first_unit = source_units[0]
                             _template = '$%s' % _first_unit.hostname
                             _params['$%s' % _first_unit.hostname] = {'get_attr': [_first_unit.hostname, 'networks', _network_name, ip_number]}
                             for source_unit in source_units[1:]:
                                 _template += ';$%s' % source_unit.hostname
                                 _params['$%s' % source_unit.hostname] = {'get_attr': [source_unit.hostname, 'networks', _network_name, ip_number]}
                         param = {}
                         param[requirement.name] = {}
                         param[requirement.name]['str_replace'] = {}
                         param[requirement.name]['str_replace']['template'] = _template
                         param[requirement.name]['str_replace']['params'] = _params
                         params.update(param)
                     else:
                         LOG.debug('ERROR: Units for ServiceInstance %s were not found.' % requirement.source)
                         raise Exception
                 else:
                     LOG.debug('ERROR: ServiceInstance %s was not found' % requirement.source)
                     raise Exception
             properties['user_data']['str_replace']['params'] = params
     server_config['properties'] = properties
     resource[self.name] = server_config
     return resource
Пример #42
0
    resp = (response.read())

    logger.debug(resp)

    logger.debug(
        '\n###################CREATE SECURITY GROUPS###################')
    connection.request('GET', '/secgroups')
    response = connection.getresponse()
    resp = (response.read())

    logger.debug(resp)

    logger.debug(
        '\n###################CREATE SERVICE###########################')

    db = DatabaseManager()

    lst = [db.get_all(SecurityGroup)]
    _json = create_service(lst)

    connection.request('POST', '/services', _json, headers)
    response = connection.getresponse()
    resp = (response.read())

    logger.debug(resp)

    logger.debug(
        '\n####################GET SERVICES############################')
    connection.request('GET', '/services')

    response = connection.getresponse()
Пример #43
0
    def get_template(topology):
        #name = topology.name
        _template = {}
        _template['heat_template_version'] = '2013-05-23'
        _resources = {}
        _outputs = {}
        LOG.debug("create Template for Topology: %s" % topology.name)

        db = DatabaseManager()

        for service_instance in topology.service_instances:
            _software_config = None
            #Create SoftwareConfig for user_data
            if service_instance.user_data:
                _inputs = []
                if service_instance.requirements:
                    for requirement in service_instance.requirements:
                        _inputs.append(requirement.name)
                _software_config = SoftwareConfig(
                    name='%s-SoftwareConfig' % service_instance.name,
                    config=service_instance.user_data,
                    group=service_instance.name,
                    inputs=_inputs)

            for unit in service_instance.units:
                #Create input values for user_data for SoftwareDeployment
                if service_instance.requirements or service_instance.user_data:
                    _input_values = {}
                    for requirement in service_instance.requirements:
                        try:
                            source_service_instances = db.get_by_name(
                                ServiceInstance, requirement.source)
                        except:
                            LOG.debug(
                                'ERROR: Entry %s was not found in Table ServiceInstance'
                                % requirement.source)
                            raise
                        source_units = []
                        if source_service_instances:
                            source_service_instance = source_service_instances[
                                0]
                            source_units = source_service_instance.units
                            LOG.debug(source_units)
                            if source_units:
                                if requirement.parameter == 'private_ip' or requirement.parameter == 'public_ip':
                                    #Get requested network specified in the requirement
                                    _networks = [
                                        network for network in
                                        source_service_instance.networks
                                        if network.name == requirement.obj_name
                                    ]
                                    _network = None
                                    if _networks:
                                        _network_id = _networks[0].private_net
                                    else:
                                        LOG.debug(
                                            'ERROR: obj_name %s was not found in networks of ServiceInstance %s'
                                            % (requirement.obj_name,
                                               source_service_instance))
                                        raise
                                    #Get network name of the specified network id
                                    _network_names = [
                                        network.name
                                        for network in db.get_all(Network)
                                        if network.ext_id == _network_id
                                    ]
                                    _network_name = None
                                    if _network_names:
                                        _network_name = _network_names[0]
                                    else:
                                        LOG.debug(
                                            'ERROR: Cannot find network with id %s in Table Network'
                                            % _network_id)
                                    if requirement.parameter == "private_ip":
                                        ip_number = 0
                                    elif requirement.parameter == "public_ip":
                                        ip_number = 1
                                    #Create the variable
                                    _param_params = {}
                                    _first_unit = source_units[0]
                                    _param_template = '$%s' % _first_unit.hostname
                                    _param_params['$%s' %
                                                  _first_unit.hostname] = {
                                                      'get_attr': [
                                                          _first_unit.hostname,
                                                          'networks',
                                                          _network_name,
                                                          ip_number
                                                      ]
                                                  }
                                    for source_unit in source_units[1:]:
                                        _param_template += ';$%s' % source_unit.hostname
                                        _param_params[
                                            '$%s' % source_unit.hostname] = {
                                                'get_attr': [
                                                    source_unit.hostname,
                                                    'networks', _network_name,
                                                    ip_number
                                                ]
                                            }
                                param = {}
                                param[requirement.name] = {}
                                param[requirement.name]['str_replace'] = {}
                                param[requirement.name]['str_replace'][
                                    'template'] = _param_template
                                param[requirement.name]['str_replace'][
                                    'params'] = _param_params
                                _input_values.update(param)
                            else:
                                LOG.debug(
                                    'ERROR: Units for ServiceInstance %s were not found.'
                                    % requirement.source)
                                raise Exception
                        else:
                            LOG.debug(
                                'ERROR: ServiceInstance %s was not found' %
                                requirement.source)
                            raise Exception
                    _software_deployment = SoftwareDeployment(
                        name='%s-SoftwareDeployment' % unit.hostname,
                        config=_software_config,
                        server=unit,
                        input_values=_input_values)
                    _resources.update(_software_deployment.dump_to_dict())

                #Create Ports and floating IPs for this unit
                _ports = []
                _floating_ips = []
                if service_instance.networks:
                    i = 1
                    for network in service_instance.networks:
                        ###Creating Port for this service instance
                        _new_port = None
                        #prepare port args for this service instance
                        _port_args = {}
                        _port_args['name'] = '%s-port-%s' % (unit.hostname, i)
                        _port_args['private_net_id'] = network.private_net
                        _port_args[
                            'private_subnet_id'] = network.private_subnet
                        _port_args['fixed_ip'] = network.fixed_ip
                        if network.security_groups:
                            _port_args[
                                'security_groups'] = network.security_groups
                        _new_port = Port(**_port_args)
                        _ports.append(_new_port)
                        if network.public_net:
                            _new_floating_ip_args = {}
                            _new_floating_ip_args[
                                'name'] = '%s-floating_ip-%s' % (unit.hostname,
                                                                 i)
                            _new_floating_ip_args[
                                'floating_network_id'] = network.public_net
                            _new_floating_ip_args['port'] = _new_port.name
                            _new_floating_ip = FloatingIP(
                                **_new_floating_ip_args)
                            _floating_ips.append(_new_floating_ip)
                        ###Adding Security Groups
                        for _security_group in network.security_groups:
                            _new_name = _security_group.name
                            _new_rules = []
                            _rules = _security_group.rules
                            for _rule in _rules:
                                _name = _rule.name
                                _remote_ip_prefix = _rule.remote_ip_prefix
                                _protocol = _rule.protocol
                                _port_range_max = int(
                                    _rule.port_range_max
                                ) if _rule.port_range_max else None
                                _port_range_min = int(
                                    _rule.port_range_min
                                ) if _rule.port_range_min else None
                                _new_rule = Rule(_name, _remote_ip_prefix,
                                                 _protocol, _port_range_max,
                                                 _port_range_min)
                                _new_rules.append(_new_rule)
                            _new_security_group = SecurityGroup(
                                name=_new_name, rules=_new_rules)
                            _resources.update(
                                _new_security_group.dump_to_dict())
                        i += 1

                ###Create Server for this service instance
                _new_server = None
                #prepare server args
                _server_args = {}
                _server_args['name'] = "%s" % unit.hostname
                _server_args['hostname'] = "%s" % unit.hostname
                _server_args['flavor'] = service_instance.flavor.name
                _server_args['image'] = service_instance.image.name
                _server_args['key_name'] = service_instance.key.name
                _server_args['network_ports'] = _ports
                _server_args['user_data'] = service_instance.user_data
                _server_args['requirements'] = service_instance.requirements
                _new_server = Server(**_server_args)

                _resources.update(_new_server.dump_to_dict())

                if _software_config:
                    _resources.update(_software_config.dump_to_dict())
                if _ports:
                    for _port in _ports:
                        _resources.update(_port.dump_to_dict())
                if _floating_ips:
                    for _floating_ip in _floating_ips:
                        _resources.update(_floating_ip.dump_to_dict())
        print _resources
        _template['resources'] = _resources

        ###Output section###
        db = DatabaseManager()
        for service_instance in topology.service_instances:
            for network_instance in service_instance.networks:
                if network_instance.public_net:
                    _public_network_names = [
                        _network.name for _network in db.get_all(Network)
                        if _network.ext_id == network_instance.public_net
                    ]
                    _public_network_name = None
                    if _public_network_names:
                        _public_network_name = _public_network_names[0]
                    else:
                        LOG.debug(
                            'ERROR: Cannot find network with id %s in Table Network'
                            % network_instance.public_net)
                if network_instance.private_net:
                    _private_network_names = [
                        _network.name for _network in db.get_all(Network)
                        if _network.ext_id == network_instance.private_net
                    ]
                    _private_network_name = None
                    if _private_network_names:
                        _private_network_name = _private_network_names[0]
                    else:
                        LOG.debug(
                            'ERROR: Cannot find network with id %s in Table Network'
                            % network_instance.net)
                for unit in service_instance.units:
                    if network_instance.public_net and _public_network_name:
                        output = {}
                        output['value'] = {
                            'get_attr': [
                                unit.hostname, 'networks',
                                _private_network_name, 1
                            ]
                        }
                        output[
                            'description'] = 'Public IP of %s.' % unit.hostname
                        _outputs['mcn.endpoint.%s' % unit.hostname] = output
                    elif network_instance.private_net and _private_network_name:
                        output = {}
                        output['value'] = {
                            'get_attr': [
                                unit.hostname, 'networks',
                                _private_network_name, 0
                            ]
                        }
                        output[
                            'description'] = 'Private IP of %s.' % unit.hostname
                        _outputs['mcn.endpoint.%s' % unit.hostname] = output
        _template['outputs'] = _outputs

        yaml.add_representer(
            unicode, lambda dumper, value: dumper.represent_scalar(
                u'tag:yaml.org,2002:str', value))
        yaml.add_representer(SysUtil.literal_unicode,
                             SysUtil.literal_unicode_representer)
        LOG.debug((_template))
        #LOG.debug(yaml.dumps(template))
        #f = open('/net/u/mpa/tempalte_file.yaml', 'w')
        #f.write(yaml.dump(template, indent=2))
        return yaml.dump(_template)
Пример #44
0
class CheckerThread(threading.Thread):
    def __init__(self, topology):
        super(CheckerThread, self).__init__()
        self.heatclient = HeatClient()
        self.topology = topology
        self.db = DatabaseManager()
        self.is_stopped = False
        self.is_dnsaas = False
        self.dnsaas = None
        self.novac = NovaClient()
        self.dns_configurator = None
        self.neutronc = NeutronClient(
            utilSys.get_endpoint("network", region_name=SysUtil().get_sys_conf()["os_region_name"]), utilSys.get_token()
        )
        self.dns_configurator = None

    def run(self):
        while not self.is_stopped:
            self.update_topology_state()
            if self.topology.state == "DELETED":
                TopologyOrchestrator.delete(self.topology)
                self.is_stopped = True
            for si in self.topology.service_instances:
                if not self.is_stopped:
                    for unit in si.units:
                        if len(unit.ports) == 0:
                            self.set_ips(unit)
            time.sleep(30)

    def provision(self, dnsaas):
        if dnsaas is not None:
            self.dns_configurator = ImsDnsClient(dnsaas)
            self.is_dnsaas = True
        while not self.topology.state == "DEPLOYED":
            logger.info("topology not yet in status deployed, currently %s" % self.topology.state)
            time.sleep(5)
        self.configure_topology()

    def configure_topology(self):
        for si in self.topology.service_instances:
            si.adapter_instance = FactoryServiceAdapter.get_agent(si.service_type, si.adapter)
            for unit in si.units:
                try:
                    config = {}
                    config["hostname"] = unit.hostname
                    config["ips"] = unit.ips
                    config["zabbix_ip"] = os.environ["ZABBIX_IP"]
                    config["floating_ips"] = unit.floating_ips
                    config["hostname"] = unit.hostname
                except:
                    logging.error("there was an issue getting the config for the vnf")

                try:
                    logging.info("sending requests to the adapter %s with config" % config)
                    si.adapter_instance.preinit(config)
                    si.adapter_instance.install(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)

                # add relations
                for ext_service in si.relation:
                    logger.info(
                        "solving dependencies between si %s and external service %s" % (si.name, ext_service.name)
                    )
                    if ext_service.name == "dns" and self.is_dnsaas is True:
                        ext_unit = Unit(hostname=None, state="INITIALISING")
                        ext_si = ServiceInstance(
                            name="dns", service_type="dns", state="INITIALISING", image="test", flavor="test", size={}
                        )
                        ext_unit.ips["mgmt"] = os.environ["DNSAAS_IP"]
                        self.dns_configurator.configure_dns_entry(si.service_type)(unit.ips["mgmt"], unit.hostname)
                        si.adapter_instance.add_dependency(config, ext_unit, ext_si)
                    else:
                        service_list = self.db.get_by_name(ServiceInstance, ext_service.name)
                        if len(service_list) == 1:
                            ext_si = service_list[0]
                            for ext_unit in ext_si.units:
                                logging.info(
                                    "sending request add_dependency to the adapter %s with config %s and ext_unit %s"
                                    % (si.service_type, config, ext_unit)
                                )
                                si.adapter_instance.add_dependency(config, ext_unit, ext_si)
                try:
                    si.adapter_instance.pre_start(config)
                    si.adapter_instance.start(config)
                except Exception, e:
                    logging.error("error while configuring vnf %s" % e)
class TopologyManager(ABCTopologyManager):

    def __init__(self):
        self.db = DatabaseManager()

    def create(self, config):
        ###Topology arguments
        top_name = config.get('name')
        top_state = 'DEFINED'
        top_service_instances = []
        logger.debug("Parse Topology \"%s\"" % top_name)
        ###Parse all service instances described in config file
        sis_config = config.get('service_instances')
        for si_config in sis_config:
            si_args = {}
            si_args['state'] = 'DEFINED'
            service = None
            if si_config.get('service_type'):
                service_type = si_config.get('service_type')
                logger.debug('Fetching service of service_type \"%s\"' % service_type)
                service_list = self.db.get_by_service_type(Service, service_type)
                if len(service_list) == 1:
                    service = service_list[0]
                    logger.debug('Service \"%s\" is available.' % service)
                    si_args['service_type'] = service.service_type
                    si_args['image'] = service.image
                    si_args['flavor'] = service.flavor
                    si_args['configuration'] = service.configuration
                    si_args['key'] = service.key
                    si_args['size'] = service.size
                    si_args['adapter'] = service.adapter
                    si_args['requirements'] = []
                    for requirement in service.requirements:
                        func = Requirement.__init__
                        needed_parameters = func.func_code.co_varnames[1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = requirement.__dict__.get(param)
                        new_requirement = Requirement(**args)
                        si_args['requirements'].append(new_requirement)
                    si_args['networks'] = []
                    for network in service.networks:
                        func = Network_Instance.__init__
                        needed_parameters = func.func_code.co_varnames[1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = network.__dict__.get(param)
                        new_network = Network_Instance(**args)
                        si_args['networks'].append(new_network)
                    si_args['user_data'] = []
                    for command in service.user_data:
                        func = Command.__init__
                        needed_parameters = func.func_code.co_varnames[1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = command.__dict__.get(param)
                        new_command = Command(**args)
                        si_args['user_data'].append(new_command)
                else:
                    raise NotFoundException('service_type:\"%s\" is not available.' % service_type)
            else:
                raise NotDefinedException("service_type is not defined.")
            for si_item in si_config:
                if si_item == "name":
                    si_args['name'] = si_config.get(si_item)
                    logger.debug("Parsing service instance \"%s\"" % si_args['name'])
                elif si_item == "service_type":
                    si_args['service_type'] = si_config.get(si_item)
                elif si_item == "adapter":
                    si_args['adapter'] = si_config.get(si_item)
                elif si_item == "image":
                    image_name = si_config.get(si_item)
                    image_list = self.db.get_by_name(Image, image_name)
                    if len(image_list) == 1:
                        image = image_list[0]
                        si_args['image'] = image
                    else:
                        raise NotFoundException('image:\"%s\" is not available.' % image_name)
                elif si_item == "flavor":
                    flavor_name = si_config.get(si_item)
                    flavor_list = self.db.get_by_name(Flavor, flavor_name)
                    if len(flavor_list) == 1:
                        flavor = flavor_list[0]
                        si_args['flavor'] = flavor
                    else:
                        raise NotFoundException('flavor:\"%s\" is not available.' % flavor_name)
                elif si_item == "key":
                    key_name = si_config.get(si_item)
                    key_list = self.db.get_by_name(Key, key_name)
                    if len(key_list) == 1:
                        key = key_list[0]
                        si_args['key'] = key
                    else:
                        raise NotFoundException('key:\"%s\" is not available.' % key_name)
                elif si_item == "size":
                    si_args['size'].update(si_config.get(si_item))
                elif si_item == "networks":
                    networks = []
                    _networks = si_config.get(si_item)
                    logger.debug("Fetch SecurityGroups for networks %s." % _networks)
                    for _net_inst in _networks:
                        secgroups = []
                        for _secgroup_name in _net_inst.get('security_groups'):
                            lst = self.db.get_by_name(SecurityGroup, _secgroup_name)
                            if len(lst) > 0:
                                _secgroup = lst[0]
                            else:
                                raise NotFoundException('SecurityGroup:\"%s\" is not available.'  % _secgroup_name)
                            secgroups.append(_secgroup)
                        _net_inst['security_groups'] = secgroups
                        networks.append(Network_Instance(**_net_inst))
                    si_args['networks'] = networks
                elif si_item == "configuration":
                    for key in si_config.get(si_item).keys():
                        if key in service.configuration.keys():
                            si_args['configuration'][key] = si_config.get(si_item).get(key)
                elif si_item == "policies":
                    policies = []
                    _policies = si_config.get(si_item)
                    for _policy in _policies:
                        _new_policy_args = {}
                        for _po_item in _policy:
                            if _po_item == "name":
                                _new_policy_args.update({'name':_policy.get(_po_item)})
                            elif _po_item == "period":
                                _new_policy_args.update({'period':_policy.get(_po_item)})
                            elif _po_item == "alarm":
                                _new_alarm_args = _policy.get(_po_item)
                                _new_alarm = Alarm(**_new_alarm_args)
                                _new_policy_args.update({'alarm':_new_alarm})
                            elif _po_item == "action":
                                _new_action_args = _policy.get(_po_item)
                                _new_action = Action(**_new_action_args)
                                _new_policy_args.update({'action':_new_action})
                        try:
                            _new_policy = Policy(**_new_policy_args)
                        except TypeError:
                            raise InvalidInputException()
                        policies.append(_new_policy)
                    si_args['policies'] = policies
                elif si_item == "requirements":
                   requirements = []
                   for _req in si_config.get(si_item):
                       _req_args = _req
                       requirement = Requirement(**_req_args)
                       requirements.append(requirement)
                   si_args['requirements'] = requirements
                elif si_item == "user_data":
                    user_data = []
                    for _user_data_item in  si_config.get(si_item):
                        command = Command(_user_data_item)
                        user_data.append(command)
                    si_args['user_data'] = user_data
                elif si_item == "location":
                    location = []
                    for _loc in si_config.get(si_item):
                        logger.debug("found item location %s"%_loc)
                        loc = Location(_loc)
                        location.append(loc)
                    si_args['location'] = location
                elif si_item == "relation":
                    relation = []
                    for _rel in si_config.get(si_item):
                        logger.debug("found item relation %s"%_rel)
                        rel = Relation(_rel)
                        relation.append(rel)
                    si_args['relation'] = relation
                else:
                    raise InvalidInputException("parameter \"%s\" is not provided by Services." % si_config.get(si_item))

            ###Initialise Units
            units = []
            unit_number = si_args.get('size').get('def') or 1
            for i in range(1,unit_number+1):
                _hostname = '%s-%s' % (si_args.get('name'),i)
                _new_unit = Unit(hostname=_hostname, state='DEFINED')
                units.append(_new_unit)
            si_args['units'] = units
            ###Initialise Service Instance
            new_service_instance = ServiceInstance(**si_args)
            ###Add the new service instance to the topology
            top_service_instances.append(new_service_instance)

        ###Initialise Topology
        ext_name = '' + top_name + '_' +str(random.randint(1000,9999))
        topology = Topology(name=top_name, state=top_state, service_instances=top_service_instances, ext_name=ext_name)
        logger.debug(topology)
        return topology

    def update(self, new_topology, old_topology):
        conf = sys_util().get_sys_conf()
        db = FactoryAgent().get_agent(conf['database_manager'])
        updated_topology = old_topology
        updated_topology.name = new_topology.name
        #check for additional service instances and add them to the list of new instances
        appended_service_instances = []
        for new_service_instance in new_topology.service_instances:
            is_found = False
            for updated_service_instance in updated_topology.service_instances:
                if new_service_instance.name == updated_service_instance.name:
                    is_found = True
                    break
            if not is_found:
                appended_service_instances.append(new_service_instance)
        #check for removed service instances and add it to the list of removed instances
        removed_service_instances = []
        for updated_service_instance in updated_topology.service_instances:
            is_found = False
            for new_service_instance in new_topology.service_instances:
                if new_service_instance.name == updated_service_instance.name:
                    is_found = True
                    break
            if not is_found:
                removed_service_instances.append(updated_service_instance)
        #remove removed service instances
        for removed_service_instance in removed_service_instances:
            updated_topology.service_instances.remove(removed_service_instance)
            logger.debug('Removed ServiceInstance \"%s\" from Topology \"%s\".' % (removed_service_instance.name, updated_topology.name))
        #append additional service instances
        for appended_service_instance in appended_service_instances:
            appended_service_instance.topology_id = updated_topology.id
            updated_topology.service_instances.append(appended_service_instance)
            if appended_service_instance.policies is not None:
                for policy in appended_service_instance.policies:
                    db.persist(policy)
            db.persist(appended_service_instance)
            logger.debug('Appended ServiceInstance \"%s\" to Topology \"%s\".' % (appended_service_instance.name, updated_topology.name))
        #Update all values for each service instance
        for updated_service_instance in updated_topology.service_instances:
            for new_service_instance in new_topology.service_instances:
                if updated_service_instance.name == new_service_instance.name:
                    updated_service_instance.size = new_service_instance.size
                    updated_service_instance.configuration = new_service_instance.configuration
                    updated_service_instance.policies = new_service_instance.policies
                    #updated_service_instance.service_type = new_service_instance.service_type
                    if new_service_instance.service_type and updated_service_instance.service_type != new_service_instance.service_type:
                        logger.warning("Cannot update service_type for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    if new_service_instance.adapter and updated_service_instance.adapter != new_service_instance.adapter:
                        logger.warning("Cannot update adapter for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    #updated_service_instance.flavor = new_service_instance.flavor
                    if new_service_instance.flavor and updated_service_instance.flavor.name != new_service_instance.flavor.name:
                        logger.warning("Cannot update flavor for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    #updated_service_instance.image = new_service_instance.image
                    if new_service_instance.image and updated_service_instance.image.name != new_service_instance.image.name:
                        logger.warning("Cannot update image for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    #updated_service_instance.networks = new_service_instance.networks
                    if new_service_instance.networks is not None:
                        logger.warning("Cannot update networks for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    #updated_service_instance.requirements = new_service_instance.requirements
                    if new_service_instance.requirements is not None:
                        logger.warning("Cannot update networks for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    #updated_service_instance.user_data = new_service_instance.user_data
                    if new_service_instance.user_data is not None:
                        logger.warning("Cannot update user_data for %s->%s. Not Implemented." % (updated_topology.name, updated_service_instance.name))
                    if new_service_instance.key and updated_service_instance.key.name != new_service_instance.key.name:
                        logger.warning("Cannot update key for %s->%s without replacement." % (updated_topology.name, updated_service_instance.name))
        #Add or remove units according to minimal or maximal size
        for updated_service_instance in updated_topology.service_instances:
            if updated_service_instance not in appended_service_instances:
                if len(updated_service_instance.units) < updated_service_instance.size.get('min'):
                    for i in range(updated_service_instance.size.get('min') - len(updated_service_instance.units)):
                        _hostname = '%s-%s' % (
                            updated_service_instance.name, str(len(updated_service_instance.units) + 1))
                        _state = 'DEFINED'
                        new_unit = Unit(hostname=_hostname, state=_state)
                        new_unit.service_instance_id = updated_service_instance.id
                        updated_service_instance.units.append(new_unit)
                        db.persist(new_unit)
                if len(updated_service_instance.units) > updated_service_instance.size.get('max'):
                    for i in range(len(updated_service_instance.units) - updated_service_instance.size.get('max')):
                        removed_unit = updated_service_instance.units.pop(len(updated_service_instance.units) - 1)
                        db.remove(removed_unit)
        return updated_topology

    def dynamic_create(self, dict):
        logger.debug(dict)
        t = Topology(**dict)
        logger.debug(t.__dict__)
        return t

    def get_attrs(self,_class):
        return [k for k in dir(_class) if not k.startswith('__') ]
Пример #46
0
    connection.request('POST', '/secgroups', _json, headers)
    response = connection.getresponse()
    resp = (response.read())

    logger.debug(resp)

    logger.debug('\n###################CREATE SECURITY GROUPS###################')
    connection.request('GET', '/secgroups')
    response = connection.getresponse()
    resp = (response.read())

    logger.debug(resp)

    logger.debug('\n###################CREATE SERVICE###########################')

    db = DatabaseManager()

    lst = [db.get_all(SecurityGroup)]
    _json = create_service(lst)

    connection.request('POST', '/services', _json, headers)
    response = connection.getresponse()
    resp = (response.read())

    logger.debug(resp)


    logger.debug('\n####################GET SERVICES############################')
    connection.request('GET', '/services')

    response = connection.getresponse()
Пример #47
0
 def get_all(cls):
     return DatabaseManager().get_all(Service)
 def __init__(self):
     self.db = DatabaseManager()
Пример #49
0
 def __init__(self):
     self.db = DatabaseManager()
Пример #50
0
def checkNetwork(network):
    try:
        db = DatabaseManager()
        existing_networks = db.get_all(Network)
        found_private_net = False
        found_subnet = False
        found_public_net = False
        for existing_network in existing_networks:
            if network.private_net == existing_network.ext_id and not found_private_net:
                if existing_network.public == False:
                    LOG.debug("private_network \"%s\" is available." %
                              network.private_net)
                    found_private_net = True
                else:
                    raise InvalidInputException(
                        "private_network:\"%s\" is available but it is marked as public and not as private as defined."
                        % network.private_net)
                for subnet in existing_network.subnets:
                    if network.private_subnet == subnet.ext_id and not found_subnet:
                        found_subnet = True
                if found_subnet:
                    LOG.debug("private_subnet \"%s\" is available." %
                              network.private_subnet)
                else:
                    raise InvalidInputException(
                        "private_subnet:\"%s\" is not available." %
                        network.private_subnet)
            if network.public_net == existing_network.ext_id and not found_public_net:
                if existing_network.public == True:
                    LOG.debug("public_network \"%s\" is available." %
                              network.public_net)
                    found_public_net = True
                else:
                    raise InvalidInputException(
                        "network:\"%s\" is available but it is marked as private and not as public as defined."
                        % network.public_net)
        if not network.private_net and not network.private_subnet and not network.public_net:
            LOG.debug("Networks were not defined.")
        elif network.private_net and network.private_subnet and network.public_net:
            if found_private_net and found_subnet and found_public_net:
                LOG.debug(
                    "All defined networks are available for network: %s" %
                    network)
            if not found_private_net:
                raise NotFoundException("Not found private network: %s" %
                                        network)
            if not found_subnet:
                raise NotFoundException(
                    "Not found private subnet network: %s" % network)
            if not found_public_net:
                raise NotFoundException("Not found public network: %s" %
                                        network)
        elif network.private_net and network.private_subnet and not network.public_net:
            if found_private_net and found_subnet and not found_public_net:
                LOG.debug(
                    "All defined networks are available for network: %s" %
                    network)
            if not found_private_net:
                raise NotFoundException("Not found private network: %s" %
                                        network)
            if not found_subnet:
                raise NotFoundException(
                    "Not found private subnet network: %s" % network)
        elif not network.private_net and network.public_net:
            raise InvalidInputException(
                "Private net is not defined but the public.")
        else:
            raise InvalidInputException("Error while checking networks.")
    except Exception, exc:
        exc.message = 'Network:\"%s\"->%s' % (network.name, exc.message)
        raise exc
Пример #51
0
class ServiceManager(ABCServiceManager):
    def __init__(self):
        self.db = DatabaseManager()

    def create(self, config):
        ###Service arguments
        service_type = config.get('service_type')
        LOG.debug("Parse Service \"%s\"" % service_type)
        ###Parse each item described in config file
        new_service_args = {}
        for service_item in config.keys():
            if service_item == "service_type":
                new_service_args['service_type'] = config.get(service_item)
            elif service_item == "image":
                image_name = config.get(service_item)
                image_list = self.db.get_by_name(Image, image_name)
                if len(image_list) == 1:
                    image = image_list[0]
                    new_service_args['image'] = image
                else:
                    raise NotFoundException('image:\"%s\" is not available.' %
                                            image_name)
            elif service_item == "flavor":
                flavor_name = config.get(service_item)
                flavor_list = self.db.get_by_name(Flavor, flavor_name)
                if len(flavor_list) == 1:
                    flavor = flavor_list[0]
                    new_service_args['flavor'] = flavor
                else:
                    raise NotFoundException('flavor:\"%s\" is not available.' %
                                            flavor_name)
            elif service_item == "key":
                key_name = config.get(service_item)
                key_list = self.db.get_by_name(Key, key_name)
                if len(key_list) == 1:
                    key = key_list[0]
                    new_service_args['key'] = key
                else:
                    raise NotFoundException('key:\"%s\" is not available.' %
                                            key_name)
            elif service_item == "size":
                new_service_args['size'] = config.get(service_item)
            elif service_item == "networks":
                networks = []
                _networks = config.get(service_item)
                LOG.debug("Fetch SecurityGroups for networks %s." % _networks)
                for _net_inst in _networks:
                    secgroups = []
                    for _secgroup_name in _net_inst.get('security_groups'):
                        lst = self.db.get_by_name(SecurityGroup,
                                                  _secgroup_name)
                        if len(lst) > 0:
                            _secgroup = lst[0]
                        else:
                            raise NotFoundException(
                                'SecurityGroup:\"%s\" is not available.' %
                                _secgroup_name)
                        secgroups.append(_secgroup)
                    _net_inst['security_groups'] = secgroups
                    networks.append(Network_Instance(**_net_inst))
                new_service_args['networks'] = networks
            elif service_item == "configuration":
                new_service_args['configuration'] = config.get(service_item)
            elif service_item == "requirements":
                requirements = []
                for _req in config.get(service_item):
                    _req_args = _req
                    requirement = Requirement(**_req_args)
                    requirements.append(requirement)
                new_service_args['requirements'] = requirements
            elif service_item == "user_data":
                user_data = []
                for _user_data_item in config.get(service_item):
                    command = Command(_user_data_item)
                    user_data.append(command)
                new_service_args['user_data'] = user_data
            elif service_item == "version":
                new_service_args['version'] = config.get(service_item)
            elif service_item == "adapter":
                new_service_args['adapter'] = config.get(service_item)
            else:
                raise InvalidInputException(
                    "paramter \"%s\" is not provided by Services." %
                    config.get(service_item))

        ###Create Service
        print(new_service_args)
        new_service = Service(**new_service_args)
        LOG.debug(new_service)
        return new_service

    def update(self):
        pass

    def dynamic_create(self, dict):
        LOG.debug(dict)
        t = Topology(**dict)
        LOG.debug(t.__dict__)
        return t

    def get_attrs(self, _class):
        return [k for k in dir(_class) if not k.startswith('__')]

    def get_service_instance(self, _class, _name):
        return self.db.get_by_name(_class, _name)
Пример #52
0
    def get_template(topology):
        #name = topology.name
        template = {}
        template['heat_template_version'] = '2013-05-23'
        resources = {}
        outputs = {}
        #print "create Template for Topology: %s" % name

        for service_instance in topology.service_instances:
            for unit in service_instance.units:
                #Create Ports and floating IPs for this unit
                ports = []
                floating_ips = []
                if service_instance.networks:
                    i = 1
                    for network in service_instance.networks:
                        ###Creating Port for this service instance
                        new_port = None
                        #prepare port args for this service instance
                        port_args = {}
                        port_args['name'] = '%s-port-%s' % (unit.hostname, i)
                        port_args['private_net_id'] = network.private_net
                        port_args['private_subnet_id'] = network.private_subnet
                        # if "hss-1" in unit.hostname:
                        #     port_args['fixed_ip'] = "192.168.9.48"
                        # elif "hss-2" in unit.hostname:
                        #     port_args['fixed_ip'] = "192.168.9.55"
                        # else:
                        port_args['fixed_ip'] = network.fixed_ip

                        if network.security_groups:
                            port_args[
                                'security_groups'] = network.security_groups
                        new_port = Port(**port_args)
                        ports.append(new_port)
                        if network.public_net:
                            new_floating_ip_args = {}
                            new_floating_ip_args[
                                'name'] = '%s-floating_ip-%s' % (unit.hostname,
                                                                 i)
                            new_floating_ip_args[
                                'floating_network_id'] = network.public_net
                            new_floating_ip_args['port'] = new_port.name
                            new_floating_ip = FloatingIP(
                                **new_floating_ip_args)
                            floating_ips.append(new_floating_ip)
                        ###Adding Security Groups
                        for _security_group in network.security_groups:
                            _new_name = _security_group.name
                            _new_rules = []
                            _rules = _security_group.rules
                            for _rule in _rules:
                                _name = _rule.name
                                _remote_ip_prefix = _rule.remote_ip_prefix
                                _protocol = _rule.protocol
                                _port_range_max = int(
                                    _rule.port_range_max
                                ) if _rule.port_range_max else None
                                _port_range_min = int(
                                    _rule.port_range_min
                                ) if _rule.port_range_min else None
                                _new_rule = Rule(_name, _remote_ip_prefix,
                                                 _protocol, _port_range_max,
                                                 _port_range_min)
                                _new_rules.append(_new_rule)
                            _new_security_group = SecurityGroup(
                                name=_new_name, rules=_new_rules)
                            resources.update(
                                _new_security_group.dump_to_dict())
                        i += 1

                ###Create Server for this service instance
                new_server = None
                #prepare server args
                server_args = {}
                server_args['name'] = "%s" % unit.hostname
                server_args['hostname'] = "%s" % unit.hostname
                server_args['availability_zone'] = unit.availability_zone
                server_args['flavor'] = service_instance.flavor.name
                # if "hss-1" in unit.hostname:
                #     server_args['image'] = "hss-1"
                # elif "hss-2" in unit.hostname:
                #     server_args['image'] = "hss-2"
                # else:
                server_args['image'] = service_instance.image.name
                server_args['key_name'] = service_instance.key.name
                server_args['network_ports'] = ports
                server_args['user_data'] = service_instance.user_data
                server_args['requirements'] = service_instance.requirements
                new_server = Server(**server_args)

                resources.update(new_server.dump_to_dict())

                if ports:
                    for port in ports:
                        resources.update(port.dump_to_dict())
                if floating_ips:
                    for floating_ip in floating_ips:
                        resources.update(floating_ip.dump_to_dict())

        template['resources'] = resources

        ###Output section###
        db = DatabaseManager()
        for service_instance in topology.service_instances:
            for network_instance in service_instance.networks:
                if network_instance.public_net:
                    _public_network_names = [
                        _network.name for _network in db.get_all(Network)
                        if _network.ext_id == network_instance.public_net
                    ]
                    _public_network_name = None
                    if _public_network_names:
                        _public_network_name = _public_network_names[0]
                    else:
                        LOG.debug(
                            'ERROR: Cannot find network with id %s in Table Network'
                            % network_instance.public_net)
                if network_instance.private_net:
                    _private_network_names = [
                        _network.name for _network in db.get_all(Network)
                        if _network.ext_id == network_instance.private_net
                    ]
                    _private_network_name = None
                    if _private_network_names:
                        _private_network_name = _private_network_names[0]
                    else:
                        LOG.debug(
                            'ERROR: Cannot find network with id %s in Table Network'
                            % network_instance.net)

                for unit in service_instance.units:
                    if network_instance.public_net and _public_network_name:
                        output = {}
                        floating_name = '%s-floating_ip-1' % unit.hostname
                        output['value'] = {
                            'get_attr': [floating_name, 'floating_ip_address']
                        }
                        output[
                            'description'] = 'Public IP of %s.' % unit.hostname
                        outputs['mcn.endpoint.%s' % unit.hostname] = output
                    elif network_instance.private_net and _private_network_name:
                        output = {}
                        port_name = '%s-port-1' % unit.hostname
                        output['value'] = {
                            'get_attr':
                            [port_name, 'fixed_ips', 0, 'ip_address']
                        }
                        output[
                            'description'] = 'Private IP of %s.' % unit.hostname
                        outputs['mcn.endpoint.%s' % unit.hostname] = output

                    if network_instance.public_net and _public_network_name:
                        network_type = "public"
                        output = {}
                        floating_name = '%s-floating_ip-1' % unit.hostname
                        output['value'] = {
                            'get_attr': [floating_name, 'floating_ip_address']
                        }
                        output['description'] = '%s ip of %s in %s.' % (
                            network_type.capitalize(), unit.hostname,
                            network_instance.name)

                        outputs['mcn.endpoint.%s.%s.%s' %
                                (unit.hostname, network_instance.name,
                                 network_type)] = output

                    if network_instance.private_net and _private_network_name:
                        network_type = "private"
                        output = {}
                        port_name = '%s-port-1' % unit.hostname
                        output['value'] = {
                            'get_attr':
                            [port_name, 'fixed_ips', 0, 'ip_address']
                        }
                        output['description'] = '%s ip of %s in %s.' % (
                            network_type.capitalize(), unit.hostname,
                            network_instance.name)

                        outputs['mcn.endpoint.%s.%s.%s' %
                                (unit.hostname, network_instance.name,
                                 network_type)] = output

        template['outputs'] = outputs

        yaml.add_representer(
            unicode, lambda dumper, value: dumper.represent_scalar(
                u'tag:yaml.org,2002:str', value))
        yaml.add_representer(SysUtil.literal_unicode,
                             SysUtil.literal_unicode_representer)
        LOG.debug((template))
        #LOG.debug(yaml.dumps(template))
        #f = open('/net/u/mpa/tempalte_file.yaml', 'w')
        #f.write(yaml.dump(template, indent=2))
        return yaml.dump(template)
Пример #53
0
 def get_all(cls):
     db = DatabaseManager()
     return db.get_all(SecurityGroup)
Пример #54
0
 def delete_rule(cls, id):
     db = DatabaseManager()
     try:
         db.remove(db.get_by_id(Rule, id))
     except NotFoundException as e:
         raise e
Пример #55
0
    def get_template(topology):
        #name = topology.name
        template = {}
        template['heat_template_version'] = '2013-05-23'
        resources = {}
        outputs = {}
        #print "create Template for Topology: %s" % name

        for service_instance in topology.service_instances:
            for unit in service_instance.units:
                #Create Ports and floating IPs for this unit
                ports = []
                floating_ips = []
                if service_instance.networks:
                    i=1
                    for network in service_instance.networks:
                        ###Creating Port for this service instance
                        new_port = None
                        #prepare port args for this service instance
                        port_args = {}
                        port_args['name'] = '%s-port-%s' % (unit.hostname, i)
                        port_args['private_net_id'] = network.private_net
                        port_args['private_subnet_id'] = network.private_subnet
                        # if "hss-1" in unit.hostname:
                        #     port_args['fixed_ip'] = "192.168.9.48"
                        # elif "hss-2" in unit.hostname:
                        #     port_args['fixed_ip'] = "192.168.9.55"
                        # else:
                        port_args['fixed_ip'] = network.fixed_ip

                        if network.security_groups:
                            port_args['security_groups'] = network.security_groups
                        new_port = Port(**port_args)
                        ports.append(new_port)
                        if network.public_net:
                            new_floating_ip_args = {}
                            new_floating_ip_args['name'] = '%s-floating_ip-%s' % (unit.hostname, i)
                            new_floating_ip_args['floating_network_id'] = network.public_net
                            new_floating_ip_args['port'] = new_port.name
                            new_floating_ip = FloatingIP(**new_floating_ip_args)
                            floating_ips.append(new_floating_ip)
                        ###Adding Security Groups
                        for _security_group in network.security_groups:
                            _new_name=_security_group.name
                            _new_rules=[]
                            _rules=_security_group.rules
                            for _rule in _rules:
                                _name = _rule.name
                                _remote_ip_prefix = _rule.remote_ip_prefix
                                _protocol = _rule.protocol
                                _port_range_max = int(_rule.port_range_max) if _rule.port_range_max else None
                                _port_range_min = int(_rule.port_range_min) if _rule.port_range_min else None
                                _new_rule = Rule(_name, _remote_ip_prefix, _protocol, _port_range_max, _port_range_min)
                                _new_rules.append(_new_rule)
                            _new_security_group = SecurityGroup(name=_new_name, rules=_new_rules)
                            resources.update(_new_security_group.dump_to_dict())
                        i += 1

                ###Create Server for this service instance
                new_server = None
                #prepare server args
                server_args = {}
                server_args['name'] = "%s" % unit.hostname
                server_args['hostname'] = "%s" % unit.hostname
                server_args['availability_zone'] = unit.availability_zone
                server_args['flavor'] = service_instance.flavor.name
                # if "hss-1" in unit.hostname:
                #     server_args['image'] = "hss-1"
                # elif "hss-2" in unit.hostname:
                #     server_args['image'] = "hss-2"
                # else:
                server_args['image'] = service_instance.image.name
                server_args['key_name'] = service_instance.key.name
                server_args['network_ports'] = ports
                server_args['user_data'] = service_instance.user_data
                server_args['requirements'] = service_instance.requirements
                new_server = Server(**server_args)

                resources.update(new_server.dump_to_dict())

                if ports:
                    for port in ports:
                        resources.update(port.dump_to_dict())
                if floating_ips:
                    for floating_ip in floating_ips:
                        resources.update(floating_ip.dump_to_dict())

        template['resources'] = resources

        ###Output section###
        db = DatabaseManager()
        for service_instance in topology.service_instances:
            for network_instance in service_instance.networks:
                if network_instance.public_net:
                    _public_network_names = [_network.name for _network in db.get_all(Network) if
                                             _network.ext_id == network_instance.public_net]
                    _public_network_name = None
                    if _public_network_names:
                        _public_network_name = _public_network_names[0]
                    else:
                        LOG.debug('ERROR: Cannot find network with id %s in Table Network' % network_instance.public_net)
                if network_instance.private_net:
                    _private_network_names = [_network.name for _network in db.get_all(Network) if
                                              _network.ext_id == network_instance.private_net]
                    _private_network_name = None
                    if _private_network_names:
                        _private_network_name = _private_network_names[0]
                    else:
                        LOG.debug('ERROR: Cannot find network with id %s in Table Network' % network_instance.net)

                for unit in service_instance.units:
                    if network_instance.public_net and _public_network_name:
                        output = {}
                        floating_name = '%s-floating_ip-1' % unit.hostname
                        output['value'] = {'get_attr': [floating_name,
                                                        'floating_ip_address']}
                        output['description'] = 'Public IP of %s.' % unit.hostname
                        outputs['mcn.endpoint.%s' % unit.hostname] = output
                    elif network_instance.private_net and _private_network_name:
                        output = {}
                        port_name = '%s-port-1' % unit.hostname
                        output['value'] = {'get_attr': [port_name, 'fixed_ips',
                                                        0, 'ip_address']}
                        output['description'] = 'Private IP of %s.' % unit.hostname
                        outputs['mcn.endpoint.%s' % unit.hostname] = output

                    if network_instance.public_net and _public_network_name:
                        network_type = "public"
                        output = {}
                        floating_name = '%s-floating_ip-1' % unit.hostname
                        output['value'] = {'get_attr': [floating_name,
                                                        'floating_ip_address']}
                        output['description'] = '%s ip of %s in %s.' % (
                            network_type.capitalize(),
                            unit.hostname,
                            network_instance.name)

                        outputs['mcn.endpoint.%s.%s.%s' % (unit.hostname,
                                                           network_instance.name,
                                                           network_type)] = output

                    if network_instance.private_net and _private_network_name:
                        network_type = "private"
                        output = {}
                        port_name = '%s-port-1' % unit.hostname
                        output['value'] = {'get_attr': [port_name, 'fixed_ips',
                                                        0, 'ip_address']}
                        output['description'] = '%s ip of %s in %s.' % (
                            network_type.capitalize(),
                            unit.hostname,
                            network_instance.name)

                        outputs['mcn.endpoint.%s.%s.%s' % (unit.hostname,
                                                           network_instance.name,
                                                           network_type)] = output

        template['outputs'] = outputs

        yaml.add_representer(unicode, lambda dumper, value: dumper.represent_scalar(u'tag:yaml.org,2002:str', value))
        yaml.add_representer(SysUtil.literal_unicode, SysUtil.literal_unicode_representer)
        LOG.debug((template))
        #LOG.debug(yaml.dumps(template))
        #f = open('/net/u/mpa/tempalte_file.yaml', 'w')
        #f.write(yaml.dump(template, indent=2))
        return yaml.dump(template)
Пример #56
0
 def dump_to_dict(self):
     db = DatabaseManager()
     resource = {}
     server_config = {}
     server_config['type'] = self.type
     properties = {}
     properties['name'] = self.name
     properties['image'] = self.image
     properties['flavor'] = self.flavor
     if self.key_name is not None: properties['key_name'] = self.key_name
     if self.availability_zone is not None:
         properties['availability_zone'] = self.availability_zone
     if self.network_ports is not None:
         networks = []
         LOG.debug(self.network_ports)
         for network_port in self.network_ports:
             networks.append({'port': {'get_resource': network_port.name}})
         properties['networks'] = networks
     if self.user_data:
         properties['user_data_format'] = 'RAW'
         properties['user_data'] = {}
         properties['user_data']['str_replace'] = {}
         properties['user_data']['str_replace']['template'] = ''
         _user_data = ''
         _user_data_list = []
         for command in self.user_data:
             _user_data += "%s\n" % command.command
         properties['user_data']['str_replace'][
             'template'] = SysUtil.literal_unicode((_user_data))
         properties['user_data']['str_replace']['params'] = {'': ''}
         if self.requirements:
             params = {}
             for requirement in self.requirements:
                 try:
                     source_service_instances = db.get_by_name(
                         ServiceInstance, requirement.source)
                 except:
                     LOG.debug(
                         'ERROR: Entry %s was not found in Table ServiceInstance'
                         % requirement.source)
                     raise
                 source_units = []
                 if source_service_instances:
                     source_service_instance = source_service_instances[0]
                     source_units = source_service_instance.units
                     LOG.debug(source_units)
                     if source_units:
                         if requirement.parameter == 'private_ip' or requirement.parameter == 'public_ip':
                             #Get requested network specified in the requirement
                             _networks = [
                                 network for network in
                                 source_service_instance.networks
                                 if network.name == requirement.obj_name
                             ]
                             _network = None
                             if _networks:
                                 _network_id = _networks[0].private_net
                             else:
                                 LOG.debug(
                                     'ERROR: obj_name %s was not found in networks of ServiceInstance %s'
                                     % (requirement.obj_name,
                                        source_service_instance))
                                 raise
                             #Get network name of the specified network id
                             _network_names = [
                                 network.name
                                 for network in db.get_all(Network)
                                 if network.ext_id == _network_id
                             ]
                             _network_name = None
                             if _network_names:
                                 _network_name = _network_names[0]
                             else:
                                 LOG.debug(
                                     'ERROR: Cannot find network with id %s in Table Network'
                                     % _network_id)
                             if requirement.parameter == "private_ip":
                                 ip_number = 0
                             elif requirement.parameter == "public_ip":
                                 ip_number = 1
                             #Create the variable
                             _params = {}
                             _first_unit = source_units[0]
                             _template = '$%s' % _first_unit.hostname
                             _params['$%s' % _first_unit.hostname] = {
                                 'get_attr': [
                                     _first_unit.hostname, 'networks',
                                     _network_name, ip_number
                                 ]
                             }
                             for source_unit in source_units[1:]:
                                 _template += ';$%s' % source_unit.hostname
                                 _params['$%s' % source_unit.hostname] = {
                                     'get_attr': [
                                         source_unit.hostname, 'networks',
                                         _network_name, ip_number
                                     ]
                                 }
                         param = {}
                         param[requirement.name] = {}
                         param[requirement.name]['str_replace'] = {}
                         param[requirement.
                               name]['str_replace']['template'] = _template
                         param[requirement.
                               name]['str_replace']['params'] = _params
                         params.update(param)
                     else:
                         LOG.debug(
                             'ERROR: Units for ServiceInstance %s were not found.'
                             % requirement.source)
                         raise Exception
                 else:
                     LOG.debug('ERROR: ServiceInstance %s was not found' %
                               requirement.source)
                     raise Exception
             properties['user_data']['str_replace']['params'] = params
     server_config['properties'] = properties
     resource[self.name] = server_config
     return resource
Пример #57
0
class TopologyManager(ABCTopologyManager):
    def __init__(self):
        self.db = DatabaseManager()

    def create(self, config):
        ###Topology arguments
        top_name = config.get('name')
        top_state = 'DEFINED'
        top_service_instances = []
        LOG.debug("Parse Topology \"%s\"" % top_name)
        ###Parse all service instances described in config file
        sis_config = config.get('service_instances')
        for si_config in sis_config:
            si_args = {}
            si_args['state'] = 'DEFINED'
            service = None
            if si_config.get('service_type'):
                service_type = si_config.get('service_type')
                LOG.debug('Fetching service of service_type \"%s\"' %
                          service_type)
                service_list = self.db.get_by_service_type(
                    Service, service_type)
                if len(service_list) == 1:
                    service = service_list[0]
                    LOG.debug('Service \"%s\" is available.' % service)
                    si_args['service_type'] = service.service_type
                    si_args['image'] = service.image
                    si_args['flavor'] = service.flavor
                    si_args['configuration'] = service.configuration
                    si_args['key'] = service.key
                    si_args['size'] = service.size
                    si_args['adapter'] = service.adapter
                    si_args['requirements'] = []
                    for requirement in service.requirements:
                        func = Requirement.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = requirement.__dict__.get(param)
                        new_requirement = Requirement(**args)
                        si_args['requirements'].append(new_requirement)
                    si_args['networks'] = []
                    for network in service.networks:
                        func = Network_Instance.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = network.__dict__.get(param)
                        new_network = Network_Instance(**args)
                        si_args['networks'].append(new_network)
                    si_args['user_data'] = []
                    for command in service.user_data:
                        func = Command.__init__
                        needed_parameters = func.func_code.co_varnames[
                            1:func.func_code.co_argcount]
                        args = {}
                        for param in needed_parameters:
                            args[param] = command.__dict__.get(param)
                        new_command = Command(**args)
                        si_args['user_data'].append(new_command)
                else:
                    raise NotFoundException(
                        'service_type:\"%s\" is not available.' % service_type)
            else:
                raise NotDefinedException("service_type is not defined.")
            for si_item in si_config:
                if si_item == "name":
                    si_args['name'] = si_config.get(si_item)
                    LOG.debug("Parsing service instance \"%s\"" %
                              si_args['name'])
                elif si_item == "service_type":
                    si_args['service_type'] = si_config.get(si_item)
                elif si_item == "adapter":
                    si_args['adapter'] = si_config.get(si_item)
                elif si_item == "image":
                    image_name = si_config.get(si_item)
                    image_list = self.db.get_by_name(Image, image_name)
                    if len(image_list) == 1:
                        image = image_list[0]
                        si_args['image'] = image
                    else:
                        raise NotFoundException(
                            'image:\"%s\" is not available.' % image_name)
                elif si_item == "flavor":
                    flavor_name = si_config.get(si_item)
                    flavor_list = self.db.get_by_name(Flavor, flavor_name)
                    if len(flavor_list) == 1:
                        flavor = flavor_list[0]
                        si_args['flavor'] = flavor
                    else:
                        raise NotFoundException(
                            'flavor:\"%s\" is not available.' % flavor_name)
                elif si_item == "key":
                    key_name = si_config.get(si_item)
                    key_list = self.db.get_by_name(Key, key_name)
                    if len(key_list) == 1:
                        key = key_list[0]
                        si_args['key'] = key
                    else:
                        raise NotFoundException(
                            'key:\"%s\" is not available.' % key_name)
                elif si_item == "size":
                    si_args['size'].update(si_config.get(si_item))
                elif si_item == "networks":
                    networks = []
                    _networks = si_config.get(si_item)
                    LOG.debug("Fetch SecurityGroups for networks %s." %
                              _networks)
                    for _net_inst in _networks:
                        secgroups = []
                        for _secgroup_name in _net_inst.get('security_groups'):
                            lst = self.db.get_by_name(SecurityGroup,
                                                      _secgroup_name)
                            if len(lst) > 0:
                                _secgroup = lst[0]
                            else:
                                raise NotFoundException(
                                    'SecurityGroup:\"%s\" is not available.' %
                                    _secgroup_name)
                            secgroups.append(_secgroup)
                        _net_inst['security_groups'] = secgroups
                        networks.append(Network_Instance(**_net_inst))
                    si_args['networks'] = networks
                elif si_item == "configuration":
                    for key in si_config.get(si_item).keys():
                        if key in service.configuration.keys():
                            si_args['configuration'][key] = si_config.get(
                                si_item).get(key)
                elif si_item == "policies":
                    policies = []
                    _policies = si_config.get(si_item)
                    for _policy in _policies:
                        _new_policy_args = {}
                        for _po_item in _policy:
                            if _po_item == "name":
                                _new_policy_args.update(
                                    {'name': _policy.get(_po_item)})
                            elif _po_item == "period":
                                _new_policy_args.update(
                                    {'period': _policy.get(_po_item)})
                            elif _po_item == "alarm":
                                _new_alarm_args = _policy.get(_po_item)
                                _new_alarm = Alarm(**_new_alarm_args)
                                _new_policy_args.update({'alarm': _new_alarm})
                            elif _po_item == "action":
                                _new_action_args = _policy.get(_po_item)
                                _new_action = Action(**_new_action_args)
                                _new_policy_args.update(
                                    {'action': _new_action})
                        try:
                            _new_policy = Policy(**_new_policy_args)
                        except TypeError:
                            raise InvalidInputException()
                        policies.append(_new_policy)
                    si_args['policies'] = policies
                elif si_item == "requirements":
                    requirements = []
                    for _req in si_config.get(si_item):
                        _req_args = _req
                        requirement = Requirement(**_req_args)
                        requirements.append(requirement)
                    si_args['requirements'] = requirements
                elif si_item == "user_data":
                    user_data = []
                    for _user_data_item in si_config.get(si_item):
                        command = Command(_user_data_item)
                        user_data.append(command)
                    si_args['user_data'] = user_data
                elif si_item == "location":
                    location = []
                    for _loc in si_config.get(si_item):
                        LOG.debug("found item location %s" % _loc)
                        loc = Location(_loc)
                        location.append(loc)
                    si_args['location'] = location
                elif si_item == "relation":
                    relation = []
                    for _rel in si_config.get(si_item):
                        LOG.debug("found item relation %s" % _rel)
                        rel = Relation(_rel)
                        relation.append(rel)
                    si_args['relation'] = relation
                else:
                    raise InvalidInputException(
                        "parameter \"%s\" is not provided by Services." %
                        si_config.get(si_item))

            ###Initialise Units
            units = []
            unit_number = si_args.get('size').get('def') or 1
            for i in range(1, unit_number + 1):
                if i == 1:
                    _hostname = '%s' % si_args.get('name')
                else:
                    _hostname = '%s-%s' % (si_args.get('name'), i)
                _new_unit = Unit(hostname=_hostname, state='DEFINED')
                units.append(_new_unit)
            si_args['units'] = units
            ###Initialise Service Instance
            new_service_instance = ServiceInstance(**si_args)
            ###Add the new service instance to the topology
            top_service_instances.append(new_service_instance)

        ###Initialise Topology
        ext_name = '' + top_name + '_' + str(random.randint(1000, 9999))
        topology = Topology(name=top_name,
                            state=top_state,
                            service_instances=top_service_instances,
                            ext_name=ext_name)
        LOG.debug(topology)
        return topology

    def update(self, new_topology, old_topology):
        conf = sys_util().get_sys_conf()
        db = FactoryAgent().get_agent(conf['database_manager'])
        updated_topology = old_topology
        updated_topology.name = new_topology.name
        #check for additional service instances and add them to the list of new instances
        appended_service_instances = []
        for new_service_instance in new_topology.service_instances:
            is_found = False
            for updated_service_instance in updated_topology.service_instances:
                if new_service_instance.name == updated_service_instance.name:
                    is_found = True
                    break
            if not is_found:
                appended_service_instances.append(new_service_instance)
        #check for removed service instances and add it to the list of removed instances
        removed_service_instances = []
        for updated_service_instance in updated_topology.service_instances:
            is_found = False
            for new_service_instance in new_topology.service_instances:
                if new_service_instance.name == updated_service_instance.name:
                    is_found = True
                    break
            if not is_found:
                removed_service_instances.append(updated_service_instance)
        #remove removed service instances
        for removed_service_instance in removed_service_instances:
            updated_topology.service_instances.remove(removed_service_instance)
            LOG.debug('Removed ServiceInstance \"%s\" from Topology \"%s\".' %
                      (removed_service_instance.name, updated_topology.name))
        #append additional service instances
        for appended_service_instance in appended_service_instances:
            appended_service_instance.topology_id = updated_topology.id
            updated_topology.service_instances.append(
                appended_service_instance)
            if appended_service_instance.policies is not None:
                for policy in appended_service_instance.policies:
                    db.persist(policy)
            db.persist(appended_service_instance)
            LOG.debug('Appended ServiceInstance \"%s\" to Topology \"%s\".' %
                      (appended_service_instance.name, updated_topology.name))
        #Update all values for each service instance
        for updated_service_instance in updated_topology.service_instances:
            for new_service_instance in new_topology.service_instances:
                if updated_service_instance.name == new_service_instance.name:
                    updated_service_instance.size = new_service_instance.size
                    updated_service_instance.configuration = new_service_instance.configuration
                    updated_service_instance.policies = new_service_instance.policies
                    #updated_service_instance.service_type = new_service_instance.service_type
                    if new_service_instance.service_type and updated_service_instance.service_type != new_service_instance.service_type:
                        LOG.warning(
                            "Cannot update service_type for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    if new_service_instance.adapter and updated_service_instance.adapter != new_service_instance.adapter:
                        LOG.warning(
                            "Cannot update adapter for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    #updated_service_instance.flavor = new_service_instance.flavor
                    if new_service_instance.flavor and updated_service_instance.flavor.name != new_service_instance.flavor.name:
                        LOG.warning(
                            "Cannot update flavor for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    #updated_service_instance.image = new_service_instance.image
                    if new_service_instance.image and updated_service_instance.image.name != new_service_instance.image.name:
                        LOG.warning(
                            "Cannot update image for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    #updated_service_instance.networks = new_service_instance.networks
                    if new_service_instance.networks is not None:
                        LOG.warning(
                            "Cannot update networks for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    #updated_service_instance.requirements = new_service_instance.requirements
                    if new_service_instance.requirements is not None:
                        LOG.warning(
                            "Cannot update networks for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    #updated_service_instance.user_data = new_service_instance.user_data
                    if new_service_instance.user_data is not None:
                        LOG.warning(
                            "Cannot update user_data for %s->%s. Not Implemented."
                            % (updated_topology.name,
                               updated_service_instance.name))
                    if new_service_instance.key and updated_service_instance.key.name != new_service_instance.key.name:
                        LOG.warning(
                            "Cannot update key for %s->%s without replacement."
                            % (updated_topology.name,
                               updated_service_instance.name))
        #Add or remove units according to minimal or maximal size
        for updated_service_instance in updated_topology.service_instances:
            if updated_service_instance not in appended_service_instances:
                if len(updated_service_instance.units
                       ) < updated_service_instance.size.get('min'):
                    for i in range(
                            updated_service_instance.size.get('min') -
                            len(updated_service_instance.units)):
                        _hostname = '%s-%s' % (
                            updated_service_instance.name,
                            str(len(updated_service_instance.units) + 1))
                        _state = 'DEFINED'
                        new_unit = Unit(hostname=_hostname, state=_state)
                        new_unit.service_instance_id = updated_service_instance.id
                        updated_service_instance.units.append(new_unit)
                        db.persist(new_unit)
                if len(updated_service_instance.units
                       ) > updated_service_instance.size.get('max'):
                    for i in range(
                            len(updated_service_instance.units) -
                            updated_service_instance.size.get('max')):
                        removed_unit = updated_service_instance.units.pop(
                            len(updated_service_instance.units) - 1)
                        db.remove(removed_unit)
        return updated_topology

    def dynamic_create(self, dict):
        LOG.debug(dict)
        t = Topology(**dict)
        LOG.debug(t.__dict__)
        return t

    def get_attrs(self, _class):
        return [k for k in dir(_class) if not k.startswith('__')]
 def dump_to_dict(self):
     db = DatabaseManager()
     resource = {}
     server_config = {}
     server_config["type"] = self.type
     properties = {}
     properties["name"] = self.name
     properties["image"] = self.image
     properties["flavor"] = self.flavor
     if self.key_name is not None:
         properties["key_name"] = self.key_name
     if self.availability_zone is not None:
         properties["availability_zone"] = self.availability_zone
     if self.network_ports is not None:
         networks = []
         logger.debug(self.network_ports)
         for network_port in self.network_ports:
             networks.append({"port": {"get_resource": network_port.name}})
         properties["networks"] = networks
     if self.user_data:
         properties["user_data_format"] = "RAW"
         properties["user_data"] = {}
         properties["user_data"]["str_replace"] = {}
         properties["user_data"]["str_replace"]["template"] = ""
         _user_data = ""
         _user_data_list = []
         for command in self.user_data:
             _user_data += "%s\n" % command.command
         properties["user_data"]["str_replace"]["template"] = SysUtil.literal_unicode((_user_data))
         properties["user_data"]["str_replace"]["params"] = {"": ""}
         if self.requirements:
             params = {}
             for requirement in self.requirements:
                 try:
                     source_service_instances = db.get_by_name(ServiceInstance, requirement.source)
                 except:
                     logger.debug("ERROR: Entry %s was not found in Table ServiceInstance" % requirement.source)
                     raise
                 source_units = []
                 if source_service_instances:
                     source_service_instance = source_service_instances[0]
                     source_units = source_service_instance.units
                     logger.debug(source_units)
                     if source_units:
                         if requirement.parameter == "private_ip" or requirement.parameter == "public_ip":
                             # Get requested network specified in the requirement
                             _networks = [
                                 network
                                 for network in source_service_instance.networks
                                 if network.name == requirement.obj_name
                             ]
                             _network = None
                             if _networks:
                                 _network_id = _networks[0].private_net
                             else:
                                 logger.debug(
                                     "ERROR: obj_name %s was not found in networks of ServiceInstance %s"
                                     % (requirement.obj_name, source_service_instance)
                                 )
                                 raise
                             # Get network name of the specified network id
                             _network_names = [
                                 network.name for network in db.get_all(Network) if network.ext_id == _network_id
                             ]
                             _network_name = None
                             if _network_names:
                                 _network_name = _network_names[0]
                             else:
                                 logger.debug("ERROR: Cannot find network with id %s in Table Network" % _network_id)
                             if requirement.parameter == "private_ip":
                                 ip_number = 0
                             elif requirement.parameter == "public_ip":
                                 ip_number = 1
                             # Create the variable
                             _params = {}
                             _first_unit = source_units[0]
                             _template = "$%s" % _first_unit.hostname
                             _params["$%s" % _first_unit.hostname] = {
                                 "get_attr": [_first_unit.hostname, "networks", _network_name, ip_number]
                             }
                             for source_unit in source_units[1:]:
                                 _template += ";$%s" % source_unit.hostname
                                 _params["$%s" % source_unit.hostname] = {
                                     "get_attr": [source_unit.hostname, "networks", _network_name, ip_number]
                                 }
                         param = {}
                         param[requirement.name] = {}
                         param[requirement.name]["str_replace"] = {}
                         param[requirement.name]["str_replace"]["template"] = _template
                         param[requirement.name]["str_replace"]["params"] = _params
                         params.update(param)
                     else:
                         logger.debug("ERROR: Units for ServiceInstance %s were not found." % requirement.source)
                         raise Exception
                 else:
                     logger.debug("ERROR: ServiceInstance %s was not found" % requirement.source)
                     raise Exception
             properties["user_data"]["str_replace"]["params"] = params
     server_config["properties"] = properties
     resource[self.name] = server_config
     return resource