summaryrefslogtreecommitdiffstats
path: root/app/toscalib/utils/tosca_operate.py
blob: bc67310c50ff8a8ab64dae9aa12eced81747401e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#Author: Shu Shi
#emaiL: shushi@research.att.com


from toscalib.templates.node import Node
import copy, logging
#from __builtin__ import False

def _create_new_node(template, type_name, node_name = None):
    db = template.db
#    if db.NODE_TYPES.has_key(type_name) is False:
    if type_name not in db.NODE_TYPES:
        new_name = 'unknown_'+ str(template.node_index)
        template.node_index = template.node_index + 1
        new_node = Node(template, new_name, None)
        logging.debug( 'New node: '+ new_name+ ' added')
        return new_node
    
    if node_name is None:
        new_name = _get_basename(db.NODE_TYPES[type_name].name) + '_' + str(template.node_index)
        template.node_index = template.node_index + 1
    else:
        new_name = node_name
         
    new_node = Node(template, new_name, db.NODE_TYPES[type_name])

    template._add_node(new_node)    
    
    logging.debug( 'New node: '+ new_name+ ' added')
    return new_node
    

def _create_new_template(template, type_name, prefix_name = None):
    db = template.db
    if prefix_name is None:
        prefix = db.TEMPLATES[type_name].name + '_' + str(template.temp_index) + '_'
        template.temp_index = template.temp_index + 1
    elif prefix_name == 'NO_PREFIX':
        prefix = ''
    else:
        prefix = prefix_name
    
    new_temp = copy.deepcopy(db.TEMPLATES[type_name])
    new_temp._update_prefix(prefix)
    template.inputs.update(new_temp.inputs)
    template.outputs.update(new_temp.outputs)
    template.node_dict.update(new_temp.node_dict)
    return template

def _assign_property_value(node, property_name, value):
#    if node.properties.has_key(property_name) is False:
    if property_name not in node.properties:
        logging.warning( 'No property with name '+ property_name+ ' in the node '+ node.name)
        return False
    return node.properties[property_name]._assign(value)

def _assign_capability_property_value(node, cap_name, prop_name, value):
#    if node.capabilities.has_key(cap_name) is False:
    if cap_name not in node.capabilities:
        logging.warning( 'No capability with name '+ cap_name+ ' in the node '+ node.name)
        return False
    cap_item = node.capabilities[cap_name]
#    if cap_item.properties.has_key(prop_name) is False:
    if prop_name not in cap_item.properties:
        logging.warning( 'No propoerty with name'+ prop_name+ ' in the node '+ node.name+ ' capability '+ cap_name)
        return False
    return cap_item.properties[prop_name]._assign(value)

def _assign_requirement_value(node, requirement_name, value):

    requirement_found = False
    for req in node.requirements:
        if req.name == requirement_name:
            requirement_found = req
            break
    if requirement_found is False:
        logging.warning( 'No requirement with name '+ requirement_name+ ' in the node '+ node.name)
        return False
    if isinstance(value, Node) is False:
        logging.warning( 'Node value should be passed to requirement assignment')
        return False
    else:
        if requirement_found._verify_node(value):
            requirement_found._assign(value)
        else:
            logging.warning( 'Invalid requirement fulfillment for node '+ node.name+ '->'+ requirement_name)
 
    
    return True
 
def _get_basename(name):
    names = name.split(".")
    return names[len(names)-1]