예제 #1
0
    def get(self):
        """
        Retrieval of all registered Kinds and Mixins
            Retrieval of a filtered list of Kinds and Mixins
        """
        category_json_serializer = json_serializer.category_serializer()

        _kind_values = list()
        for _kind in category_registry().get_kinds().values():
            _kind_values.append(
                json.loads(category_json_serializer.to_json(_kind)))

        _mixin_values = list()
        for _mixin in category_registry().get_mixins().values():
            _mixin_values.append(
                json.loads(category_json_serializer.to_json(_mixin)))

        result_json = _kind_values + _mixin_values

        result_dump = cStringIO.StringIO()
        json.dump(result_json, result_dump, indent=4 * ' ')

        self.res.body = result_dump.getvalue()

        return self.res
예제 #2
0
    def get(self):
        """
        Retrieval of all registered Kinds and Mixins
            Retrieval of a filtered list of Kinds and Mixins
        """
        category_json_serializer = json_serializer.category_serializer()

        _kind_values = list()
        for _kind in category_registry().get_kinds().values():
            _kind_values.append(json.loads(category_json_serializer.to_json(_kind)))

        _mixin_values = list()
        for _mixin in category_registry().get_mixins().values():
            _mixin_values.append(json.loads(category_json_serializer.to_json(_mixin)))

        result_json = _kind_values + _mixin_values

        result_dump = cStringIO.StringIO()
        json.dump(result_json, result_dump, indent=4 * ' ')

        self.res.body = result_dump.getvalue()

        return self.res
예제 #3
0
    def to_json(self, obj):
        """

        method to convert an object (OCCI object) to a JSON format

        """
        self.entity_dict = OrderedDict({'occi.core.id': obj.occi_core_id, 'occi.core.title': obj.occi_core_title})
        self.kind_dict = OrderedDict(
                {'kind': {'term': obj.kind.term, 'scheme': obj.kind.scheme, 'class': 'kind', 'title': obj.kind.title}})

        self.mixins_dict = OrderedDict({'mixins': []})
        mixins_values = list()
        for __mixin in obj.mixins:
            mixins_values.append(
                    {'term': __mixin.term, 'scheme': __mixin.scheme, 'class': 'mixin', 'title': __mixin.title})
        self.mixins_dict = {'mixins': mixins_values}

        self.attributes_dict = OrderedDict({'attributes': ''})
        attributes_values = OrderedDict()

        # to extract attributes defined directly by the Kind
        for __attribute in obj.kind.attributes:
            _a = obj.__getattribute__(re.sub('\.', '_', __attribute.name))
            _b = jsonpickle.encode(_a, unpicklable=False)
            attributes_values[str(__attribute.name)] = jsonpickle.decode(_b)

        # to extract attributes defined by the related Kinds
        for __kind_related in obj.kind.related:
            # to do (not urgent): attributes of the related kind of the related kind (recursively)
            # we use this if because tha main attributes of Resource and Link should be outside of Attributes bloc
            if __kind_related.__repr__() != Resource._kind.__repr__() and __kind_related.__repr__() != Link._kind.__repr__():
                for __attribute in  category_registry().get_kind(str(__kind_related)).attributes:
                    _a = obj.__getattribute__(re.sub('\.', '_', __attribute.name))
                    _b = jsonpickle.encode(_a, unpicklable=False)
                    attributes_values[str(__attribute.name)] = jsonpickle.decode(_b)

        # to extract attributes defined by the mixins and related mixins of each mixin
        for __mixin in obj.mixins:
            for __attribute in __mixin.attributes:
                _cc = obj.__getattribute__(re.sub('\.', '_', __attribute.name))
                _d = jsonpickle.encode(_cc, unpicklable=False)
                attributes_values[str(__attribute.name)] = jsonpickle.decode(_d)
            for __mixin_related in __mixin.related:
                # to do (not urgent): attributes of the related mixin of the related mixin (recursively)
                for __attribute in category_registry().get_kind(str(__mixin_related)).attributes:
                    _e = obj.__getattribute__(re.sub('\.', '_', __attribute.name))
                    _f = jsonpickle.encode(_e, unpicklable=False)
                    attributes_values[str(__attribute.name)] = jsonpickle.decode(_f)

        self.attributes_dict = {'attributes': attributes_values}

        self.link_dict = OrderedDict(
                {'occi.core.source': obj.occi_core_source, 'occi.core.target': obj.occi_core_target})

        self.location_dict = OrderedDict({'location': location_registry().get_location(obj)})

        result_json = OrderedDict(
            self.entity_dict.items() + self.kind_dict.items() + self.mixins_dict.items() + self.attributes_dict.items() + self.link_dict.items() + self.location_dict.items())
        result_dump = cStringIO.StringIO()
        json.dump(result_json, result_dump, indent=4 * ' ')

        logger.debug('link_serialization_to_json=' + result_dump.getvalue())
        return result_dump.getvalue()
예제 #4
0
    def from_json(self, json_doc):
        """

        method to convert from JSON to an object (OCCI object)

        """

        result_obj = jsonpickle.decode(json_doc)

        _kind_term = result_obj['kind']['term']
        _kind_scheme = result_obj['kind']['scheme']
        _kind_class = result_obj['kind']['class']

        _occi_core_id = result_obj['occi.core.id']
        _occi_core_title = result_obj['occi.core.title']
        _occi_core_summary = result_obj['occi.core.summary']
        _mixins = result_obj['mixins']
        _links = result_obj['links']
        _attributes = result_obj['attributes']

        _kind = category_registry().get_kind(_kind_scheme + '#' + _kind_term)
        if  _kind is not None:
            _resource = Resource(occi_core_id=_occi_core_id, kind=_kind, occi_core_title=_occi_core_title,
                                 mixins=_mixins, occi_core_summary=_occi_core_summary, links=_links)

            # P.S. if the attribute value extracted from the json is a list type than use a boucle

            #  = 1 = pour tout les attributes du kind (ex attributs du kind compute), il faut ajouter les valeurs des attributs à resource
            for _attribute in _kind.attributes:
                _attribute_name = _attribute.name.replace('.', '_')
                # condition on the type of the attribute if it is based on a specific new class ('' means a standard type)
                if _attribute.type is '':
                    _resource.__dict__[_attribute_name] = _attributes[_attribute.name] or ''
                else:
                    # many elements
                    if str(_attribute.multiplicity).endswith('*'):
                        result_attribute = []
                        for a in _attributes[_attribute.name]:
                            # JUST EXAMPLE: fun = 'specification.ocni/specification.ocni.Availability(' + 'ocni_availability_start="' + str(t1) + '", ocni_availability_end="' + str(t2) + '")'
                            best_func = tools.classPath2modulePath(_attribute.type) + '/' + _attribute.type + '('
                            i = 0
                            for cle in a:
                                best_func += re.sub('\.', '_', str(cle)) + '="' + a[cle] + '"'
                                if i < len(a) - 1:
                                    best_func += ','
                                i += 1
                            best_func += ')'

                            obj = jsonpickle.unpickler.loadrepr(best_func)
                            result_attribute.append(obj)

                        _resource.__dict__[_attribute_name] = result_attribute
                        # just one element
                    else:
                        a = _attributes[_attribute.name]
                        best_func = tools.classPath2modulePath(_attribute.type) + '/' + _attribute.type + '('
                        i = 0
                        for cle in a:
                            best_func += re.sub('\.', '_', str(cle)) + '="' + a[cle] + '"'
                            if i < len(a) - 1:
                                best_func += ','
                            i += 1
                        best_func += ')'

                        obj = jsonpickle.unpickler.loadrepr(best_func)

                        _resource.__dict__[_attribute_name] = obj
                        pass
                    pass

                #_resource.__dict__.update(attribute_name)
                #_resource.attribute_name = lambda: None
                #setattr(_resource, attribute_name, 'attribute_name_value')
                #print getattr(_resource, attribute_name)

                logger.debug('the attribute to add:' + str(getattr(_resource, _attribute_name)))

            # = 2 = pour tout les mixins, et pour chaque mixin il faut: appliquer le mixin sur l'objet resource puis il faut ajouter les avaleurs des attributs du mixin à l'objet resource

            for _mixin in _mixins:
                _mixin_obj = category_registry().get_mixin(_mixin['scheme'] + '#' + _mixin['term'])
                if _mixin_obj is not None:
                    for _element in _mixin_obj.attributes:
                        _attribute_name = _element.name.replace('.', '_')
                        # condition on the type of the attribute if it is based on a specific new class ('' means a standard type)
                        if _element.type is '':
                            _resource.__dict__[_attribute_name] = _attributes[_element.name] or ''
                        else:
                            # many elements
                            if str(_element.multiplicity).endswith('*'):
                                result_element = []
                                for a in _attributes[_element.name]:
                                    # JUST EXAMPLE: fun = 'specification.ocni/specification.ocni.Availability(' + 'ocni_availability_start="' + str(t1) + '", ocni_availability_end="' + str(t2) + '")'
                                    best_func = tools.classPath2modulePath(_element.type) + '/' + _element.type + '('
                                    i = 0
                                    for cle in a:
                                        best_func += re.sub('\.', '_', str(cle)) + '="' + a[cle] + '"'
                                        if i < len(a) - 1:
                                            best_func += ','
                                        i += 1
                                    best_func += ')'

                                    obj = jsonpickle.unpickler.loadrepr(best_func)
                                    result_element.append(obj)

                                _resource.__dict__[_attribute_name] = result_element
                                # just one element
                            else:
                                a = _attributes[_element.name]
                                best_func = tools.classPath2modulePath(_element.type) + '/' + _element.type + '('
                                i = 0
                                for cle in a:
                                    best_func += re.sub('\.', '_', str(cle)) + '="' + a[cle] + '"'
                                    if i < len(a) - 1:
                                        best_func += ','
                                    i += 1
                                best_func += ')'

                                obj = jsonpickle.unpickler.loadrepr(best_func)

                                _resource.__dict__[_attribute_name] = obj
                                pass
                            pass
                            ## just as backup in case of a problem by replacing this block
                            #if _element.type is '':
                            #    _resource.__dict__[_attribute_name] = _attributes[_element.name] or ''
                            #else:
                            #    pass
                else:
                    logger.warning('trying to use an unregistered mixin')
                    return 'FAILED to add this resource because you want to use an unregistered mixin'
                pass

            return _resource

        else:
            logger.warning('trying to create a resource of an unregistered kind')
            return 'FAILED to add this resource'
예제 #5
0
# ======================================================================================
# main
# ======================================================================================
if __name__ == '__main__':
    # ======================================================================================
    # Needed import for registration of entities to the registries
    # ======================================================================================
    from specification.occi_infrastructure import Compute, Network, Storage, NetworkInterface, StorageLink, IPNetworking, IPNetworkInterface

    # ======================================================================================
    # the category registry
    # ======================================================================================

    # register OCCI Core kinds
    category_registry().register_kind(Entity._kind)
    category_registry().register_kind(Resource._kind)
    category_registry().register_kind(Link._kind)

    # register OCCI Infrastructure kinds
    category_registry().register_kind(Compute._kind)
    category_registry().register_kind(Network._kind)
    category_registry().register_kind(Storage._kind)
    category_registry().register_kind(NetworkInterface._kind)
    category_registry().register_kind(StorageLink._kind)

    # register OCCI Infrastructure mixins
    category_registry().register_mixin(IPNetworking())
    category_registry().register_mixin(IPNetworkInterface())

    # ======================================================================================
예제 #6
0
    location_registry().purge_objects_db()


# ======================================================================================
# the category registry
# ======================================================================================

#category_registry = category_registry()

# register OCCI Core kinds
#category_registry().register_kind(Entity._kind)
#category_registry().register_kind(Resource._kind)
#category_registry().register_kind(Link._kind)

# register OCCI Infrastructure kinds
category_registry().register_kind(Compute._kind)
category_registry().register_kind(Network._kind)
category_registry().register_kind(Storage._kind)
category_registry().register_kind(NetworkInterface._kind)
category_registry().register_kind(StorageLink._kind)

# register OCCI Infrastructure mixins
category_registry().register_mixin(IPNetworking())
category_registry().register_mixin(IPNetworkInterface())

# register OCNI kinds
category_registry().register_kind(CloNeNode._kind)
category_registry().register_kind(CloNeLink._kind)
category_registry().register_kind(FNS._kind)
category_registry().register_kind(CloNeComputeLink._kind)
category_registry().register_kind(CloNeStorageLink._kind)
예제 #7
0
    location_registry().purge_locations_db()
    location_registry().purge_objects_db()

# ======================================================================================
# the category registry
# ======================================================================================

#category_registry = category_registry()

# register OCCI Core kinds
#category_registry().register_kind(Entity._kind)
#category_registry().register_kind(Resource._kind)
#category_registry().register_kind(Link._kind)

# register OCCI Infrastructure kinds
category_registry().register_kind(Compute._kind)
category_registry().register_kind(Network._kind)
category_registry().register_kind(Storage._kind)
category_registry().register_kind(NetworkInterface._kind)
category_registry().register_kind(StorageLink._kind)

# register OCCI Infrastructure mixins
category_registry().register_mixin(IPNetworking())
category_registry().register_mixin(IPNetworkInterface())

# register OCNI kinds
category_registry().register_kind(CloNeNode._kind)
category_registry().register_kind(CloNeLink._kind)
category_registry().register_kind(FNS._kind)
category_registry().register_kind(CloNeComputeLink._kind)
category_registry().register_kind(CloNeStorageLink._kind)