diff options
Diffstat (limited to 'app/toscalib/utils')
-rw-r--r-- | app/toscalib/utils/__init__.py | 0 | ||||
-rw-r--r-- | app/toscalib/utils/__init__.pyc | bin | 0 -> 156 bytes | |||
-rw-r--r-- | app/toscalib/utils/tosca_export.py | 40 | ||||
-rw-r--r-- | app/toscalib/utils/tosca_export.pyc | bin | 0 -> 1692 bytes | |||
-rw-r--r-- | app/toscalib/utils/tosca_heat.py | 6 | ||||
-rw-r--r-- | app/toscalib/utils/tosca_heat.pyc | bin | 0 -> 519 bytes | |||
-rw-r--r-- | app/toscalib/utils/tosca_import.py | 296 | ||||
-rw-r--r-- | app/toscalib/utils/tosca_import.pyc | bin | 0 -> 8467 bytes | |||
-rw-r--r-- | app/toscalib/utils/tosca_operate.py | 94 | ||||
-rw-r--r-- | app/toscalib/utils/tosca_operate.pyc | bin | 0 -> 3467 bytes | |||
-rw-r--r-- | app/toscalib/utils/tosca_print.py | 58 | ||||
-rw-r--r-- | app/toscalib/utils/tosca_print.pyc | bin | 0 -> 2453 bytes | |||
-rw-r--r-- | app/toscalib/utils/validateutils.py | 81 | ||||
-rw-r--r-- | app/toscalib/utils/yamlparser.py | 63 | ||||
-rw-r--r-- | app/toscalib/utils/yamlparser.pyc | bin | 0 -> 2236 bytes |
15 files changed, 638 insertions, 0 deletions
diff --git a/app/toscalib/utils/__init__.py b/app/toscalib/utils/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/toscalib/utils/__init__.py diff --git a/app/toscalib/utils/__init__.pyc b/app/toscalib/utils/__init__.pyc Binary files differnew file mode 100644 index 0000000..b047006 --- /dev/null +++ b/app/toscalib/utils/__init__.pyc diff --git a/app/toscalib/utils/tosca_export.py b/app/toscalib/utils/tosca_export.py new file mode 100644 index 0000000..c4399ef --- /dev/null +++ b/app/toscalib/utils/tosca_export.py @@ -0,0 +1,40 @@ +#Author: Shu Shi +#emaiL: shushi@research.att.com + + +import yaml +import re +from toscalib.templates.constant import * + +def _yaml_export(filename, content): + out_str = yaml.safe_dump(content, default_flow_style=False, width=float("inf")) + out_str = re.sub(YMO_PREFIX, '', out_str) + if filename == 'WEB': + return out_str + with open(filename, 'w') as outfile: + outfile.write(out_str) + return out_str + +def _heat_export(filename, content): + heat_ver = YMO_PREFIX + 'heat_template_version' + heat_content = content[YMO_TOPOLOGY] + if heat_content is None: + heat_content = {} + heat_content[heat_ver] = '2013-05-23' +# if heat_content.has_key(YMO_TOPO_OUTPUTS): + if YMO_TOPO_OUTPUTS in heat_content: + heat_content.pop(YMO_TOPO_OUTPUTS) + + out_str = yaml.dump(heat_content, default_flow_style=False) + out_str = re.sub(YMO_TOPO_INPUTS, YMO_PREFIX+'parameters', out_str) + out_str = re.sub(YMO_TOPO_NODE_TEMPLATES, YMO_PREFIX+'resources', out_str) + + out_str = re.sub('get_input', 'get_param', out_str) + out_str = re.sub('get_attribute', 'get_attr', out_str) + out_str = re.sub('get_id', 'get_resource', out_str) + out_str = re.sub('get_property', 'get_attr', out_str) + out_str = re.sub('type: integer', 'type: number', out_str) + + out_str = re.sub(YMO_PREFIX, '', out_str) + with open(filename, 'w') as outfile: + outfile.write(out_str) diff --git a/app/toscalib/utils/tosca_export.pyc b/app/toscalib/utils/tosca_export.pyc Binary files differnew file mode 100644 index 0000000..2552481 --- /dev/null +++ b/app/toscalib/utils/tosca_export.pyc diff --git a/app/toscalib/utils/tosca_heat.py b/app/toscalib/utils/tosca_heat.py new file mode 100644 index 0000000..ca5672c --- /dev/null +++ b/app/toscalib/utils/tosca_heat.py @@ -0,0 +1,6 @@ + +def _type_validate(type): + pass + +def _type_translate(type): + pass
\ No newline at end of file diff --git a/app/toscalib/utils/tosca_heat.pyc b/app/toscalib/utils/tosca_heat.pyc Binary files differnew file mode 100644 index 0000000..a69a761 --- /dev/null +++ b/app/toscalib/utils/tosca_heat.pyc diff --git a/app/toscalib/utils/tosca_import.py b/app/toscalib/utils/tosca_import.py new file mode 100644 index 0000000..d0e2f03 --- /dev/null +++ b/app/toscalib/utils/tosca_import.py @@ -0,0 +1,296 @@ +#Author: Shu Shi +#emaiL: shushi@research.att.com + + +import os +from toscalib.utils.yamlparser import load_yaml, simple_parse +from toscalib.templates.database import ToscaDB +from toscalib.types.node import NodeType +from toscalib.types.data import DataType +from toscalib.types.capability import CapabilityType +from toscalib.types.relationship import RelationshipType +from toscalib.templates.constant import * +from toscalib.templates.topology import ToscaTopology +from macpath import dirname +import logging + + +# _TRUE_VALUES = ('True', 'true', '1', 'yes') + +class import_context(object): + def __init__(self): + self.curr_path = os.getcwd() + self.curr_file_name = '' + self.metadata = None + self.temp_name = None + self.extra_imports = [] + +def _dir_import(files_imported, dir_name, db=None, ctx=None): + if db is None: + db = ToscaDB() + + if os.path.isdir(dir_name) is False: + logging.warning( 'Dir: '+ dir_name+ ' not exist! Loading failed!') + return db + + for f in os.listdir(dir_name): + filename = os.path.join(dir_name, f) + if os.path.isfile(filename): + try: + db = _file_import(files_imported, filename, db) + except ValueError: + logging.error( 'Fail to import file: '+ filename) + continue + if os.path.isdir(filename): + try: + db = _dir_import(files_imported, filename, db) + except ValueError: + logging.error( 'Fail to import dir: '+ filename) + continue + + return db + + + +def _file_import(files_imported, filename, db=None, ctx=None): + logging.debug( 'Start to import file: '+ filename) + + if db is None: + db = ToscaDB() + + if ctx is None: + orig_import = True + ctx = import_context() + else: + orig_import = False + + if filename == os.path.abspath(filename): + full_file_path = filename + else: + full_file_path = os.path.abspath(os.path.join(ctx.curr_path, filename)) + + if os.path.isfile(full_file_path) == False: + logging.debug( 'File: ' + filename + ' not exist! Import as extra import!') + ctx.extra_imports.append({filename:filename}) + return db + + if full_file_path in files_imported: + logging.debug( 'File: ' + filename + ' has been imported!') + return db + + ctx.curr_path = os.path.dirname(full_file_path) + ctx.curr_file_name = os.path.basename(full_file_path) + parser = load_yaml(full_file_path) + + ctx.extra_imports = [] + +# if parser.has_key(IMPORT): + if IMPORT in parser: + db = _parse_import(files_imported, parser[IMPORT], db, ctx) +# if parser.has_key(METADATA): + if METADATA in parser: + ctx.metadata = parser[METADATA] +# if parser.has_key(DATA_TYPE): + if DATA_TYPE in parser: + db = _parse_data_type(parser[DATA_TYPE], db, ctx) +# if parser.has_key(NODE_TYPE): + if NODE_TYPE in parser: + db = _parse_node_type(parser[NODE_TYPE], db, ctx) +# if parser.has_key(TOPOLOGY): + if TOPOLOGY in parser: + db = _parse_topology_template(parser[TOPOLOGY], db, ctx) +# if parser.has_key(CAPABILITY_TYPE): + if CAPABILITY_TYPE in parser: + db = _parse_capability_type(parser[CAPABILITY_TYPE], db, ctx) +# if parser.has_key(RELATIONSHIP_TYPE): + if RELATIONSHIP_TYPE in parser: + db = _parse_relationship_type(parser[RELATIONSHIP_TYPE], db, ctx) + + if orig_import: + db._parse_objects() + + files_imported.append(full_file_path) + logging.debug( 'File '+ filename+ ' imported!') + return db + +def _single_template_file_import(filename, db=None, ctx=None): + logging.debug( 'Start to import file: '+ filename) + + if db is None: + db = ToscaDB() + + if ctx is None: + ctx = import_context() + + if filename == os.path.abspath(filename): + full_file_path = filename + else: + full_file_path = os.path.abspath(os.path.join(ctx.curr_path, filename)) + + if os.path.isfile(full_file_path) == False: + logging.warning( 'File: ' +filename + ' not exist! Import failed!') + return db + + + ctx.curr_path = os.path.dirname(full_file_path) + ctx.curr_file_name = os.path.basename(full_file_path) + parser = load_yaml(full_file_path) + +# if parser.has_key(IMPORT): +# db = _parse_import(files_imported, parser[IMPORT], db, ctx) + ctx.extra_imports = [] +# if parser.has_key(IMPORT): + if IMPORT in parser: + db = _parse_import([], parser[IMPORT], db, ctx) +# if parser.has_key(METADATA): + if METADATA in parser: + ctx.metadata = parser[METADATA] +# if parser.has_key(DATA_TYPE): + if DATA_TYPE in parser: + db = _parse_data_type(parser[DATA_TYPE], db, ctx) +# if parser.has_key(NODE_TYPE): + if NODE_TYPE in parser: + db = _parse_node_type(parser[NODE_TYPE], db, ctx) +# if parser.has_key(TOPOLOGY): + if TOPOLOGY in parser: + db = _parse_topology_template(parser[TOPOLOGY], db, ctx) +# if parser.has_key(CAPABILITY_TYPE): + if CAPABILITY_TYPE in parser: + db = _parse_capability_type(parser[CAPABILITY_TYPE], db, ctx) +# if parser.has_key(RELATIONSHIP_TYPE): + if RELATIONSHIP_TYPE in parser: + db = _parse_relationship_type(parser[RELATIONSHIP_TYPE], db, ctx) + + db._parse_objects() + + logging.debug( 'File '+ filename+ ' imported!') + return db + +def _yaml_str_import(yml_str, db=None, ctx=None): + parser = simple_parse(yml_str) + + if ctx is None: + ctx = import_context() + + ctx.extra_imports = [] + +# if parser.has_key(IMPORT): + if IMPORT in parser: + db = _parse_import([], parser[IMPORT], db, ctx) +# if parser.has_key(METADATA): + if METADATA in parser: + ctx.metadata = parser[METADATA] +# if parser.has_key(DATA_TYPE): + if DATA_TYPE in parser: + db = _parse_data_type(parser[DATA_TYPE], db, ctx) +# if parser.has_key(NODE_TYPE): + if NODE_TYPE in parser: + db = _parse_node_type(parser[NODE_TYPE], db, ctx) +# if parser.has_key(TOPOLOGY): + if TOPOLOGY in parser: + db = _parse_topology_template(parser[TOPOLOGY], db, ctx) +# if parser.has_key(CAPABILITY_TYPE): + if CAPABILITY_TYPE in parser: + db = _parse_capability_type(parser[CAPABILITY_TYPE], db, ctx) +# if parser.has_key(RELATIONSHIP_TYPE): + if RELATIONSHIP_TYPE in parser: + db = _parse_relationship_type(parser[RELATIONSHIP_TYPE], db, ctx) + + db._parse_objects() + + return db + +def _parse_data_type(data_type_section, db, ctx): + if data_type_section is None: + return db + if db is None: + db = ToscaDB() + + for data_type_name in data_type_section.keys(): + db._import_data_type(DataType(data_type_name, data_type_section[data_type_name])) + return db + +def _parse_node_type(node_type_section, db, ctx): + if node_type_section is None: + return db + if db is None: + db = ToscaDB() + + for node_type_def_name in node_type_section.keys(): + db._import_node_type(NodeType(node_type_def_name, node_type_section[node_type_def_name])) + return db + +def _parse_capability_type(cap_type_section, db, ctx): + if cap_type_section is None: + return db + if db is None: + db = ToscaDB() + + for cap_type_def_name in cap_type_section.keys(): + db._import_capability_type(CapabilityType(cap_type_def_name, cap_type_section[cap_type_def_name])) + return db + +def _parse_relationship_type(rel_type_section, db, ctx): + if rel_type_section is None: + return db + if db is None: + db = ToscaDB() + + for rel_type_def_name in rel_type_section.keys(): + db._import_relationship_type(RelationshipType(rel_type_def_name,rel_type_section[rel_type_def_name])) + return db + +def _parse_topology_template(topology_section, db, ctx): + if topology_section is None: + return db + if db is None: + db = ToscaDB() + + if ctx.metadata is None or ctx.metadata['template_name'] is None or ctx.metadata['template_name']=='': + template_key = ctx.curr_file_name + else: + template_key = ctx.metadata['template_name'] + + if template_key is None or template_key == '': + index = 0 + while True: +# if db.TEMPLATES.has_key('template'+str(index)): + if 'template'+str(index) in db.TEMPLATES: + index+=1 + continue + break + template_key = 'template'+str(index) + + ctx.temp_name = template_key + + new_topology = ToscaTopology(template_key, ctx.metadata, topology_section) + new_topology.extra_imports = ctx.extra_imports + db._import_template(new_topology) + + return db + +def _parse_requirement_name_and_value(content): + list_size = len(content.keys()) + if list_size != 1: + logging.warning( 'Requirement section does not have exact one element: '+ list_size) + return + ck = list(content.keys())[0] + return ck, content[ck] + +def _parse_import(file_imported, import_section, db, ctx): + if db is None: + db = ToscaDB() + + if import_section is None: + return db + + for new_file_sec in import_section: +# for new_file in new_file_sec.itervalues(): + for new_file in iter(new_file_sec.values()): + curr_path_bk = ctx.curr_path + curr_filename_bk = ctx.curr_file_name + _file_import(file_imported, new_file, db, ctx) + ctx.curr_path = curr_path_bk + ctx.curr_file_name = curr_filename_bk + + return db diff --git a/app/toscalib/utils/tosca_import.pyc b/app/toscalib/utils/tosca_import.pyc Binary files differnew file mode 100644 index 0000000..b86dfa7 --- /dev/null +++ b/app/toscalib/utils/tosca_import.pyc diff --git a/app/toscalib/utils/tosca_operate.py b/app/toscalib/utils/tosca_operate.py new file mode 100644 index 0000000..bc67310 --- /dev/null +++ b/app/toscalib/utils/tosca_operate.py @@ -0,0 +1,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] +
\ No newline at end of file diff --git a/app/toscalib/utils/tosca_operate.pyc b/app/toscalib/utils/tosca_operate.pyc Binary files differnew file mode 100644 index 0000000..4ebd264 --- /dev/null +++ b/app/toscalib/utils/tosca_operate.pyc diff --git a/app/toscalib/utils/tosca_print.py b/app/toscalib/utils/tosca_print.py new file mode 100644 index 0000000..a7205e3 --- /dev/null +++ b/app/toscalib/utils/tosca_print.py @@ -0,0 +1,58 @@ +#Author: Shu Shi +#emaiL: shushi@research.att.com + + +LEVEL_NODE_NAME = 1 +LEVEL_NODE_DETAILS = 2 +LEVEL_NODE_EVERYTHING = 3 + +def _print_template(template, level=LEVEL_NODE_DETAILS): + print_str = '' + if template is None: + return print_str + print_str += 'Nodes:'+ '\n' + for node in iter(template.node_dict.values()): + print_str += _print_node(node, level) + return print_str + +def _print_node(node, level): + print_str = '' + if node is None: + return + print_str += ' '+ node.name + '\n' + print_str += ' type: '+ node.type+ '\n' + if level == LEVEL_NODE_DETAILS: + if len(node.properties) > 0: + print_str += ' properties:' + '\n' + for prop in iter(node.properties.values()): + if prop.filled: + print_str += ' '+ prop.name+ ': '+ str(prop.value._get_value()[0])+ '\n' + else: + print_str += ' '+ prop.name+ ': null'+ '\n' + if len(node.requirements)> 0: + print_str += ' requirements:'+ '\n' + for req in node.requirements: + if req.filled: + print_str += ' '+ req.name+ ': '+ req.value.name+ '\n' + else: + print_str += ' '+ req.name+ ': null'+ '\n' + print_str += ''+ '\n' + return print_str + + +def _print_node_types(db): + print_str = 'Available node types: '+ '\n' + for name in db.NODE_TYPES.keys(): + print_str += name, '\n' + return print_str + +def _print_templates(db): + print_str = 'Available templates: '+ '\n' + for name in db.TEMPLATES.keys(): + print_str += name, '\n' + return print_str + +def _print_node_type(node_type): + print_str = 'Node Type Definition: '+ node_type.name+ '\n' + print_str += node_type.raw_content+ '\n' + return print_str diff --git a/app/toscalib/utils/tosca_print.pyc b/app/toscalib/utils/tosca_print.pyc Binary files differnew file mode 100644 index 0000000..7c39616 --- /dev/null +++ b/app/toscalib/utils/tosca_print.pyc diff --git a/app/toscalib/utils/validateutils.py b/app/toscalib/utils/validateutils.py new file mode 100644 index 0000000..dca08b1 --- /dev/null +++ b/app/toscalib/utils/validateutils.py @@ -0,0 +1,81 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import collections +import dateutil.parser +import numbers + +import six + +#from toscalib.utils.gettextutils import _ + + +def str_to_num(value): + '''Convert a string representation of a number into a numeric type.''' + if isinstance(value, numbers.Number): + return value + try: + return int(value) + except ValueError: + return float(value) + + +def validate_number(value): + return str_to_num(value) + + +def validate_integer(value): + if not isinstance(value, int): + try: + value = int(value) + except Exception: + raise ValueError(_('"%s" is not an integer') % value) + return value + + +def validate_float(value): + if not isinstance(value, float): + raise ValueError(_('"%s" is not a float') % value) + return validate_number(value) + + +def validate_string(value): + if not isinstance(value, six.string_types): + raise ValueError(_('"%s" is not a string') % value) + return value + + +def validate_list(value): + if not isinstance(value, list): + raise ValueError(_('"%s" is not a list') % value) + return value + + +def validate_map(value): + if not isinstance(value, collections.Mapping): + raise ValueError(_('"%s" is not a map') % value) + return value + + +def validate_boolean(value): + if isinstance(value, bool): + return value + + if isinstance(value, str): + normalised = value.lower() + if normalised in ['true', 'false']: + return normalised == 'true' + raise ValueError(_('"%s" is not a boolean') % value) + + +def validate_timestamp(value): + return dateutil.parser.parse(value) diff --git a/app/toscalib/utils/yamlparser.py b/app/toscalib/utils/yamlparser.py new file mode 100644 index 0000000..e54227d --- /dev/null +++ b/app/toscalib/utils/yamlparser.py @@ -0,0 +1,63 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import codecs +from collections import OrderedDict + +import yaml + + +if hasattr(yaml, 'CSafeLoader'): + yaml_loader = yaml.CSafeLoader +else: + yaml_loader = yaml.SafeLoader + + +def load_yaml(path): + with codecs.open(path, encoding='utf-8', errors='strict') as f: + return yaml.load(f.read(), Loader=yaml_loader) + + +def simple_parse(tmpl_str): + try: + tpl = yaml.load(tmpl_str, Loader=yaml_loader) + except yaml.YAMLError as yea: + raise ValueError(yea) + else: + if tpl is None: + tpl = {} + return tpl + + +def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict): + class OrderedLoader(Loader): + pass + + def construct_mapping(loader, node): + loader.flatten_mapping(node) + return object_pairs_hook(loader.construct_pairs(node)) + + OrderedLoader.add_constructor( + yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, + construct_mapping) + return yaml.load(stream, OrderedLoader) + + +def simple_ordered_parse(tmpl_str): + try: + tpl = ordered_load(tmpl_str) + except yaml.YAMLError as yea: + raise ValueError(yea) + else: + if tpl is None: + tpl = {} + return tpl diff --git a/app/toscalib/utils/yamlparser.pyc b/app/toscalib/utils/yamlparser.pyc Binary files differnew file mode 100644 index 0000000..8d97b2a --- /dev/null +++ b/app/toscalib/utils/yamlparser.pyc |