def create_service(self, st, si):
        if not self.validate_network_config(st, si):
            return

        # get current vm list
        vm_list = [None] * si.max_instances
        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            vm_list[vm.index] = vm

        # create and launch vm
        si.state = 'launching'
        instances = []
        for index in range(0, si.max_instances):
            vm = self._check_create_netns_vm(index, si, st, vm_list[index])
            if not vm:
                continue

            vr_name = self._associate_vrouter(si, vm)
            if not vr_name:
                self.logger.log_error("No vrouter available for VM %s" %
                                      vm.name)

            if si.local_preference[index] == svc_info.get_standby_preference():
                ha = ("standby: %s" % (si.local_preference[index]))
            else:
                ha = ("active: %s" % (si.local_preference[index]))
            instances.append({'uuid': vm.uuid, 'vr_name': vr_name, 'ha': ha})

        # uve trace
        si.state = 'active'
        self.logger.uve_svc_instance((':').join(si.fq_name),
            status='CREATE', vms=instances,
            st_name=(':').join(st.fq_name))
Exemplo n.º 2
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.service_template = None
     self.loadbalancer = None
     self.loadbalancer_pool = None
     self.interface_route_tables = set()
     self.service_health_checks = set()
     self.instance_ips = set()
     self.virtual_machines = set()
     self.logical_router = None
     self.params = None
     self.bindings = None
     self.kvps = None
     self.state = 'init'
     self.launch_count = 0
     self.back_off = -1
     self.image = None
     self.flavor = None
     self.max_instances = 0
     self.availability_zone = None
     self.ha_mode = None
     self.vr_id = None
     self.vn_changed = False
     self.local_preference = [None, None]
     self.vn_info = []
     self.port_tuples = set()
     obj_dict = self.update(obj_dict)
     self.set_children('port_tuple', obj_dict)
     self.add_to_parent(obj_dict)
     if self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [svc_info.get_active_preference(),
                                  svc_info.get_standby_preference()]
Exemplo n.º 3
0
    def create_service(self, st, si):
        if not self.validate_network_config(st, si):
            return

        # get current vm list
        vm_list = [None for i in range(0, si.max_instances)]
        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            index = int(vm.display_name.split('__')[-2]) - 1
            vm_list[index] = vm

        # create and launch vm
        si.state = 'launching'
        instances = []
        for index in range(0, si.max_instances):
            vm = self._check_create_netns_vm(index, si, st, vm_list[index])
            if not vm:
                continue

            vr_name = self._associate_vrouter(si, vm)

            if si.local_preference[index] == svc_info.get_standby_preference():
                ha = ("standby: %s" % (si.local_preference[index]))
            else:
                ha = ("active: %s" % (si.local_preference[index]))
            instances.append({'uuid': vm.uuid, 'vr_name': vr_name, 'ha': ha})

        # uve trace
        si.state = 'active'
        self.logger.uve_svc_instance((':').join(si.fq_name),
                                     status='CREATE',
                                     vms=instances,
                                     st_name=(':').join(st.fq_name))
    def _update_local_preference(self, si, del_vm):
        if si.ha_mode != 'active-standby':
            return

        if si.local_preference[del_vm.index] == \
                svc_info.get_standby_preference():
            return

        si.local_preference[del_vm.index] = svc_info.get_standby_preference()
        other_index = si.max_instances - del_vm.index - 1
        si.local_preference[other_index] = svc_info.get_active_preference()

        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            if vm:
                self._check_create_netns_vm(vm.index, si, st, vm)
Exemplo n.º 5
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.service_template = None
     self.loadbalancer = None
     self.loadbalancer_pool = None
     self.interface_route_tables = set()
     self.service_health_checks = set()
     self.instance_ips = set()
     self.virtual_machines = set()
     self.logical_router = None
     self.params = None
     self.bindings = None
     self.kvps = None
     self.state = 'init'
     self.launch_count = 0
     self.back_off = -1
     self.image = None
     self.flavor = None
     self.max_instances = 0
     self.availability_zone = None
     self.ha_mode = None
     self.vr_id = None
     self.vn_changed = False
     self.local_preference = [None, None]
     self.vn_info = []
     self.port_tuples = set()
     obj_dict = self.update(obj_dict)
     self.set_children('port_tuple', obj_dict)
     self.add_to_parent(obj_dict)
     if self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [
             svc_info.get_active_preference(),
             svc_info.get_standby_preference()
         ]
Exemplo n.º 6
0
    def _update_local_preference(self, si, del_vm):
        if si.ha_mode != 'active-standby':
            return

        if si.local_preference[del_vm.index] == \
                svc_info.get_standby_preference():
            return

        si.local_preference[del_vm.index] = svc_info.get_standby_preference()
        other_index = si.max_instances - del_vm.index - 1
        si.local_preference[other_index] = svc_info.get_active_preference()

        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            if vm:
                self._check_create_netns_vm(vm.index, si, st, vm)
Exemplo n.º 7
0
    def _get_local_prefs(self, si_obj, max_instances):
        local_prefs = [None, None]

        si_db_entry = self.db.service_instance_get(si_obj.get_fq_name_str())
        if si_db_entry:
            for inst_count in range(0, max_instances):
                column = self.db.get_vm_db_prefix(inst_count) + 'preference'
                if column in si_db_entry:
                    local_prefs[inst_count] = int(si_db_entry[column])

        if not local_prefs[0] and not local_prefs[1]:
            local_prefs[0] = svc_info.get_active_preference()
            local_prefs[1] = svc_info.get_standby_preference()
        elif local_prefs[0] == svc_info.get_active_preference():
            local_prefs[1] = svc_info.get_standby_preference()
        elif local_prefs[0] == svc_info.get_standby_preference():
            local_prefs[1] = svc_info.get_active_preference()

        return local_prefs
    def _get_local_prefs(self, si_obj, max_instances):
        local_prefs = [None, None]

        si_db_entry = self.db.service_instance_get(si_obj.get_fq_name_str())
        if si_db_entry:
            for inst_count in range(0, max_instances):
                column = self.db.get_vm_db_prefix(inst_count) + 'preference'
                if column in si_db_entry:
                    local_prefs[inst_count] = int(si_db_entry[column])

        if not local_prefs[0] and not local_prefs[1]:
            local_prefs[0] = svc_info.get_active_preference()
            local_prefs[1] = svc_info.get_standby_preference()
        elif local_prefs[0] == svc_info.get_active_preference():
            local_prefs[1] = svc_info.get_standby_preference()
        elif local_prefs[0] == svc_info.get_standby_preference():
            local_prefs[1] = svc_info.get_active_preference()

        return local_prefs
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        for si in ServiceInstanceSM.values():
            if req.si_name and req.si_name != si.name:
                continue

            st = ServiceTemplateSM.get(si.service_template)
            sandesh_si = sandesh.ServiceInstance(
                name=(':').join(si.fq_name),
                si_type=st.virtualization_type,
                si_state=si.state)

            sandesh_vm_list = []
            for vm_id in si.virtual_machines:
                vm = VirtualMachineSM.get(vm_id)
                if not vm:
                    continue
                vm_str = ("%s: %s" % (vm.name, vm.uuid))

                vr_name = 'None'
                vr = VirtualRouterSM.get(vm.virtual_router)
                if vr:
                    vr_name = vr.name

                ha_str = "active"
                if vm.index < len(si.local_preference):
                    if vm.index >= 0:
                        ha = si.local_preference[vm.index]
                        if ha and int(ha) == svc_info.get_standby_preference():
                            ha_str = "standby"
                        if ha:
                            ha_str = ha_str + ': ' + str(ha)
                    else:
                        ha_str = "unknown"

                vm = sandesh.ServiceInstanceVM(name=vm_str,
                                               vr_name=vr_name,
                                               ha=ha_str)
                sandesh_vm_list.append(vm)
            sandesh_si.vm_list = list(sandesh_vm_list)

            for nic in si.vn_info:
                vn = VirtualNetworkSM.get(nic['net-id'])
                if not vn:
                    continue
                if nic['type'] == svc_info.get_left_if_str():
                    sandesh_si.left_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_right_if_str():
                    sandesh_si.right_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_management_if_str():
                    sandesh_si.management_vn = [vn.name, vn.uuid]

            si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
Exemplo n.º 10
0
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        for si in ServiceInstanceSM.values():
            if req.si_name and req.si_name != si.name:
                continue

            st = ServiceTemplateSM.get(si.service_template)
            sandesh_si = sandesh.ServiceInstance(
                name=(':').join(si.fq_name), si_type=st.virtualization_type,
                si_state=si.state)

            sandesh_vm_list = []
            for vm_id in si.virtual_machines:
                vm = VirtualMachineSM.get(vm_id)
                if not vm:
                    continue
                vm_str = ("%s: %s" % (vm.name, vm.uuid))

                vr_name = 'None'
                vr = VirtualRouterSM.get(vm.virtual_router)
                if vr:
                    vr_name = vr.name

                ha_str = "active"
                if vm.index < len(si.local_preference):
                    if vm.index >= 0:
                        ha = si.local_preference[vm.index]
                        if ha and int(ha) == svc_info.get_standby_preference():
                            ha_str = "standby"
                        if ha:
                            ha_str = ha_str + ': ' + str(ha)
                    else:
                        ha_str = "unknown"

                vm = sandesh.ServiceInstanceVM(
                        name=vm_str, vr_name=vr_name, ha=ha_str)
                sandesh_vm_list.append(vm)
            sandesh_si.vm_list = list(sandesh_vm_list)

            for nic in si.vn_info:
                vn = VirtualNetworkSM.get(nic['net-id'])
                if not vn:
                    continue
                if nic['type'] == svc_info.get_left_if_str():
                    sandesh_si.left_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_right_if_str():
                    sandesh_si.right_vn = [vn.name, vn.uuid]
                if nic['type'] == svc_info.get_management_if_str():
                    sandesh_si.management_vn = [vn.name, vn.uuid]

            si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
Exemplo n.º 11
0
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        if req.si_name is None:
            si_list = self._db.service_instance_list()

            for si_fq_name_str, si in si_list or []:
                sandesh_si = sandesh.ServiceInstance(
                    name=si_fq_name_str,
                    si_type=si.get('instance_type', ''),
                    si_state=si.get('state', ''))

                sandesh_vm_list = []
                for idx in range(0, int(si.get('max-instances', '0'))):
                    prefix = self._db.get_vm_db_prefix(idx)
                    if not (prefix + 'name') in si.keys():
                        continue
                    vm_name = si.get(prefix + 'name', '')
                    vm_uuid = si.get(prefix + 'uuid', '')
                    vm_str = ("%s: %s" % (vm_name, vm_uuid))
                    vr_name = si.get(prefix + 'vrouter', '')
                    ha = si.get(prefix + 'preference', '')
                    if int(ha) == svc_info.get_standby_preference():
                        ha_str = ("standby: %s" % (ha))
                    else:
                        ha_str = ("active: %s" % (ha))
                    vm = sandesh.ServiceInstanceVM(name=vm_str,
                                                   vr_name=vr_name,
                                                   ha=ha_str)
                    sandesh_vm_list.append(vm)
                sandesh_si.vm_list = list(sandesh_vm_list)

                for itf_type in svc_info.get_if_str_list():
                    key = itf_type + '-vn'
                    if key not in si.keys():
                        continue
                    vn_name = si[key]
                    vn_uuid = si[vn_name]
                    if itf_type == svc_info.get_left_if_str():
                        sandesh_si.left_vn = [vn_name, vn_uuid]
                    if itf_type == svc_info.get_right_if_str():
                        sandesh_si.right_vn = [vn_name, vn_uuid]
                    if itf_type == svc_info.get_management_if_str():
                        sandesh_si.management_vn = [vn_name, vn_uuid]

                si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
Exemplo n.º 12
0
    def sandesh_si_handle_request(self, req):
        si_resp = sandesh.ServiceInstanceListResp(si_names=[])
        if req.si_name is None:
            si_list = self._db.service_instance_list()

            for si_fq_name_str, si in si_list or []:
                sandesh_si = sandesh.ServiceInstance(
                    name=si_fq_name_str, si_type=si.get('instance_type', ''),
                    si_state=si.get('state', ''))

                sandesh_vm_list = []
                for idx in range(0, int(si.get('max-instances', '0'))):
                    prefix = self._db.get_vm_db_prefix(idx)
                    if not (prefix + 'name') in si.keys():
                        continue
                    vm_name = si.get(prefix + 'name', '')
                    vm_uuid = si.get(prefix + 'uuid', '')
                    vm_str = ("%s: %s" % (vm_name, vm_uuid))
                    vr_name = si.get(prefix + 'vrouter', '')
                    ha = si.get(prefix + 'preference', '')
                    if int(ha) == svc_info.get_standby_preference():
                        ha_str = ("standby: %s" % (ha))
                    else:
                        ha_str = ("active: %s" % (ha))
                    vm = sandesh.ServiceInstanceVM(name=vm_str,
                        vr_name=vr_name, ha=ha_str)
                    sandesh_vm_list.append(vm)
                sandesh_si.vm_list = list(sandesh_vm_list)

                for itf_type in svc_info.get_if_str_list():
                    key = itf_type + '-vn'
                    if key not in si.keys():
                        continue
                    vn_name = si[key]
                    vn_uuid = si[vn_name]
                    if itf_type == svc_info.get_left_if_str():
                        sandesh_si.left_vn = [vn_name, vn_uuid]
                    if itf_type == svc_info.get_right_if_str():
                        sandesh_si.right_vn = [vn_name, vn_uuid]
                    if itf_type == svc_info.get_management_if_str():
                        sandesh_si.management_vn = [vn_name, vn_uuid]

                si_resp.si_names.append(sandesh_si)

        si_resp.response(req.context())
    def create_service(self, st, si):
        if not self.validate_network_config(st, si):
            return

        # get current vm list
        vm_list = [None] * si.max_instances
        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            vm_list[vm.index] = vm

        # if vn has changed then delete VMs
        if si.vn_changed:
            for vm in vm_list:
                if vm:
                    self.delete_service(vm)
            vm_list = [None] * si.max_instances
            si.vn_changed = False

        # create and launch vm
        si.state = 'launching'
        instances = []
        for index in range(0, si.max_instances):
            vm = self._check_create_netns_vm(index, si, st, vm_list[index])
            if not vm:
                continue

            vr_name = self._associate_vrouter(si, vm)
            if not vr_name:
                self.logger.log_error("No vrouter available for VM %s" %
                                      vm.name)

            if si.local_preference[index] == svc_info.get_standby_preference():
                ha = ("standby: %s" % (si.local_preference[index]))
            else:
                ha = ("active: %s" % (si.local_preference[index]))
            instances.append({'uuid': vm.uuid, 'vr_name': vr_name, 'ha': ha})

        # uve trace
        si.state = 'active'
        self.logger.uve_svc_instance((':').join(si.fq_name),
                                     status='CREATE',
                                     vms=instances,
                                     st_name=(':').join(st.fq_name))
Exemplo n.º 14
0
 def update(self, obj=None):
     if obj is None:
         obj = self.read_obj(self.uuid)
     self.name = obj['fq_name'][-1]
     self.fq_name = obj['fq_name']
     self.proj_name = obj['fq_name'][-2]
     self.params = obj['service_instance_properties']
     self.update_single_ref('service_template', obj)
     self.update_single_ref('loadbalancer_pool', obj)
     self.update_multiple_refs('virtual_machine', obj)
     self.id_perms = obj['id_perms']
     self.ha_mode = self.params.get('ha_mode', None)
     if self.ha_mode and self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [svc_info.get_active_preference(),
             svc_info.get_standby_preference()]
     else:
         scale_out = self.params.get('scale_out', None)
         if scale_out:
             self.max_instances = scale_out.get('max_instances', 1)
Exemplo n.º 15
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.service_template = None
     self.loadbalancer_pool = None
     self.virtual_machines = set()
     self.params = None
     self.state = 'init'
     self.image = None
     self.flavor = None
     self.max_instances = 0
     self.availability_zone = None
     self.ha_mode = None
     self.vr_id = None
     self.local_preference = [None, None]
     self.vn_info = []
     self.update(obj_dict)
     if self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [svc_info.get_active_preference(),
                                  svc_info.get_standby_preference()]
Exemplo n.º 16
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.service_template = None
     self.loadbalancer_pool = None
     self.virtual_machines = set()
     self.params = None
     self.state = 'init'
     self.image = None
     self.flavor = None
     self.max_instances = 0
     self.availability_zone = None
     self.ha_mode = None
     self.vr_id = None
     self.local_preference = [None, None]
     self.vn_info = []
     self.update(obj_dict)
     if self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [svc_info.get_active_preference(),
                                  svc_info.get_standby_preference()]
    def create_service(self, st, si):
        if not self.validate_network_config(st, si):
            return

        # get current vm list
        vm_list = [None] * si.max_instances
        for vm_id in si.virtual_machines:
            vm = VirtualMachineSM.get(vm_id)
            vm_list[vm.index] = vm

        # if vn has changed then delete VMs
        if si.vn_changed:
            for vm in vm_list:
                if vm:
                    self.delete_service(vm)
            vm_list = [None] * si.max_instances
            si.vn_changed = False

        # create and launch vm
        si.state = "launching"
        instances = []
        for index in range(0, si.max_instances):
            vm = self._check_create_netns_vm(index, si, st, vm_list[index])
            if not vm:
                continue

            vr_name = self._associate_vrouter(si, vm)
            if not vr_name:
                self.logger.log_error("No vrouter available for VM %s" % vm.name)

            if si.local_preference[index] == svc_info.get_standby_preference():
                ha = "standby: %s" % (si.local_preference[index])
            else:
                ha = "active: %s" % (si.local_preference[index])
            instances.append({"uuid": vm.uuid, "vr_name": vr_name, "ha": ha})

        # uve trace
        si.state = "active"
        self.logger.uve_svc_instance(
            (":").join(si.fq_name), status="CREATE", vms=instances, st_name=(":").join(st.fq_name)
        )
Exemplo n.º 18
0
 def update(self, obj=None):
     if obj is None:
         obj = self.read_obj(self.uuid)
     self.name = obj['fq_name'][-1]
     self.fq_name = obj['fq_name']
     self.proj_name = obj['fq_name'][-2]
     self.params = obj['service_instance_properties']
     self.update_single_ref('service_template', obj)
     self.update_single_ref('loadbalancer_pool', obj)
     self.update_multiple_refs('virtual_machine', obj)
     self.id_perms = obj['id_perms']
     self.ha_mode = self.params.get('ha_mode', None)
     if self.ha_mode and self.ha_mode == 'active-standby':
         self.max_instances = 2
         self.local_preference = [
             svc_info.get_active_preference(),
             svc_info.get_standby_preference()
         ]
     else:
         scale_out = self.params.get('scale_out', None)
         if scale_out:
             self.max_instances = scale_out.get('max_instances', 1)
Exemplo n.º 19
0
    def create_service(self, st_obj, si_obj):
        si_props = si_obj.get_service_instance_properties()
        st_props = st_obj.get_service_template_properties()
        if st_props is None:
            self.logger.log("Cannot find service template associated to "
                            "service instance %s" % si_obj.get_fq_name_str())
            return

        # populate nic information
        nics = self._get_nic_info(si_obj, si_props, st_props)

        # set max instances
        local_prefs = None
        max_instances = 1
        if si_props.get_ha_mode() == 'active-standby':
            max_instances = 2
            local_prefs = self._get_local_prefs(si_obj, max_instances)
        elif si_props.get_scale_out():
            max_instances = si_props.get_scale_out().get_max_instances()
        self.db.service_instance_insert(si_obj.get_fq_name_str(),
                                        {'max-instances': str(max_instances)})

        # Create virtual machines, associate them to the service instance and
        # schedule them to different virtual routers
        instances = []
        for inst_count in range(0, max_instances):
            # Create a virtual machine
            instance_name = self._get_instance_name(si_obj, inst_count)
            try:
                vm_obj = self._vnc_lib.virtual_machine_read(
                    fq_name=[instance_name])
                self.logger.log("Info: VM %s already exists" % (instance_name))
            except NoIdError:
                vm_obj = VirtualMachine(instance_name)
                self._vnc_lib.virtual_machine_create(vm_obj)
                self.logger.log("Info: VM %s created" % (instance_name))

            si_refs = vm_obj.get_service_instance_refs()
            if (si_refs is None) or (si_refs[0]['to'][0] == 'ERROR'):
                vm_obj.set_service_instance(si_obj)
                self._vnc_lib.virtual_machine_update(vm_obj)
                self.logger.log("Info: VM %s updated with SI %s" %
                                (instance_name, si_obj.get_fq_name_str()))

            # Create virtual machine interfaces with an IP on networks
            local_preference = None
            if local_prefs:
                local_preference = local_prefs[inst_count]

            for nic in nics:
                user_visible = True
                if (st_props.get_service_type() ==
                        svc_info.get_lb_service_type()):
                    if nic['type'] == svc_info.get_right_if_str():
                        user_visible = False
                else:
                    user_visible = False
                vmi_obj = self._create_svc_vm_port(
                    nic,
                    instance_name,
                    st_obj,
                    si_obj,
                    local_preference=int(local_preference),
                    user_visible=user_visible,
                    ha_mode=si_props.get_ha_mode())
                if vmi_obj.get_virtual_machine_refs() is None:
                    vmi_obj.set_virtual_machine(vm_obj)
                    self._vnc_lib.virtual_machine_interface_update(vmi_obj)
                    self.logger.log("Info: VMI %s updated with VM %s" %
                                    (vmi_obj.get_fq_name_str(), instance_name))

            # Associate instance on the scheduled vrouter
            chosen_vr_fq_name = None
            vrouter_name = None
            state = 'pending'
            vrouter_back_refs = vm_obj.get_virtual_router_back_refs()
            if vrouter_back_refs is None:
                chosen_vr_fq_name = self.vrouter_scheduler.schedule(
                    si_obj.uuid, vm_obj.uuid)
                if chosen_vr_fq_name:
                    vrouter_name = chosen_vr_fq_name[-1]
                    state = 'active'
                    self.logger.log(
                        "Info: VRouter %s updated with VM %s" %
                        (':'.join(chosen_vr_fq_name), instance_name))
            else:
                vrouter_name = vrouter_back_refs[0]['to'][-1]
                state = 'active'

            vm_db_entry = self._set_vm_db_info(inst_count, instance_name,
                                               vm_obj.uuid, state,
                                               vrouter_name, local_preference)
            self.db.service_instance_insert(si_obj.get_fq_name_str(),
                                            vm_db_entry)

            if int(local_preference) == svc_info.get_standby_preference():
                ha = ("standby: %s" % (local_preference))
            else:
                ha = ("active: %s" % (local_preference))

            instances.append({
                'uuid': vm_obj.uuid,
                'vr_name': vrouter_name,
                'ha': ha
            })

        # uve trace
        self.logger.uve_svc_instance(si_obj.get_fq_name_str(),
                                     status='CREATE',
                                     vms=instances,
                                     st_name=st_obj.get_fq_name_str())
    def create_service(self, st_obj, si_obj):
        si_props = si_obj.get_service_instance_properties()
        st_props = st_obj.get_service_template_properties()
        if st_props is None:
            self.logger.log("Cannot find service template associated to "
                             "service instance %s" % si_obj.get_fq_name_str())
            return

        # populate nic information
        nics = self._get_nic_info(si_obj, si_props, st_props)

        # set max instances
        local_prefs = None
        max_instances = 1
        if si_props.get_ha_mode() == 'active-standby':
            max_instances = 2
            local_prefs = self._get_local_prefs(si_obj, max_instances)
        elif si_props.get_scale_out():
            max_instances = si_props.get_scale_out().get_max_instances()
        self.db.service_instance_insert(si_obj.get_fq_name_str(),
                                        {'max-instances': str(max_instances)})

        # Create virtual machines, associate them to the service instance and
        # schedule them to different virtual routers
        instances = []
        for inst_count in range(0, max_instances):
            # Create a virtual machine
            instance_name = self._get_instance_name(si_obj, inst_count)
            try:
                vm_obj = self._vnc_lib.virtual_machine_read(fq_name=[instance_name])
                self.logger.log("Info: VM %s already exists" % (instance_name))
            except NoIdError:
                vm_obj = VirtualMachine(instance_name)
                self._vnc_lib.virtual_machine_create(vm_obj)
                self.logger.log("Info: VM %s created" % (instance_name))

            si_refs = vm_obj.get_service_instance_refs()
            if (si_refs is None) or (si_refs[0]['to'][0] == 'ERROR'):
                vm_obj.set_service_instance(si_obj)
                self._vnc_lib.virtual_machine_update(vm_obj)
                self.logger.log("Info: VM %s updated with SI %s" %
                    (instance_name, si_obj.get_fq_name_str()))

            # Create virtual machine interfaces with an IP on networks
            local_preference = None
            if local_prefs:
                local_preference = local_prefs[inst_count]

            for nic in nics:
                user_visible = True
                if (st_props.get_service_type() ==
                        svc_info.get_lb_service_type()):
                    if nic['type'] == svc_info.get_right_if_str():
                        user_visible = False
                else:
                    user_visible = False
                vmi_obj = self._create_svc_vm_port(nic, instance_name, st_obj,
                    si_obj, int(local_preference), user_visible)
                if vmi_obj.get_virtual_machine_refs() is None:
                    vmi_obj.set_virtual_machine(vm_obj)
                    self._vnc_lib.virtual_machine_interface_update(vmi_obj)
                    self.logger.log("Info: VMI %s updated with VM %s" %
                                    (vmi_obj.get_fq_name_str(), instance_name))

            # Associate instance on the scheduled vrouter
            chosen_vr_fq_name = None
            vrouter_name = None
            state = 'pending'
            vrouter_back_refs = vm_obj.get_virtual_router_back_refs()
            if vrouter_back_refs is None:
                chosen_vr_fq_name = self.vrouter_scheduler.schedule(
                    si_obj.uuid, vm_obj.uuid)
                if chosen_vr_fq_name:
                    vrouter_name = chosen_vr_fq_name[-1]
                    state = 'active'
                    self.logger.log("Info: VRouter %s updated with VM %s" %
                        (':'.join(chosen_vr_fq_name), instance_name))
            else:
                vrouter_name = vrouter_back_refs[0]['to'][-1]
                state = 'active'

            vm_db_entry = self._set_vm_db_info(inst_count, instance_name,
                vm_obj.uuid, state, vrouter_name, local_preference)
            self.db.service_instance_insert(si_obj.get_fq_name_str(),
                                            vm_db_entry)

            if int(local_preference) == svc_info.get_standby_preference():
                ha = ("standby: %s" % (local_preference))
            else:
                ha = ("active: %s" % (local_preference))

            instances.append({'uuid': vm_obj.uuid,
                              'vr_name': vrouter_name,
                              'ha': ha})

        # uve trace
        self.logger.uve_svc_instance(si_obj.get_fq_name_str(),
            status='CREATE', vms=instances,
            st_name=st_obj.get_fq_name_str())