Exemplo n.º 1
0
    def __new__(cls, agent_url, class_name, **kwargs_ontology):

        entity_id = lib_util.KWArgsToEntityId(class_name, **kwargs_ontology)
        if agent_url:
            host_agent = agent_to_host(agent_url)
            instance_key = host_agent + "+++" + entity_id
        else:
            instance_key = "NO_AGENT" + "+++" + entity_id

        # TODO: The key to this class instance must include the host associated to the agent.
        try:
            cache_instance = cls.m_instances_cache[instance_key]
            return cache_instance
        except KeyError:

            new_instance = super(BaseCIMClass, cls).__new__(cls)
            cls.m_instances_cache[instance_key] = new_instance
            return new_instance
Exemplo n.º 2
0
def create_CIM_class(agentUrl,className,**kwargsOntology):
    global CacheCIMClasses
    entity_id = lib_util.KWArgsToEntityId(className,**kwargsOntology)

    # No need to use the class in the key, because the cache is class-specific.
    DEBUG("create_CIM_class agentUrl=%s className=%s entity_id=%s",agentUrl,className,entity_id)

    try:
        newCIMClass = CacheCIMClasses[className]
        #DEBUG("Found existing className=%s",className)
    except KeyError:
        # This class is not yet created.
        # TODO: If entity_label contains slashes, submodules must be imported.
        newCIMClass = CIM_class_factory_no_cache(className)

        CacheCIMClasses[className] = newCIMClass

    # Now, it creates a new instance and stores it in the cache of the CIM class.
    newInstance = newCIMClass(agentUrl, className, **kwargsOntology)
    return newInstance
Exemplo n.º 3
0
    def __new__(cls, agentUrl, className, **kwargsOntology):

        entity_id = lib_util.KWArgsToEntityId(className, **kwargsOntology)
        if agentUrl:
            hostAgent = agent_to_host(agentUrl)
            instanceKey = hostAgent + "+++" + entity_id
        else:
            instanceKey = "NO_AGENT" + "+++" + entity_id

        # TODO: The key to this class instance must include the host associated to the agent.
        try:
            cacheInstance = cls.m_instances_cache[instanceKey]
            DEBUG("BaseCIMClass.__new__ %s is IN the cache instanceKey=",instanceKey)
            return cacheInstance
        except KeyError:
            DEBUG("BaseCIMClass.__new__ %s is NOT in the cache instanceKey=",instanceKey)
            # newInstance = super(ClassA, cls).__new__(cls, agentUrl, **kwargsOntology)
            #DEBUG("cls=%s kwargs=%s",cls.__name__,str(kwargs))

            newInstance = super(BaseCIMClass, cls).__new__(cls)
            cls.m_instances_cache[instanceKey] = newInstance
            return newInstance
Exemplo n.º 4
0
 def Derived__init__(self, agent_url, class_name, **kwargs_ontology):
     """This function will be used as a constructor for the new class."""
     for key, value in kwargs_ontology.items():
         setattr(self, key, value)
     entity_id = lib_util.KWArgsToEntityId(class_name, **kwargs_ontology)
     BaseCIMClass.__init__(self, agent_url, entity_id, kwargs_ontology)
Exemplo n.º 5
0
    def CallbackSelect(self, grph, class_name, predicate_prefix,
                       filtered_where_key_values):
        DEBUG(
            "SurvolCallbackSelect class_name=%s predicate_prefix=%s where_key_values=%s",
            class_name, predicate_prefix, str(filtered_where_key_values))

        # Maybe there is a script: predicate_prefix="survol:CIM_DataFile/mapping_processes"
        prefix, colon, script_nickname = predicate_prefix.partition(":")
        DEBUG("SurvolCallbackSelect script_nickname=%s", script_nickname)

        if script_nickname:
            # For example: script_nickname="CIM_DataFile/mapping_processes"
            # Wildcards or directories are not accepted yet.
            script_name = "sources_types/" + script_nickname + ".py"
            DEBUG(
                "SurvolCallbackSelect script_name=%s filtered_where_key_values=%s",
                script_name, str(filtered_where_key_values))

            # TODO: Check that there are enough parameters for this script ?

            my_source = lib_client.SourceLocal(script_name, class_name,
                                               **filtered_where_key_values)
            DEBUG("SurvolCallbackSelect my_source=%s", my_source)
            my_triplestore = my_source.get_triplestore()

            # This is returned anyway, as a triplestore that rdflib Sparql can work on.
            my_triplestore.copy_to_graph(grph)

            list_instances = my_triplestore.get_instances()

            # TODO: We filter only the objects of the right type,
            # TODO: ... but we lose all the other objects which could be stored in the output triplestore !!...

            DEBUG("SurvolCallbackSelect tp=%s class_name=%s",
                  type(list_instances), class_name)
            DEBUG("SurvolCallbackSelect list_instances=%s",
                  str(list_instances))
            for one_instance in list_instances:
                WARNING(
                    "SurvolCallbackSelect one_instance.__class__.__name__=%s",
                    one_instance.__class__.__name__)
                if one_instance.__class__.__name__ == class_name:
                    # 'CIM_DataFile.Name=/usr/lib/systemd/systemd-journald'
                    instance_url = one_instance.__class__.__name__ + "." + one_instance.m_entity_id

                    one_instance.m_key_value_pairs[
                        lib_kbase.
                        PredicateIsDefinedBy] = lib_common.NodeLiteral(
                            predicate_prefix)
                    # Add it again, so the original Sparql query will work.
                    one_instance.m_key_value_pairs[
                        lib_kbase.PredicateSeeAlso] = lib_common.NodeLiteral(
                            predicate_prefix)
                    DEBUG("SurvolCallbackSelect instance_url=%s", instance_url)
                    yield (instance_url, one_instance.m_key_value_pairs)

        else:
            entity_module = lib_util.GetEntityModule(class_name)
            if not entity_module:
                raise Exception(
                    "SurvolCallbackSelect: No module for class:%s" %
                    class_name)

            try:
                enumerate_function = entity_module.SelectFromWhere
            except AttributeError:
                exc = sys.exc_info()[1]
                WARNING("No Enumerate for %s:%s", class_name, str(exc))
                return

            iter_enumeration = enumerate_function(filtered_where_key_values)
            # for one_key_value_dict in iter_enumeration:
            for one_key_value_dict_nodes in iter_enumeration:
                class_ontology = lib_util.OntologyClassKeys(class_name)
                ontology_key_values = {}
                for key_node, value_node in one_key_value_dict_nodes.items():
                    key_str = lib_properties.PropToQName(key_node)
                    if key_str in class_ontology:
                        ontology_key_values[key_str] = str(value_node)

                # This reorders the attributes if needed.
                key_value_path = lib_util.KWArgsToEntityId(
                    class_name, **ontology_key_values)

                # key_value_path = ".".join( '%s="%s"' % ( lib_properties.PropToQName(key), str(value) ) for key, value in one_key_value_dict_nodes.items() )
                object_path = "SurvolLocalHost:" + class_name + "." + key_value_path

                one_key_value_dict_nodes[
                    lib_kbase.PredicateIsDefinedBy] = lib_common.NodeLiteral(
                        predicate_prefix)
                # Add it again, so the original Sparql query will work.
                one_key_value_dict_nodes[
                    lib_kbase.PredicateSeeAlso] = lib_common.NodeLiteral(
                        predicate_prefix)

                yield (object_path, one_key_value_dict_nodes)