Example #1
0
def get_template_by_name(name, **kwargs):
    """
        Get a specific resource template, by name.
    """
    try:
        tmpl_i = db.DBSession.query(Template).filter(
            Template.name == name).one()

        tmpl_j = JSONObject(tmpl_i)

        tmpl_j.templatetypes = tmpl_i.get_types()

        return tmpl_j
    except NoResultFound:
        log.info("%s is not a valid identifier for a template", name)
        raise HydraError('Template "%s" not found' % name)
Example #2
0
def get_template(template_id, **kwargs):
    """
        Get a specific resource template, by ID.
    """
    try:
        tmpl_i = db.DBSession.query(Template).filter(
            Template.id == template_id).one()

        tmpl_j = JSONObject(tmpl_i)

        tmpl_j.templatetypes = tmpl_i.get_types()

        #ignore the messing around we've been doing to the ORM objects
        #db.DBSession.expunge(tmpl_i)

        return tmpl_j
    except NoResultFound:
        raise HydraError("Template %s not found" % template_id)
Example #3
0
def mock_template(client):
    link_attr_1 = client.testutils.create_attribute("link_attr_1", dimension='Pressure')
    link_attr_2 = client.testutils.create_attribute("link_attr_2", dimension='Speed')
    node_attr_1 = client.testutils.create_attribute("node_attr_1", dimension='Volume')
    node_attr_2 = client.testutils.create_attribute("node_attr_2", dimension='Speed')
    net_attr_1 = client.testutils.create_attribute("net_attr_2", dimension='Speed')

    template = JSONObject()
    template.name = 'Test template @ %s' % datetime.datetime.now()

    layout = {}
    layout['groups'] = '<groups>...</groups>'

    template.layout = layout

    template.templatetypes = []
    # **********************
    # type 1           #
    # **********************
    type1 = JSONObject()
    type1.name = "Node type"
    type1.alias = "Node type alias"
    type1.resource_type = 'NODE'

    type1.typeattrs = []

    tattr_1 = JSONObject()
    tattr_1.attr_id = node_attr_1.id
    tattr_1.description = "Type attribute 1 description"
    tattr_1.properties = {'test_property': "test property add type"}
    tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
    type1.typeattrs.append(tattr_1)

    tattr_2 = JSONObject()
    tattr_2.attr_id = node_attr_2.id
    tattr_2.description = "Type attribute 2 description"
    tattr_2.data_restriction = {'INCREASING': None}
    type1.typeattrs.append(tattr_2)

    template.templatetypes.append(type1)
    # **********************
    # type 2           #
    # **********************
    type2 = JSONObject()
    type2.name = "Link type"
    type2.alias = "Link type alias"
    type2.resource_type = 'LINK'

    type2.typeattrs = []

    tattr_1 = JSONObject()
    tattr_1.attr_id = link_attr_1.id
    type2.typeattrs.append(tattr_1)

    tattr_2 = JSONObject()
    tattr_2.attr_id = link_attr_2.id
    type2.typeattrs.append(tattr_2)

    template.templatetypes.append(type2)

    # **********************
    # type 3           #
    # **********************
    type3 = JSONObject()
    type3.name = "Network Type"
    type3.alias = "Network Type alias"
    type3.resource_type = 'NETWORK'

    type3.typeattrs = []

    tattr_3 = JSONObject()
    tattr_3.attr_id = net_attr_1.id
    tattr_3.data_restriction = {}

    type3.typeattrs.append(tattr_3)

    template.templatetypes.append(type3)

    new_template_i = client.add_template(template)
    # TODO: HACK to load the attr
    for tt in new_template_i.templatetypes:
        for ta in tt.typeattrs:
            ta.attr

    new_template_j = JSONObject(new_template_i)
    return new_template_j
Example #4
0
    def test_update_template(self, client):

        #Only applicable for tests.
        client.template.ATTR_CACHE = {}

        # Defining attributes
        attribute_1 = client.testutils.create_attribute("link_attr_1", dimension='Pressure')
        attribute_2 = client.testutils.create_attribute("link_attr_2", dimension='Speed')
        attribute_3 = client.testutils.create_attribute("node_attr_1", dimension='Volume')

        # Defining template
        template = JSONObject()

        template.name = 'Test Template @ %s'%datetime.datetime.now()

        template.templatetypes = []

        template_type_1 = JSONObject({
            "name" : "Node type 2",
            "alias" : "Node type 2 alias",
            "resource_type" : 'NODE',
            "typeattrs" : []
        })

        template_type_2 = JSONObject({
            "name" : "Link type 2",
            "alias" : "Link type 2 alias",
            "resource_type" : 'LINK',
            "typeattrs" : []
        })

        # Definind a typeattr data structure
        type_attribute_1 = JSONObject({
            "attr_id" : attribute_1.id,
            "unit_id" : client.get_unit_by_abbreviation('bar').id,
            "description" : "typeattr description 1",
            "properties" : {"test_property": "property value"}
        })
        template_type_1.typeattrs.append(type_attribute_1)

        type_attribute_2 = JSONObject({
            "attr_id" : attribute_2.id,
            "unit_id" : client.get_unit_by_abbreviation('mph').id,
            "description" : "typeattr description 2"
        })
        template_type_2.typeattrs.append(type_attribute_2)

        template.templatetypes.append(template_type_1)
        template.templatetypes.append(template_type_2)

        new_template_i = client.add_template(template)
        new_template_j = JSONObject(new_template_i)

        assert new_template_j.name == template.name, "Names are not the same!"
        assert new_template_j.id is not None, "New Template has no ID!"
        assert new_template_j.id > 0, "New Template has incorrect ID!"

        assert len(new_template_j.templatetypes) == 2, "Resource types did not add correctly"
        assert len(new_template_j.templatetypes[0].typeattrs) == 1, "Resource type attrs did not add correctly"
        assert new_template_j.templatetypes[0].typeattrs[0].unit_id == client.get_unit_by_abbreviation('bar').id

        #update the name of one of the types
        new_template_j.templatetypes[0].name = "Test type 3"
        updated_type_id = new_template_j.templatetypes[0].id

        #add a new type
        new_type = JSONObject({
            "name" : "New Node type",
            "alias" : "New Node type alias",
            "resource_type" : 'NODE',
            "typeattrs" : []
        })
        new_template_j.templatetypes.append(new_type)

        #add an template attr to one of the types
        type_attribute_3 = JSONObject({
            "attr_id" : attribute_3.id,
            "description" : "updated typeattr description 1",
            "properties" : {"test_property_of_added_type": "property value"}
        })
        new_template_j.templatetypes[0].typeattrs.append(type_attribute_3)
        updated_template_j = client.update_template(new_template_j)
        updated_template_retrieved_j = client.get_template(new_template_j.id)

        assert updated_template_retrieved_j.name == template.name, "Names are not the same!"

        updated_type = None
        for tmpltype in new_template_j.templatetypes:
            if tmpltype.id == updated_type_id:
                updated_type = tmpltype
                break

        assert updated_type.name == "Test type 3", "Resource types did not update correctly"

        assert len(updated_type.typeattrs) == 2, "Resource type template attr did not update correctly"

        """
            Test that when setting a unit on a type attr, it matches the dimension of its attr
            In this case, setting m^3(Volume) fails as the attr has a dimension of 'Pressure'
        """
        old_attribute_type_to_restore = updated_template_j.templatetypes[0].typeattrs[0].unit_id
        updated_template_j.templatetypes[0].typeattrs[0].unit_id = client.get_unit_by_abbreviation('m^3').id
        with pytest.raises(HydraError):
            client.update_template(updated_template_j)
        # Restoring the old value to not fail later for this reason
        updated_template_j.templatetypes[0].typeattrs[0].unit_id = old_attribute_type_to_restore


        """
            Testing the case in which the attribute has no dimension while the typeattr has UNIT_ID that is not none
        """
        attribute_dimension_none = client.testutils.create_attribute("node_attr_dimension_none", dimension=None)
        template_type_dimension_none = JSONObject({
            "name" : "Node type dimension_none",
            "alias" : "Node type dimension_none alias",
            "resource_type" : 'NODE',
            "typeattrs" : []
        })
        type_attribute_dimension_none = JSONObject({
            "attr_id" : attribute_dimension_none.id,
            "unit_id" : client.get_unit_by_abbreviation('bar').id,
            "description" : "type_attribute_dimension_none description 1",
            "properties" : {"test_property": "property value"}
        })
        template_type_dimension_none.typeattrs.append(type_attribute_dimension_none)
        updated_template_j.templatetypes.append(template_type_dimension_none)

        with pytest.raises(HydraError):
            client.update_template(updated_template_j) # It MUST fail because the unit_id is not consistent to dimension_id none
Example #5
0
    def create_template(self):

        net_attr1 = self.create_attribute("net_attr_a", dimension='Volume')
        net_attr2 = self.create_attribute("net_attr_c", dimension=None)
        link_attr_1 = self.create_attribute("link_attr_a", dimension='Pressure')
        link_attr_2 = self.create_attribute("link_attr_b", dimension='Speed')
        link_attr_3 = self.create_attribute("link_attr_c", dimension='Length')
        node_attr_1 = self.create_attribute("node_attr_a", dimension='Volume')
        node_attr_2 = self.create_attribute("node_attr_b", dimension='Speed')
        node_attr_3 = self.create_attribute("node_attr_c", dimension='Monetary value')
        node_attr_4 = self.create_attribute("node_attr_d", dimension='Volumetric flow rate')
        group_attr_1 = self.create_attribute("grp_attr_1", dimension='Monetary value')
        group_attr_2 = self.create_attribute("grp_attr_2", dimension=None)

        template = JSONObject()
        template['name'] = 'Default Template ' + str(datetime.datetime.now())


        types = []
        #**********************
        #network type         #
        #**********************
        net_type = JSONObject()
        net_type.name = "Default Network"
        net_type.alias = "Test type alias"
        net_type.resource_type = 'NETWORK'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = net_attr1.id
        typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        typeattr_1.unit_id = self.get_unit('m^3').id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = net_attr2.id
        typeattrs.append(typeattr_2)

        net_type.typeattrs = typeattrs

        types.append(net_type)
        #**********************
        # node type           #
        #**********************
        node_type = JSONObject()
        node_type.name = "Default Node"
        node_type.alias = "Test type alias"
        node_type.resource_type = 'NODE'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = node_attr_1.id
        typeattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        typeattr_1.unit_id = self.get_unit('m^3').id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = node_attr_2.id
        typeattr_2.data_restriction = {'INCREASING': None}
        typeattrs.append(typeattr_2)

        typeattr_3 = JSONObject()
        typeattr_3.attr_id = node_attr_3.id
        typeattrs.append(typeattr_3)

        typeattr_4 = JSONObject()
        typeattr_4.attr_id = node_attr_4.id
        typeattr_4.unit_id = self.get_unit("m^3 s^-1").id
        typeattrs.append(typeattr_4)

        node_type.typeattrs = typeattrs

        types.append(node_type)
        #**********************
        #link type            #
        #**********************
        link_type = JSONObject()
        link_type.name = "Default Link"
        link_type.resource_type = 'LINK'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = link_attr_1.id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = link_attr_2.id
        typeattrs.append(typeattr_2)

        typeattr_3 = JSONObject()
        typeattr_3.attr_id = link_attr_3.id
        typeattrs.append(typeattr_3)

        link_type.typeattrs = typeattrs

        types.append(link_type)

        #**********************
        #group type           #
        #**********************
        group_type = JSONObject()
        group_type.name = "Default Group"
        group_type.resource_type = 'GROUP'

        typeattrs = []

        typeattr_1 = JSONObject()
        typeattr_1.attr_id = group_attr_1.id
        typeattrs.append(typeattr_1)

        typeattr_2 = JSONObject()
        typeattr_2.attr_id = group_attr_2.id
        typeattrs.append(typeattr_2)

        group_type.typeattrs = typeattrs

        types.append(group_type)

        template.templatetypes = types

        new_template_i = self.client.add_template(template)
        new_template = JSONObject(new_template_i)

        assert new_template.name == template.name, "Names are not the same!"
        assert new_template.id is not None, "New Template has no ID!"
        assert new_template.id > 0, "New Template has incorrect ID!"

        assert len(new_template.templatetypes) == len(types), "Resource types did not add correctly"
        for t in new_template.templatetypes[1].typeattrs:
            assert t.attr_id in (node_attr_1.id, node_attr_2.id, node_attr_3.id, node_attr_4.id),\
                "Node types were not added correctly!"

        for t in new_template.templatetypes[2].typeattrs:
            assert t.attr_id in (link_attr_1.id, link_attr_2.id, link_attr_3.id),\
                "Link types were not added correctly!"

        return new_template
Example #6
0
    def test_update_template(self):

        attr_1 = self.create_attr("link_attr_1", dimension='Pressure')
        attr_2 = self.create_attr("link_attr_2", dimension='Speed')
        attr_3 = self.create_attr("node_attr_1", dimension='Volume')

        template = JSONObject()

        template.name = 'Test Template @ %s' % datetime.datetime.now()

        template.templatetypes = []

        type_1 = JSONObject()
        type_1.name = "Node type 2"
        type_1.alias = "Node type 2 alias"
        type_1.resource_type = 'NODE'
        type_1.typeattrs = []

        type_2 = JSONObject()
        type_2.name = "Link type 2"
        type_2.alias = "Link type 2 alias"
        type_2.resource_type = 'LINK'
        type_2.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = attr_1.id
        tattr_1.unit = 'bar'
        tattr_1.description = "typeattr description 1"
        tattr_1.properties = {"test_property": "property value"}
        type_1.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = attr_2.id
        tattr_2.unit = 'mph'
        tattr_2.description = "typeattr description 2"
        type_2.typeattrs.append(tattr_2)

        template.templatetypes.append(type_1)
        template.templatetypes.append(type_2)

        new_template_i = add_template(template)
        new_template_j = JSONObject(new_template_i)

        assert new_template_j.name == template.name, "Names are not the same!"
        assert new_template_j.id is not None, "New Template has no ID!"
        assert new_template_j.id > 0, "New Template has incorrect ID!"

        assert len(new_template_j.templatetypes
                   ) == 2, "Resource types did not add correctly"
        assert len(new_template_j.templatetypes[0].typeattrs
                   ) == 1, "Resource type attrs did not add correctly"
        assert new_template_j.templatetypes[0].typeattrs[0].unit == 'bar'

        #update the name of one of the types
        new_template_j.templatetypes[0].name = "Test type 3"
        updated_type_id = new_template_j.templatetypes[0].id

        #add an template attr to one of the types
        tattr_3 = JSONObject()
        tattr_3.attr_id = attr_3.id
        tattr_3.description = "updated typeattr description 1"
        tattr_3.properties = {"test_property_of_added_type": "property value"}
        new_template_j.templatetypes[0].typeattrs.append(tattr_3)

        updated_template_i = update_template(new_template_j)
        updated_template_j = JSONObject(updated_template_i)

        assert updated_template_j.name == template.name, "Names are not the same!"

        updated_type = None
        for tmpltype in new_template_j.templatetypes:
            if tmpltype.id == updated_type_id:
                updated_type = tmpltype
                break

        assert updated_type.name == "Test type 3", "Resource types did not update correctly"

        assert len(
            updated_type.typeattrs
        ) == 2, "Resource type template attr did not update correctly"

        #Test that when setting a unit on a type attr, it matches the dimension of its attr
        #In this case, setting m^3(Volume) fails as the attr has a dimension of 'Pressure'
        updated_template_j.templatetypes[0].typeattrs[0].unit = 'm^3'
        self.assertRaises(HydraError, update_template, updated_template_j)
Example #7
0
    def test_add_template(self):

        link_attr_1 = self.create_attr("link_attr_1", dimension='Pressure')
        link_attr_2 = self.create_attr("link_attr_2", dimension='Speed')
        node_attr_1 = self.create_attr("node_attr_1", dimension='Volume')
        node_attr_2 = self.create_attr("node_attr_2", dimension='Speed')
        net_attr_1 = self.create_attr("net_attr_2", dimension='Speed')

        template = JSONObject()
        template.name = 'Test template @ %s' % datetime.datetime.now()

        layout = {}
        layout['groups'] = '<groups>...</groups>'

        template.layout = layout

        template.templatetypes = []
        #**********************
        #type 1           #
        #**********************
        type1 = JSONObject()
        type1.name = "Node type"
        type1.alias = "Node type alias"
        type1.resource_type = 'NODE'

        type1.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = node_attr_1.id
        tattr_1.description = "Type attribute 1 description"
        tattr_1.properties = {'test_property': "test property add type"}
        tattr_1.data_restriction = {'LESSTHAN': 10, 'NUMPLACES': 1}
        type1.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = node_attr_2.id
        tattr_2.description = "Type attribute 2 description"
        tattr_2.data_restriction = {'INCREASING': None}
        type1.typeattrs.append(tattr_2)

        template.templatetypes.append(type1)
        #**********************
        #type 2           #
        #**********************
        type2 = JSONObject()
        type2.name = "Link type"
        type2.alias = "Link type alias"
        type2.resource_type = 'LINK'

        type2.typeattrs = []

        tattr_1 = JSONObject()
        tattr_1.attr_id = link_attr_1.id
        type2.typeattrs.append(tattr_1)

        tattr_2 = JSONObject()
        tattr_2.attr_id = link_attr_2.id
        type2.typeattrs.append(tattr_2)

        template.templatetypes.append(type2)

        #**********************
        #type 3           #
        #**********************
        type3 = JSONObject()
        type3.name = "Network Type"
        type3.alias = "Network Type alias"
        type3.resource_type = 'NETWORK'

        type3.typeattrs = []

        tattr_3 = JSONObject()
        tattr_3.attr_id = net_attr_1.id
        tattr_3.data_restriction = {}

        type3.typeattrs.append(tattr_3)

        template.templatetypes.append(type3)

        new_template_i = add_template(template)
        #TODO: HACK to load the attr
        for tt in new_template_i.templatetypes:
            for ta in tt.typeattrs:
                ta.attr

        new_template_j = JSONObject(new_template_i)

        assert new_template_j.name == template.name, "Names are not the same!"
        assert str(new_template_j.layout) == str(
            template.layout), "Layouts are not the same!"
        assert new_template_j.id is not None, "New Template has no ID!"
        assert new_template_j.id > 0, "New Template has incorrect ID!"

        assert len(new_template_j.templatetypes
                   ) == 3, "Resource types did not add correctly"
        for t in new_template_j.templatetypes[0].typeattrs:
            assert t.attr_id in (node_attr_1.id, node_attr_2.id)
            "Node types were not added correctly!"

        for t in new_template_j.templatetypes[1].typeattrs:
            assert t.attr_id in (link_attr_1.id, link_attr_2.id)
            "Node types were not added correctly!"

        return new_template_j
Example #8
0
def update_template(template, auto_delete=False, **kwargs):
    """
        Update template and a type and typeattrs.
        args:
            template (JSONObject): The template to update
            auto_delete (bool): A flag to indicated whether missing types from `template.templatetypes`
                                should be deleted automatically. This flag is also
                                used when updating the typeattrs of type. Defaults to False.
    """
    tmpl = db.DBSession.query(Template).filter(
        Template.id == template.id).one()
    tmpl.name = template.name

    if template.status is not None:
        tmpl.status = template.status

    if template.description:
        tmpl.description = template.description

    template_types = tmpl.get_types()

    if template.layout:
        tmpl.layout = get_json_as_string(template.layout)

    type_dict = dict([(t.id, t) for t in template_types])

    #a list of all the templatetypes in the incoming template object
    req_templatetype_ids = []

    if template.types is not None or template.templatetypes is not None:
        types = template.types if template.types is not None else template.templatetypes
        for templatetype in types:

            if templatetype.id is not None and templatetype.template_id != tmpl.id:
                log.debug("Type %s is a part of a parent template. Ignoring.",
                          templatetype.id)
                req_templatetype_ids.append(type_i.id)
                continue

            if templatetype.id is not None:
                type_i = type_dict[templatetype.id]
                _update_templatetype(templatetype,
                                     auto_delete=auto_delete,
                                     **kwargs)
                req_templatetype_ids.append(type_i.id)
            else:
                #Give it a template ID if it doesn't have one
                templatetype.template_id = template.id
                new_templatetype_i = _update_templatetype(
                    templatetype, auto_delete=auto_delete, **kwargs)
                req_templatetype_ids.append(new_templatetype_i.id)

    if auto_delete is True:
        for ttype in template_types:
            if ttype.id not in req_templatetype_ids:
                delete_templatetype(ttype.id, **kwargs)

    db.DBSession.flush()

    updated_templatetypes = tmpl.get_types()

    tmpl_j = JSONObject(tmpl)

    tmpl_j.templatetypes = updated_templatetypes

    #db.DBSession.expunge(tmpl)

    return tmpl_j