Commit 9b8e13e4 authored by Spiros Koulouzis's avatar Spiros Koulouzis

analyze requirements

parent bf40680e
......@@ -3,6 +3,8 @@ import toscaparser.utils.yamlparser
import re
import operator
import json
import urllib
import urllib.parse
class DumpPlanner:
......@@ -11,13 +13,29 @@ class DumpPlanner:
node_template_names = ['nodeTemplates']
requirement_names = ['requirements']
type_names = ['type']
relationships_names = ['relationshipTemplates']
service_templates = None
topology_templates = None
node_templates = None
requirements = {}
def __init__(self, service_templaete_file_path):
def __init__(self, tosca_reposetory_api_base_url,namespace,servicetemplate_id):
# dict_tpl = self.load_file(service_templaete_file_path)
self.tosca_reposetory_api_base_url = tosca_reposetory_api_base_url
servicetemplate_url = tosca_reposetory_api_base_url+"/servicetemplates/"+namespace+"/"+servicetemplate_id+"/"
header={'accept': 'application/json'}
req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET')
res = urllib.request.urlopen(req, timeout=5)
res_body = res.read()
dict_tpl = json.loads(res_body.decode("utf-8"))
dict_tpl = self.load_file(service_templaete_file_path)
requirements = self.get_all_requirements(dict_tpl)
unmet_requirements = self.get_unmet_requirements(requirements)
print(unmet_requirements)
service_templates = self.get_service_template(dict_tpl)
req = self.get_all_requirements(service_templates)
print(req)
# relationships = self.get_all_relationships(dict_tpl)
# print(relationships)
# unmet_requirements = self.get_unmet_requirements(requirements)
# print(unmet_requirements)
# yaml_dict_tpl = self.trnsform_to_tosca(yaml_dict_tpl)
......@@ -47,37 +65,87 @@ class DumpPlanner:
def handle_tosca_exeption(self,exception,yaml_dict_tpl):
print(exception)
def get_all_requirements(self,dict_tpl):
all_requirements = []
service_templates = self.get_service_template(dict_tpl)
for service in service_templates:
topology_template = self.get_topology_template(service)
node_templates = self.get_node_templates(topology_template)
for node_template in node_templates:
requirements = self.get_requirements(node_template)
if requirements:
for requirement in requirements['requirement']:
all_requirements.append(requirement)
return all_requirements
def get_all_requirements(self,service_templates):
if (not self.requirements):
for service in service_templates:
topology_template = self.get_topology_template(service)
node_templates = self.get_node_templates(topology_template)
for node_template in node_templates:
node_type = node_template['type']
all_requirements = self.get_super_types_requirements(node_type,'nodetypes',None)
id = node_template['id']
req = self.get_requirements(node_template)
if(req):
for r in req['requirement']:
all_requirements.add(r['type'])
self.requirements[id] = all_requirements
return self.requirements
def get_unmet_requirements(self,requirements):
for requirement in requirements:
requirement_type = self.get_requirement_type(requirement)
print(requirement_type)
def get_all_relationships(self,dict_tpl):
all_relationships = []
service_templates = self.get_service_template(dict_tpl)
for service in service_templates:
topology_template = self.get_topology_template(service)
relationships = self.get_relationships(topology_template)
all_relationships.append(relationships)
return all_relationships
def get_super_types_requirements(self,component_type,type_name,requirements):
if (requirements == None):
requirements = set()
regex = r"\{(.*?)\}"
matches = re.finditer(regex, component_type, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1)
id = component_type.replace("{"+namespace+"}", "")
header={'accept': 'application/json'}
#winery needs it double percent-encoded
encoded_namespace = urllib.parse.quote(namespace, safe='')
encoded_namespace = urllib.parse.quote(encoded_namespace, safe='')
def get_service_template(self,dict_tpl):
return self.find(dict_tpl,self.service_template_names)
servicetemplate_url = self.tosca_reposetory_api_base_url+"/"+type_name+"/"+encoded_namespace+"/"+id+"/"
req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET')
res = urllib.request.urlopen(req, timeout=5)
res_body = res.read()
component = json.loads(res_body.decode("utf-8"))
for c in component['serviceTemplateOrNodeTypeOrNodeTypeImplementation']:
if 'requirementDefinitions' in c and 'requirementDefinition' in c['requirementDefinitions']:
for req in c['requirementDefinitions']['requirementDefinition']:
requirements.add(req['requirementType'])
if 'derivedFrom' in c and c['derivedFrom'] and c['derivedFrom']['type']:
self.get_super_types_requirements(c['derivedFrom']['type'],type_name,requirements)
return requirements
def get_service_template(self,dict_tpl):
if(not self.service_templates):
self.service_templates = self.find(dict_tpl,self.service_template_names)
return self.service_templates
def get_topology_template(self,dict_tpl):
return self.find(dict_tpl,self.topology_template_names)
if (not self.topology_templates):
self.topology_templates = self.find(dict_tpl,self.topology_template_names)
return self.topology_templates
def get_node_templates(self,dict_tpl):
return self.find(dict_tpl,self.node_template_names)
def get_node_templates(self,dict_tpl):
if (not self.node_templates):
self.node_templates = self.find(dict_tpl,self.node_template_names)
return self.node_templates
def get_requirements(self,dict_tpl):
return self.find(dict_tpl,self.requirement_names)
return self.find(dict_tpl,self.requirement_names)
def get_relationships(self,dict_tpl):
return self.find(dict_tpl,self.relationships_names)
def get_requirement_type(self,dict_req):
return self.find(dict_req,self.type_names)
......
......@@ -13,6 +13,7 @@ import sys
import tempfile
import time
logger = logging.getLogger(__name__)
if not getattr(logger, 'handler_set', None):
logger.setLevel(logging.INFO)
......@@ -111,7 +112,10 @@ def handle_delivery(message):
if __name__ == "__main__":
if(sys.argv[1] == "test_local"):
home = expanduser("~")
planner = DumpPlanner(home+"/Downloads/topology.json")
tosca_reposetory_api_base_url = "http://localhost:8080/winery"
namespace = "http%253A%252F%252Fsne.uva.nl%252Fservicetemplates"
servicetemplate_id = "wordpress_w1-wip1"
planner = DumpPlanner(tosca_reposetory_api_base_url,namespace,servicetemplate_id)
else:
logger.info("Input args: " + sys.argv[0] + ' ' + sys.argv[1] + ' ' + sys.argv[2])
channel = init_chanel(sys.argv)
......
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