Exemplo n.º 1
0
 def get_instance(cls):
     if cls._instance is None:
         cls._instance = cls(get_extensions_path(),
                             CrdManager.get_service_plugins())
     return cls._instance
Exemplo n.º 2
0
    def launch_chain(self, context, chainset_id, chainmap_id, rule_id):
        self.db = sfc_db.SFCPluginDb()
        self.delta_db = sfc_delta_db.SfcDeltaDb()
        self.crdnovadb = nova_db.NovaDb()
        self.plugin = q_man.get_plugin()
        nt = novaclient(context)
        qt = crdclient(context)
        rule_details = self.db.get_chainset_rule(context, rule_id,
                                                   chainset_id)
        chain_id = rule_details['chain_id']

        chainmap_details = self.db.get_chainmap(context, chainmap_id)
        inbound_network = chainmap_details['inbound_network_id']
        outbound_network = chainmap_details['outbound_network_id']
        chainset_details = self.db.get_chainset(context, chainset_id)
        inbound_net_id = inbound_network
        outbound_net_id = outbound_network

        #LOG.debug(
        # '*****************************************************************************\n')
        #LOG.debug('Chain ID = %s' % str(chain_id))
        #LOG.debug('Inbound Network = %s' % str(inbound_net_id))
        #LOG.debug('Outbound Network = %s' % str(outbound_net_id))
        #LOG.debug(
        # '*****************************************************************************\n')


        filters = {}
        filters['chain_id'] = [chain_id]
        appliance_maps = self.db.get_chain_appliance_maps(context, filters)
        appliance_count = len(appliance_maps)
        appliance_maps = sorted(appliance_maps,
                                key=lambda k: k['sequence_number'])
        #LOG.debug(
        # '#############################################################################\n')
        #LOG.debug('appliance count = %s' % str(appliance_count))
        #LOG.debug(
        # '#############################################################################\n')
        #inner_subnets = self.subnet_list(given_subnet, appliance_count-1)
        #LOG.debug(
        # '#############################################################################\n')
        #LOG.debug('inner subnets = %s' % str(inner_subnets))
        #LOG.debug(
        # '#############################################################################\n')
        last_net = None
        count = 1
        for appliance_map in appliance_maps:
            name = appliance_map['name']
            network_name = 'internal_net_' + name
            appliance_map_id = appliance_map['id']
            chain_id = appliance_map['chain_id']
            appliance_id = appliance_map['appliance_id']
            appliance = self.db.get_appliance(context,
                                              appliance_id)
            glance_image_id = appliance['image_id']
            flavor_id = appliance['flavor_id']
            security_group_id = appliance['security_group_id']

            security_groups = []
            secgrp = nt.security_groups.get(appliance['security_group_id'])
            security_group_name = secgrp.__getattribute__('name')
            security_groups.append(security_group_name)

            nics = []
            if inbound_net_id == outbound_net_id:
                nic = {"net-id": inbound_net_id, "v4-fixed-ip": ""}
                nics.append(nic)
            else:
                nic1 = {"net-id": inbound_net_id, "v4-fixed-ip": ""}
                nics.append(nic1)
                
                nic2 = {"net-id": outbound_net_id, "v4-fixed-ip": ""}
                nics.append(nic2)
            
            config_handle_id = appliance['config_handle_id']
            if (config_handle_id == ''):
                msg = _(
                    'Failed to launch chain  %s, No Cnfiguration '
                    'associated') % chain_id
                LOG.error(msg)
                

            LOG.debug(
                '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n')
            LOG.debug('NICS = %s' % str(nics))
            LOG.debug('Name = %s' % str(name))
            LOG.debug('Galnce Image ID = %s' % str(glance_image_id))
            LOG.debug('Security Groups = %s' % str(security_groups))
            LOG.debug(
                '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n')
            dev_mapping = None
            custom_script = None
            keypair_id = None
            meta = {'vmtype': 'service'}
            instance_count = 1

            instance = nt.servers.create(name,
                                         glance_image_id,
                                         flavor_id,
                                         meta=meta,
                                         security_groups=security_groups,
                                         nics=nics,
                                         instance_count=int(instance_count))
            LOG.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
            LOG.debug('INSTANCE = %s' % str(instance))
            LOG.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
            msg = _('Instance %s was successfully launched.') % instance.id
            LOG.debug(msg)
            instance_uuid = instance.id
            
            while (1):
                msg = _("Waiting for Instance in CRD DB ...%s") % str(
                    instance_uuid)
                LOG.debug("############################################")
                LOG.debug(msg)
                LOG.debug("############################################")
                try:
                    crd_instance = self.crdnovadb.get_instance(context,
                                                               instance_uuid)
                    if crd_instance['state'] == 'active' or crd_instance[
                        'state'] == 'error':
                        break
                except:
                    pass
                time.sleep(5)
            
            appliance_instance_body = {
                'instance': {
                    'appliance_map_id': appliance_map_id,
                    'instance_uuid': instance_uuid,
                    'tenant_id': context.tenant_id,
                    'network_id': None,
                    'vlan_in': None,
                    'vlan_out': None
                }
            }
            appliance_instance = self.db.create_chain_appliance_map_instance(context, appliance_instance_body)
            ###DELTA
            data = appliance_instance
            data.update({'operation' : 'create'})
            delta={}
            delta.update({'appliance_instances_delta':data})
            result_delta = self.delta_db.create_appliance_instances_delta(context,delta)
            ###DELTA
            
            appliance_instance_id = appliance_instance['id']
            
            if instance_uuid:
                fliters = []
                filters['tenant_id'] = [context.tenant_id]
                #vlanquotas = cfg.CONF.nscs_authtoken.vlan_quota
                vlanquotas = '100-200'
                #vlanquotas = self.db.get_vlanquotas(context, filters=filters)
                if vlanquotas:
                    vlan_range = vlanquotas.split('-')
                    vlan_start = int(vlan_range[0])
                    vlan_end = int(vlan_range[1])
                    tot_vlans = []
                    u = 0
                    for u in range(vlan_start, vlan_end):
                        tot_vlans.append(u)

                    for n in nics:
                        net_id = n['net-id']
                        fliters = []
                        filters['tenant_id'] = [context.tenant_id]
                        filters['network_id'] = [net_id]
                        vlan_pairs = self.db.get_chain_appliance_map_instances(context,
                                                                     filters=filters)
                        assigned_vlans = []
                        for vp in vlan_pairs:
                            vlan_in = vp['vlan_in']
                            assigned_vlans.append(vlan_in)
                            vlan_out = vp['vlan_out']
                            assigned_vlans.append(vlan_out)
                        diff_vlans = sorted(
                            list(set(tot_vlans) - set(assigned_vlans)))
                        y = 0
                        vlan_in = 0
                        vlan_out = 0
                        for y in range(0, 2):
                            if y == 0:
                                #dir = 'ingress'
                                vlan_in = diff_vlans[0]

                            else:
                                #dir = 'egress'
                                vlan_out = diff_vlans[1]

                            #vpair.append(vlid)

                            if y > 0:
                                ###Insert Vlan Pairs in CRD...
                                appliance_instance_update_body = {
                                    'appliance_instance': {'network_id': net_id,
                                                           'vlan_in': vlan_in,
                                                           'vlan_out': vlan_out,
                                                           }
                                        }
                                #vlanpair = self.db.create_vlan_pair(context,
                                #                                    vlanpair_body)
                                v_new = self.db.update_chain_appliance_map_instance(context, appliance_instance_id,
                                                                          appliance_instance_update_body)
                                ###DELTA
                                data = {}
                                data = v_new
                                data.update({'operation' : 'update'})
                                delta={}
                                delta.update({'appliance_instances_delta':data})
                                result_delta = self.delta_db.create_appliance_instances_delta(context,delta)
                                ###DELTA
                                

                                ###Send Vlan Pairs to Relay Agent

                                #res = {'header': 'request',
                                #       'instance_uuid': instance_uuid,
                                #       'slug': 'vlanpair',
                                #       'vlanin': vlan_in,
                                #       'vlanout': vlan_out,
                                #       'version': '1.0',
                                #       'tenant_id': context.tenant_id}
                                #LOG.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                                #LOG.debug('VLAN Pair Message = %s' % str(res))
                                #LOG.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
                                #self.send_vlancast(instance_uuid,
                                #                   {'config': res})

                            y += 1
            
            #config_handle_details = self.db.get_config_handle(context,
            #                                                  config_handle_id)
            #res = {'header': 'request',
            #       'config_handle_id': config_handle_id,
            #       'slug': config_handle_details['slug'],
            #       'version': '1.0',
            #       'tenant_id': config_handle_details['tenant_id']}
            #if config_handle_details['config_mode'] == 'NFV':
            #    str1 = self.send_cast(config_handle_id, {'config':res})
            #    pass

            count += 1
            #time.sleep(120)
        #LOG.debug("Returning ChainID - %s" % str(chain_id))    
        return chain_id