summaryrefslogtreecommitdiffstats
path: root/app/toscalib/templates/database.py
blob: 7e21148ddfa33d4c80bc3261fdbb47935d812261 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#Author: Shu Shi
#emaiL: shushi@research.att.com

from toscalib.templates.constant import *
import logging

class ToscaDB(object):
    """ The database that stores all node types and TEMPLATES """
    def __init__(self):
        self.NODE_TYPES = {}
        self.CAPABILITY_TYPES = {}
        self.RELATIONSHIP_TYPES = {}
        self.DATA_TYPES = {}
        self.TEMPLATES = {}
        
    def _import_node_type(self, new_type):
        if new_type is None:
            return
#        if self.NODE_TYPES.has_key(new_type.name) == True:
        if new_type.name in self.NODE_TYPES:
            logging.debug( 'Node type: '+ new_type.name+ ' already defined and will be overwritten')
        self.NODE_TYPES[new_type.name]=new_type
            
    def _import_capability_type(self, new_type):
        if new_type is None:
            return
#        if self.CAPABILITY_TYPES.has_key(new_type.name) == True:
        if new_type.name in self.CAPABILITY_TYPES:
            logging.debug( 'Capability type: '+ new_type.name+ ' already defined and will be overwritten')

        self.CAPABILITY_TYPES[new_type.name]=new_type

    def _import_relationship_type(self, new_type):
        if new_type is None:
            return
#        if self.RELATIONSHIP_TYPES.has_key(new_type.name) == True:
        if new_type.name in self.RELATIONSHIP_TYPES:
            logging.debug( 'Relationship type: '+ new_type.name+ ' already defined and will be overwritten')

        self.RELATIONSHIP_TYPES[new_type.name]=new_type

    def _import_data_type(self, new_type):
        if new_type is None:
            return
#        if self.DATA_TYPES.has_key(new_type.name) == True:
        if new_type.name in self.DATA_TYPES:
            logging.debug( 'Data type: '+ new_type.name+ ' already defined and will be overwritten')
        self.DATA_TYPES[new_type.name]=new_type

    def _import_template(self, new_template):
        if new_template is None:
            return
#        if self.TEMPLATES.has_key(new_template.name) == False:
        if new_template.name not in self.TEMPLATES :
            self.TEMPLATES[new_template.name]= new_template        
    
    def _parse_objects(self):
        logging.debug( 'parsing database')
#        for objs in self.NODE_TYPES.itervalues():
        for objs in iter(self.NODE_TYPES.values()):
            objs._parse_content(self)
#        for objs in self.CAPABILITY_TYPES.itervalues():
        for objs in iter(self.CAPABILITY_TYPES.values()):
            objs._parse_content(self)
#        for objs in self.DATA_TYPES.itervalues():
        for objs in iter(self.DATA_TYPES.values()):
            objs._parse_content(self)
#        for objs in self.RELATIONSHIP_TYPES.itervalues():
        for objs in iter(self.RELATIONSHIP_TYPES.values()):
            objs._parse_content(self)
#        for objs in self.TEMPLATES.itervalues():
        for objs in iter(self.TEMPLATES.values()):
            objs._parse_content(self)
            
            
    def _prepare_schema(self):
        schema_output = {}
        data_sec = {}
        for key in self.DATA_TYPES.keys():
            objs = self.DATA_TYPES[key]
            data_sec[key] = objs.raw_content
        node_sec = {}
        for key in self.NODE_TYPES.keys():
            objs = self.NODE_TYPES[key]
            if objs.raw_content is None:
                objs._create_rawcontent()
            node_sec[key]=objs.raw_content
        cap_sec = {}
        for key in self.CAPABILITY_TYPES.keys():
            objs = self.CAPABILITY_TYPES[key]
            cap_sec[key]=objs.raw_content
        rel_sec = {}
        for key in self.RELATIONSHIP_TYPES.keys():
            objs = self.RELATIONSHIP_TYPES[key]
            rel_sec[key]=objs.raw_content
            
        if len(data_sec) > 0:
            schema_output[YMO_DATA_TYPE] = data_sec
        if len(node_sec) > 0:
            schema_output[YMO_NODE_TYPE] = node_sec
        if len(cap_sec) > 0:
            schema_output[YMO_CAPABILITY_TYPE] = cap_sec
        if len(rel_sec) > 0:
            schema_output[YMO_RELATIONSHIP_TYPE] = rel_sec
        
        schema_output[YMO_VERSION]= 'tosca_simple_yaml_1_0_0'
        
        return schema_output