def test_create(self):
        st_obj = vnc_api.ServiceTemplate(name="test-template")
        svc_properties = vnc_api.ServiceTemplateType()
        svc_properties.set_service_virtualization_type('vrouter-instance')
        svc_properties.set_image_name('test')
        svc_properties.set_ordered_interfaces(True)
        if_list = [['management', False], ['left', False], ['right', False]]
        for itf in if_list:
            if_type = vnc_api.ServiceTemplateInterfaceType(shared_ip=itf[1])
            if_type.set_service_interface_type(itf[0])
            svc_properties.add_interface_type(if_type)
        svc_properties.set_vrouter_instance_type("docker")
        st_obj.set_service_template_properties(svc_properties)
        si_obj = vnc_api.ServiceInstance("test2")
        si_prop = vnc_api.ServiceInstanceType(
            left_virtual_network="left",
            right_virtual_network="right",
            management_virtual_network="management")
        si_prop.set_interface_list([
            vnc_api.ServiceInstanceInterfaceType(virtual_network="left"),
            vnc_api.ServiceInstanceInterfaceType(virtual_network="right"),
            vnc_api.ServiceInstanceInterfaceType(virtual_network="management")
        ])
        si_prop.set_virtual_router_id(uuid.uuid4())
        si_obj.set_service_instance_properties(si_prop)
        si_obj.set_service_template(st_obj)
        si_obj.uuid = str(uuid.uuid4())
        st_obj.uuid = str(uuid.uuid4())

        self.vrouter_manager.create_service(st_obj, si_obj)
        self.vrouter_manager.db.service_instance_insert.assert_called_with(
            si_obj.get_fq_name_str(), DBObjMatcher(self.DB_PREFIX))
    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        try:
            si_obj = self.vnc_lib().service_instance_read(id=self.resource_id)
        except vnc_api.NoIdError:
            LOG.warn(_("Service Instance %s not found.") % self.name)
            raise
        except:
            LOG.warn(_("Unknown error."))
            raise

        si_prop = si_obj.get_service_instance_properties()

        scaleprop = prop_diff.get(self.SCALE_OUT)
        if scaleprop:
            max_instances = scaleprop.get(self.MAX_INSTANCES)
            auto_scale = scaleprop.get(self.AUTO_SCALE)
        scale_out = vnc_api.ServiceScaleOutType(max_instances=max_instances,
                                                auto_scale=auto_scale)
        si_prop.set_scale_out(scale_out)

        # allowed address pairs
        aapsprop = prop_diff.get(self.INTERFACE_LIST)
        if aapsprop:
            for intf in aapsprop:
                aaps = self._set_allowed_address_pairs(intf)
                if_type = vnc_api.ServiceInstanceInterfaceType(
                    allowed_address_pairs=aaps)
                si_prop.add_interface_list(if_type)

        si_obj.set_service_instance_properties(si_prop)
        self.vnc_lib().service_instance_update(si_obj)
    def handle_create(self):
        try:
            st_obj = self.vnc_lib().service_template_read(
                id=self.properties[self.SERVICE_TEMPLATE])
        except vnc_api.NoIdError:
            st_obj = self.vnc_lib().service_template_read(
                fq_name_str=self.properties[self.SERVICE_TEMPLATE])

        tenant_id = self.stack.context.tenant_id
        project_obj = self.vnc_lib().project_read(id=str(uuid.UUID(tenant_id)))
        si_obj = vnc_api.ServiceInstance(
            name=self.properties[self.NAME], parent_obj=project_obj)

        svc_tmpl_if_list = st_obj.get_service_template_properties().interface_type
        svc_inst_if_list = self.properties[self.INTERFACE_LIST]
        if len(svc_tmpl_if_list) != len(svc_inst_if_list):
            raise vnc_api.BadRequest

        if_index = 0
        si_prop = vnc_api.ServiceInstanceType()
        for intf in self.properties[self.INTERFACE_LIST]:
            virt_net = intf[self.VIRTUAL_NETWORK]
            if virt_net == "auto":
                vn_name = ""
            elif not ":" in virt_net:
                fq_name = self.vnc_lib().id_to_fq_name(virt_net)
                vn_name = ":".join(fq_name)
            else:
                vn_name = virt_net

            # now check for static routes on this interface
            routes_list = {}
            if svc_tmpl_if_list[if_index].get_static_route_enable(
                ) and self.STATIC_ROUTES in intf:
                routes_list['route'] = intf[self.STATIC_ROUTES]

            if_type = vnc_api.ServiceInstanceInterfaceType(
                virtual_network=vn_name,static_routes=routes_list or None)
            si_prop.add_interface_list(if_type)
            if_index = if_index + 1

        if self.properties[self.SCALE_OUT] is None:
            max_instances = 1
            auto_scale = False
        else:
            max_instances = self.properties[self.SCALE_OUT][self.MAX_INSTANCES]
            auto_scale = self.properties[self.SCALE_OUT][self.AUTO_SCALE]
        scale_out = vnc_api.ServiceScaleOutType(max_instances=max_instances,
                                                auto_scale=auto_scale)
        si_prop.set_scale_out(scale_out)

        si_prop.set_availability_zone(self.properties[self.AVAILABILITY_ZONE])

        si_prop.set_ha_mode(self.properties[self.HA_MODE])
        si_obj.set_service_instance_properties(si_prop)

        st_obj = self.vnc_lib().service_template_read(id=st_obj.uuid)
        si_obj.set_service_template(st_obj)

        si_uuid = self.vnc_lib().service_instance_create(si_obj)
        self.resource_id_set(si_uuid)