Commit e9c010de authored by Spiros Koulouzis's avatar Spiros Koulouzis

we have a bug with setting default properties

parent d6898999
...@@ -208,7 +208,7 @@ class BasicPlanner: ...@@ -208,7 +208,7 @@ class BasicPlanner:
else: else:
logging.info('The TOSCA template in: ' + path + ' has no requirements') logging.info('The TOSCA template in: ' + path + ' has no requirements')
tp = TOSCAParser() tp = TOSCAParser()
yaml_str = tp.tosca_template2_yaml(self.template) yaml_str = tosca_template2_yaml(self.template)
yaml_str = fix_duplicate_vm_names(yaml_str) yaml_str = fix_duplicate_vm_names(yaml_str)
yaml_str = yaml_str.replace('tosca_definitions_version: tosca_simple_yaml_1_0', '') yaml_str = yaml_str.replace('tosca_definitions_version: tosca_simple_yaml_1_0', '')
yaml_str = yaml_str.replace('description: TOSCA example', '') yaml_str = yaml_str.replace('description: TOSCA example', '')
......
...@@ -7,9 +7,9 @@ import operator ...@@ -7,9 +7,9 @@ import operator
from src.planner.simple_spec_alayzer import SimpleAnalyzer from src.planner.simple_spec_alayzer import SimpleAnalyzer
from src.planner.spec_service import * from src.planner.spec_service import *
from src.utils.tosca import * from src.utils.tosca_helper import *
from src.planner.specification_analyzer import * from src.planner.specification_analyzer import *
from src.utils import tosca as tosca_util from src.utils import tosca_helper as tosca_util
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
......
...@@ -3,7 +3,7 @@ import copy ...@@ -3,7 +3,7 @@ import copy
from toscaparser.nodetemplate import NodeTemplate from toscaparser.nodetemplate import NodeTemplate
from toscaparser.properties import Property from toscaparser.properties import Property
from src.utils import tosca as tosca_util from src.utils import tosca_helper as tosca_util
from src.planner.specification_analyzer import SpecificationAnalyzer from src.planner.specification_analyzer import SpecificationAnalyzer
import networkx as nx import networkx as nx
import logging import logging
......
...@@ -3,7 +3,7 @@ from abc import abstractmethod, ABCMeta ...@@ -3,7 +3,7 @@ from abc import abstractmethod, ABCMeta
from toscaparser.tosca_template import ToscaTemplate from toscaparser.tosca_template import ToscaTemplate
import networkx as nx import networkx as nx
from src.utils import tosca as tosca_util from src.utils import tosca_helper as tosca_util
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
......
...@@ -13,7 +13,6 @@ import yaml ...@@ -13,7 +13,6 @@ import yaml
from planner.basic_planner import * from planner.basic_planner import *
from planner.planner import * from planner.planner import *
from planner.spec_service import SpecService from planner.spec_service import SpecService
from utils import tosca as tosca_util
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -112,14 +111,14 @@ if __name__ == "__main__": ...@@ -112,14 +111,14 @@ if __name__ == "__main__":
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
if sys.argv[1] == "test_local": if sys.argv[1] == "test_local":
tosca_path = "../../TOSCA/" tosca_path = "../../TOSCA/"
input_tosca_file_path = tosca_path + '/application_example.yaml' input_tosca_file_path = tosca_path + '/application_example_2_topologies.yaml'
conf = {'url': "http://host"} conf = {'url': "http://host"}
spec_service = SpecService(conf) spec_service = SpecService(conf)
test_planner = Planner(input_tosca_file_path, spec_service) test_planner = Planner(input_tosca_file_path, spec_service)
test_tosca_template = test_planner.resolve_requirements() test_tosca_template = test_planner.resolve_requirements()
test_tosca_template = test_planner.set_infrastructure_specifications() test_tosca_template = test_planner.set_infrastructure_specifications()
template_dict = tosca_util.get_tosca_template_2_topology_template_dictionary(test_tosca_template) template_dict = tosca_util.get_tosca_template_2_topology_template_dictionary(test_tosca_template)
logger.info("template ----: \n" + yaml.dump(template)) # logger.info("template ----: \n" + yaml.dump(template))
try: try:
tosca_folder_path = os.path.join(tempfile.gettempdir(), tosca_path) tosca_folder_path = os.path.join(tempfile.gettempdir(), tosca_path)
......
...@@ -8,7 +8,7 @@ import unittest ...@@ -8,7 +8,7 @@ import unittest
from src.planner.planner import * from src.planner.planner import *
from src.planner.spec_service import SpecService from src.planner.spec_service import SpecService
from src.utils import tosca as tosca_util from src.utils import tosca_helper as tosca_util
class MyTestCase(unittest.TestCase): class MyTestCase(unittest.TestCase):
......
import json
import operator
import pdb
import re
from toscaparser.tosca_template import ToscaTemplate
from toscaparser.tosca_template import TopologyTemplate
from toscaparser.elements.nodetype import NodeType
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.utils import yamlparser
import urllib
import urllib.parse
import sys
import pdb
import names
import yaml
# TOSCA template key names
SECTIONS = (DEFINITION_VERSION, DEFAULT_NAMESPACE, TEMPLATE_NAME,
TOPOLOGY_TEMPLATE, TEMPLATE_AUTHOR, TEMPLATE_VERSION,
DESCRIPTION, IMPORTS, DSL_DEFINITIONS, NODE_TYPES,
RELATIONSHIP_TYPES, RELATIONSHIP_TEMPLATES,
CAPABILITY_TYPES, ARTIFACT_TYPES, DATA_TYPES, INTERFACE_TYPES,
POLICY_TYPES, GROUP_TYPES, REPOSITORIES,INPUTS,NODE_TEMPLATES,
OUTPUTS,GROUPS,SUBSTITUION_MAPPINGS,POLICIES,TYPE,REQUIREMENTS,
ARTIFACTS,PROPERTIES,INTERFACES) = \
('tosca_definitions_version', 'tosca_default_namespace',
'template_name', 'tosca_template', 'template_author',
'template_version', 'description', 'imports', 'dsl_definitions',
'node_types', 'relationship_types', 'relationship_templates',
'capability_types', 'artifact_types', 'data_types',
'interface_types', 'policy_types', 'group_types', 'repositories',
'inputs','node_templates','outputs','groups','substitution_mappings',
'policies','type','requirements','artifacts','properties','interfaces')
class TOSCAParser:
def tosca_template2_yaml(self, tosca_template):
topology_dict = {}
topology_dict[DEFINITION_VERSION] = tosca_template.version
topology_dict[IMPORTS] = tosca_template._tpl_imports()
topology_dict[DESCRIPTION] = tosca_template.description
topology_dict[TOPOLOGY_TEMPLATE] = {}
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES] = {}
node_templates = tosca_template.nodetemplates
for node_template in node_templates:
node_template_dict = self.get_node_template_dict(node_template)
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES][node_template.name] = node_template_dict
# If we don't add this then dump uses references for the same dictionary entries i.e. '&id001'
yaml.Dumper.ignore_aliases = lambda *args : True
return (yaml.dump(topology_dict,default_flow_style=False))
def get_node_template_dict(self,node_template):
node_template_dict = {}
node_template_dict[TYPE] = node_template.type
# node_template_dict[REQUIREMENTS] = {}
if node_template.requirements:
node_template_dict[REQUIREMENTS] = node_template.requirements
# if node_template.interfaces:
# interfaces = {}
# for interface in node_template.interfaces:
# interfaces[interface.type] = {}
# interfaces[interface.type][interface.name] = interface.implementation
# print( node_template.templates[node_template.name] )
if ARTIFACTS in node_template.templates[node_template.name].keys():
node_template_dict[ARTIFACTS] = node_template.templates[node_template.name][ARTIFACTS]
if PROPERTIES in node_template.templates[node_template.name].keys():
node_template_dict[PROPERTIES] = node_template.templates[node_template.name][PROPERTIES]
if INTERFACES in node_template.templates[node_template.name].keys():
node_template_dict[INTERFACES] = node_template.templates[node_template.name][INTERFACES]
# print(dir(node_template))
# print(node_template.templates)
return node_template_dict
\ No newline at end of file
import yaml
# TOSCA template key names
SECTIONS = (DEFINITION_VERSION, DEFAULT_NAMESPACE, TEMPLATE_NAME,
TOPOLOGY_TEMPLATE, TEMPLATE_AUTHOR, TEMPLATE_VERSION,
DESCRIPTION, IMPORTS, DSL_DEFINITIONS, NODE_TYPES,
RELATIONSHIP_TYPES, RELATIONSHIP_TEMPLATES,
CAPABILITY_TYPES, ARTIFACT_TYPES, DATA_TYPES, INTERFACE_TYPES,
POLICY_TYPES, GROUP_TYPES, REPOSITORIES, INPUTS, NODE_TEMPLATES,
OUTPUTS, GROUPS, SUBSTITUION_MAPPINGS, POLICIES, TYPE, REQUIREMENTS,
ARTIFACTS, PROPERTIES, INTERFACES) = \
('tosca_definitions_version', 'tosca_default_namespace',
'template_name', 'tosca_template', 'template_author',
'template_version', 'description', 'imports', 'dsl_definitions',
'node_types', 'relationship_types', 'relationship_templates',
'capability_types', 'artifact_types', 'data_types',
'interface_types', 'policy_types', 'group_types', 'repositories',
'inputs', 'node_templates', 'outputs', 'groups', 'substitution_mappings',
'policies', 'type', 'requirements', 'artifacts', 'properties', 'interfaces')
def get_node_template_dict(node_template):
node_template_dict = {TYPE: node_template.type}
# node_template_dict[REQUIREMENTS] = {}
if node_template.requirements:
node_template_dict[REQUIREMENTS] = node_template.requirements
# if node_template.interfaces:
# interfaces = {}
# for interface in node_template.interfaces:
# interfaces[interface.type] = {}
# interfaces[interface.type][interface.name] = interface.implementation
# print( node_template.templates[node_template.name] )
if ARTIFACTS in node_template.templates[node_template.name].keys():
node_template_dict[ARTIFACTS] = node_template.templates[node_template.name][ARTIFACTS]
if PROPERTIES in node_template.templates[node_template.name].keys():
node_template_dict[PROPERTIES] = node_template.templates[node_template.name][PROPERTIES]
if INTERFACES in node_template.templates[node_template.name].keys():
node_template_dict[INTERFACES] = node_template.templates[node_template.name][INTERFACES]
# print(dir(node_template))
# print(node_template.templates)
return node_template_dict
def tosca_template2_yaml(tosca_template):
topology_dict = {DEFINITION_VERSION: tosca_template.version, IMPORTS: tosca_template._tpl_imports(),
DESCRIPTION: tosca_template.description, TOPOLOGY_TEMPLATE: {}}
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES] = {}
node_templates = tosca_template.nodetemplates
for node_template in node_templates:
node_template_dict = get_node_template_dict(node_template)
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES][node_template.name] = node_template_dict
# If we don't add this then dump uses references for the same dictionary entries i.e. '&id001'
yaml.Dumper.ignore_aliases = lambda *args: True
return yaml.dump(topology_dict, default_flow_style=False)
...@@ -5,10 +5,26 @@ from toscaparser import tosca_template ...@@ -5,10 +5,26 @@ from toscaparser import tosca_template
from toscaparser.elements.nodetype import NodeType from toscaparser.elements.nodetype import NodeType
from toscaparser.nodetemplate import NodeTemplate from toscaparser.nodetemplate import NodeTemplate
from src.utils.TOSCA_parser import TOSCAParser
import yaml import yaml
import logging import logging
# TOSCA template key names
SECTIONS = (DEFINITION_VERSION, DEFAULT_NAMESPACE, TEMPLATE_NAME,
TOPOLOGY_TEMPLATE, TEMPLATE_AUTHOR, TEMPLATE_VERSION,
DESCRIPTION, IMPORTS, DSL_DEFINITIONS, NODE_TYPES,
RELATIONSHIP_TYPES, RELATIONSHIP_TEMPLATES,
CAPABILITY_TYPES, ARTIFACT_TYPES, DATA_TYPES, INTERFACE_TYPES,
POLICY_TYPES, GROUP_TYPES, REPOSITORIES, INPUTS, NODE_TEMPLATES,
OUTPUTS, GROUPS, SUBSTITUION_MAPPINGS, POLICIES, TYPE, REQUIREMENTS,
ARTIFACTS, PROPERTIES, INTERFACES) = \
('tosca_definitions_version', 'tosca_default_namespace',
'template_name', 'tosca_template', 'template_author',
'template_version', 'description', 'imports', 'dsl_definitions',
'node_types', 'relationship_types', 'relationship_templates',
'capability_types', 'artifact_types', 'data_types',
'interface_types', 'policy_types', 'group_types', 'repositories',
'inputs', 'node_templates', 'outputs', 'groups', 'substitution_mappings',
'policies', 'type', 'requirements', 'artifacts', 'properties', 'interfaces')
node_type_key_names_to_remove = ['capabilities', 'derived_from'] node_type_key_names_to_remove = ['capabilities', 'derived_from']
...@@ -58,7 +74,7 @@ def get_node_type_requirements(type_name, all_nodes): ...@@ -58,7 +74,7 @@ def get_node_type_requirements(type_name, all_nodes):
return None return None
def get_ancestors_requirements(node, all_nodes, all_custom_def, parent_requirements=None): def get_all_ancestors_requirements(node, all_node_types, all_custom_def, parent_requirements=None):
"""Recursively get all requirements all the way to the ROOT including the input node's""" """Recursively get all requirements all the way to the ROOT including the input node's"""
if not parent_requirements: if not parent_requirements:
parent_requirements = [] parent_requirements = []
...@@ -71,13 +87,14 @@ def get_ancestors_requirements(node, all_nodes, all_custom_def, parent_requireme ...@@ -71,13 +87,14 @@ def get_ancestors_requirements(node, all_nodes, all_custom_def, parent_requireme
parent_requirements.extend(node.parent_type.requirements) parent_requirements.extend(node.parent_type.requirements)
# Make parent type to NodeTemplate to continue # Make parent type to NodeTemplate to continue
if node.parent_type.type: if node.parent_type.type:
parent_template = node_type_2_node_template({'name': all_nodes[node.parent_type.type]}, all_custom_def) parent_template = node_type_2_node_template({'name': all_node_types[node.parent_type.type]},
all_custom_def)
if parent_template: if parent_template:
get_ancestors_requirements(parent_template, all_nodes, parent_requirements) get_all_ancestors_requirements(parent_template, all_node_types, parent_requirements)
elif isinstance(node, dict): elif isinstance(node, dict):
node_type_name = get_node_type_name(node) node_type_name = get_node_type_name(node)
node_template = node_type_2_node_template({'name': all_nodes[node_type_name]}, all_custom_def) node_template = node_type_2_node_template({'name': all_node_types[node_type_name]}, all_custom_def)
return get_ancestors_requirements(node_template, all_nodes, all_custom_def, parent_requirements) return get_all_ancestors_requirements(node_template, all_node_types, all_custom_def, parent_requirements)
return parent_requirements return parent_requirements
...@@ -119,8 +136,7 @@ def node_type_2_node_template(node_type, all_custom_def): ...@@ -119,8 +136,7 @@ def node_type_2_node_template(node_type, all_custom_def):
def get_tosca_template_2_topology_template_dictionary(template): def get_tosca_template_2_topology_template_dictionary(template):
tp = TOSCAParser() yaml_str = tosca_template2_yaml(template)
yaml_str = tp.tosca_template2_yaml(template)
tosca_template_dict = yaml.load(yaml_str, Loader=yaml.FullLoader) tosca_template_dict = yaml.load(yaml_str, Loader=yaml.FullLoader)
this_tosca_template = tosca_template_dict['tosca_template'] this_tosca_template = tosca_template_dict['tosca_template']
tosca_template_dict.pop('tosca_template') tosca_template_dict.pop('tosca_template')
...@@ -181,7 +197,7 @@ def get_all_ancestors_types(child_node, all_node_types, all_custom_def, ancestor ...@@ -181,7 +197,7 @@ def get_all_ancestors_types(child_node, all_node_types, all_custom_def, ancestor
return ancestors_types return ancestors_types
def get_all_ancestors_properties(node, all_nodes, all_custom_def, ancestors_properties=None, ancestors_types=None): def get_all_ancestors_properties(node, all_nodes_templates, all_custom_def, ancestors_properties=None, ancestors_types=None):
if not ancestors_properties: if not ancestors_properties:
ancestors_properties = [] ancestors_properties = []
ancestors_properties_names = [] ancestors_properties_names = []
...@@ -191,10 +207,10 @@ def get_all_ancestors_properties(node, all_nodes, all_custom_def, ancestors_prop ...@@ -191,10 +207,10 @@ def get_all_ancestors_properties(node, all_nodes, all_custom_def, ancestors_prop
node_prop_names.append(node_prop.name) node_prop_names.append(node_prop.name)
ancestors_properties.append(node_prop) ancestors_properties.append(node_prop)
if not ancestors_types: if not ancestors_types:
ancestors_types = get_all_ancestors_types(node, all_nodes, all_custom_def) ancestors_types = get_all_ancestors_types(node, all_nodes_templates, all_custom_def)
for ancestors_type in ancestors_types: for ancestors_type in ancestors_types:
ancestor = node_type_2_node_template({'name': all_nodes[ancestors_type]}, all_custom_def) ancestor = node_type_2_node_template({'name': all_nodes_templates[ancestors_type]}, all_custom_def)
if ancestor.get_properties_objects(): if ancestor.get_properties_objects():
for ancestor_prop in ancestor.get_properties_objects(): for ancestor_prop in ancestor.get_properties_objects():
if ancestor_prop.name not in ancestors_properties_names and ancestor_prop.name not in node_prop_names: if ancestor_prop.name not in ancestors_properties_names and ancestor_prop.name not in node_prop_names:
...@@ -213,3 +229,41 @@ def get_nodes_with_occurrences_in_requirements(topology_nodes): ...@@ -213,3 +229,41 @@ def get_nodes_with_occurrences_in_requirements(topology_nodes):
nodes_with_occurrences_in_requirement.append(node) nodes_with_occurrences_in_requirement.append(node)
break break
return nodes_with_occurrences_in_requirement return nodes_with_occurrences_in_requirement
def tosca_template2_yaml(tosca_template):
topology_dict = {DEFINITION_VERSION: tosca_template.version, IMPORTS: tosca_template._tpl_imports(),
DESCRIPTION: tosca_template.description, TOPOLOGY_TEMPLATE: {}}
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES] = {}
node_templates = tosca_template.nodetemplates
for node_template in node_templates:
node_template_dict = get_node_template_dict(node_template)
topology_dict[TOPOLOGY_TEMPLATE][NODE_TEMPLATES][node_template.name] = node_template_dict
# If we don't add this then dump uses references for the same dictionary entries i.e. '&id001'
yaml.Dumper.ignore_aliases = lambda *args: True
return yaml.dump(topology_dict, default_flow_style=False)
def get_node_template_dict(node_template):
node_template_dict = {TYPE: node_template.type}
# node_template_dict[REQUIREMENTS] = {}
if node_template.requirements:
node_template_dict[REQUIREMENTS] = node_template.requirements
# if node_template.interfaces:
# interfaces = {}
# for interface in node_template.interfaces:
# interfaces[interface.type] = {}
# interfaces[interface.type][interface.name] = interface.implementation
# print( node_template.templates[node_template.name] )
if ARTIFACTS in node_template.templates[node_template.name].keys():
node_template_dict[ARTIFACTS] = node_template.templates[node_template.name][ARTIFACTS]
if PROPERTIES in node_template.templates[node_template.name].keys():
node_template_dict[PROPERTIES] = node_template.templates[node_template.name][PROPERTIES]
if INTERFACES in node_template.templates[node_template.name].keys():
node_template_dict[INTERFACES] = node_template.templates[node_template.name][INTERFACES]
# print(dir(node_template))
# print(node_template.templates)
return node_template_dict
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment