Commit 602c11f5 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Implemented winery service. gets topology template nodes and node parents

parent 46193571
...@@ -24,4 +24,7 @@ ...@@ -24,4 +24,7 @@
/drip-provisioner (copy)/target/ /drip-provisioner (copy)/target/
/drip_parser.tar.gz /drip_parser.tar.gz
/docs/playbooks/set_dockerhub_credentials.yml /docs/playbooks/set_dockerhub_credentials.yml
/drip_planner2/src/planner/__pycache__/ /drip_planner2/src/planner/__pycache__/
\ No newline at end of file /winery_service/nbproject/private/
/winery_service/src/winery/__pycache__/
/tosca_service/nbproject/private/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project> <?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty> <pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">python3.6</pydev_property>
</pydev_project> </pydev_project>
import json
import operator
import pdb
import re
from toscaparser.tosca_template import ToscaTemplate from toscaparser.tosca_template import ToscaTemplate
import toscaparser.utils.yamlparser import toscaparser.utils.yamlparser
import re
import operator
import json
import urllib import urllib
import urllib.parse import urllib.parse
class DumpPlanner: class DumpPlanner:
service_template_names = ['serviceTemplateOrNodeTypeOrNodeTypeImplementation'] service_template_names = ['serviceTemplateOrNodeTypeOrNodeTypeImplementation']
topology_template_names = ['topologyTemplate'] topology_template_names = ['topologyTemplate']
...@@ -19,11 +19,11 @@ class DumpPlanner: ...@@ -19,11 +19,11 @@ class DumpPlanner:
node_templates = None node_templates = None
requirements = {} requirements = {}
def __init__(self, tosca_reposetory_api_base_url,namespace,servicetemplate_id): def __init__(self, tosca_reposetory_api_base_url, namespace, servicetemplate_id):
# dict_tpl = self.load_file(service_templaete_file_path) # dict_tpl = self.load_file(service_templaete_file_path)
self.tosca_reposetory_api_base_url = tosca_reposetory_api_base_url self.tosca_reposetory_api_base_url = tosca_reposetory_api_base_url
servicetemplate_url = tosca_reposetory_api_base_url+"/servicetemplates/"+namespace+"/"+servicetemplate_id+"/" servicetemplate_url = tosca_reposetory_api_base_url + "/servicetemplates/" + namespace + "/" + servicetemplate_id + "/"
header={'accept': 'application/json'} header = {'accept': 'application/json'}
req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET') req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET')
res = urllib.request.urlopen(req, timeout=5) res = urllib.request.urlopen(req, timeout=5)
res_body = res.read() res_body = res.read()
...@@ -34,10 +34,10 @@ class DumpPlanner: ...@@ -34,10 +34,10 @@ class DumpPlanner:
relationships = self.get_all_relationships(dict_tpl) relationships = self.get_all_relationships(dict_tpl)
unmet_requirements = {} unmet_requirements = {}
for node_name in requirements: for node_name in requirements:
relationship = self.get_relationship_of_source_node(node_name,relationships) relationship = self.get_relationship_of_source_node(node_name, relationships)
if relationship: if relationship:
for requirement in requirements[node_name]: for requirement in requirements[node_name]:
if (not self.target_meets_requirement(relationship['targetElement']['ref'],requirement)): if (not self.target_meets_requirement(relationship['targetElement']['ref'], requirement)):
unmet_requirements[node_name] = requirement unmet_requirements[node_name] = requirement
# unmet_requirements = self.get_unmet_requirements(requirements) # unmet_requirements = self.get_unmet_requirements(requirements)
...@@ -46,7 +46,7 @@ class DumpPlanner: ...@@ -46,7 +46,7 @@ class DumpPlanner:
def load_file(self,path): def load_file(self, path):
is_json = True is_json = True
with open(path) as f: with open(path) as f:
try: try:
...@@ -62,56 +62,58 @@ class DumpPlanner: ...@@ -62,56 +62,58 @@ class DumpPlanner:
return toscaparser.utils.yamlparser.load_yaml(path) return toscaparser.utils.yamlparser.load_yaml(path)
def trnsform_to_tosca(self,yaml_dict_tpl): def trnsform_to_tosca(self, yaml_dict_tpl):
try: try:
self.tt = ToscaTemplate(path=None, yaml_dict_tpl=yaml_dict_tpl) self.tt = ToscaTemplate(path=None, yaml_dict_tpl=yaml_dict_tpl)
except Exception as e: except Exception as e:
self.handle_tosca_exeption(e,yaml_dict_tpl) self.handle_tosca_exeption(e, yaml_dict_tpl)
def handle_tosca_exeption(self,exception,yaml_dict_tpl): def handle_tosca_exeption(self, exception, yaml_dict_tpl):
print(exception) print(exception)
def target_meets_requirement(self,target_relationship_element,requirement): def target_meets_requirement(self, target_relationship_element, requirement):
node = self.get_node(target_relationship_element) node = self.get_node(target_relationship_element)
if 'capabilities' in node: if 'capabilities' in node:
print(node['capabilities']) print(node['capabilities'])
else: else:
supertypes = self.get_super_types(node['type'],None) supertypes = self.get_super_types(node['type'], None)
print(supertypes)
def get_relationship_of_source_node(self,source_id,relationships): def get_relationship_of_source_node(self, source_id, relationships):
for rel in relationships: for rel in relationships:
if(rel['sourceElement']['ref'] == source_id): if(rel['sourceElement']['ref'] == source_id):
return rel return rel
def get_node(self,node_type_id): def get_node(self, node_type_id):
for node in self.node_templates: for node in self.node_templates:
if node['id'] == node_type_id: if node['id'] == node_type_id:
return node return node
def get_all_requirements(self,service_templates): def get_all_requirements(self, service_templates):
if (not self.requirements): if (not self.requirements):
for service in service_templates: for service in service_templates:
topology_template = self.get_topology_template(service) topology_template = self.get_topology_template(service)
node_templates = self.get_node_templates(topology_template) node_templates = self.get_node_templates(topology_template)
for node_template in node_templates: for node_template in node_templates:
node_type = node_template['type'] node_type = node_template['type']
all_requirements = self.get_super_types_requirements(node_type,None) all_requirements = self.get_super_types_requirements(node_type, None)
print(all_requirements)
id = node_template['id'] id = node_template['id']
req = self.get_requirements(node_template) req = self.get_requirements(node_template)
if(req): if(req):
for r in req['requirement']: for r in req['requirement']:
all_requirements.add(r['type']) all_requirements.append(r['type'])
self.requirements[id] = all_requirements self.requirements[id] = all_requirements
return self.requirements return self.requirements
def get_unmet_requirements(self,requirements): def get_unmet_requirements(self, requirements):
for requirement in requirements: for requirement in requirements:
requirement_type = self.get_requirement_type(requirement) requirement_type = self.get_requirement_type(requirement)
print(requirement_type) print(requirement_type)
def get_all_relationships(self,dict_tpl): def get_all_relationships(self, dict_tpl):
all_relationships = [] all_relationships = []
service_templates = self.get_service_template(dict_tpl) service_templates = self.get_service_template(dict_tpl)
for service in service_templates: for service in service_templates:
...@@ -121,46 +123,48 @@ class DumpPlanner: ...@@ -121,46 +123,48 @@ class DumpPlanner:
all_relationships.append(rel) all_relationships.append(rel)
return all_relationships return all_relationships
def get_super_types(self,component_type,supertypes): def get_super_types(self, component_type, supertypes):
if (supertypes == None): if (supertypes == None):
supertypes = [] supertypes = []
regex = r"\{(.*?)\}" regex = r"\{(.*?)\}"
matches = re.finditer(regex, component_type, re.MULTILINE | re.DOTALL) matches = re.finditer(regex, component_type, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1) namespace = next(matches).group(1)
id = component_type.replace("{"+namespace+"}", "") id = component_type.replace("{" + namespace + "}", "")
header={'accept': 'application/json'} header = {'accept': 'application/json'}
#winery needs it double percent-encoded #winery needs it double percent-encoded
encoded_namespace = urllib.parse.quote(namespace, safe='') encoded_namespace = urllib.parse.quote(namespace, safe='')
encoded_namespace = urllib.parse.quote(encoded_namespace, safe='') encoded_namespace = urllib.parse.quote(encoded_namespace, safe='')
type_name = namespace.rsplit('/', 1)[-1] type_name = namespace.rsplit('/', 1)[-1]
servicetemplate_url = self.tosca_reposetory_api_base_url+"/"+type_name+"/"+encoded_namespace+"/"+id+"/" servicetemplate_url = self.tosca_reposetory_api_base_url + "/" + type_name + "/" + encoded_namespace + "/" + id + "/"
req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET') req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET')
res = urllib.request.urlopen(req, timeout=5) res = urllib.request.urlopen(req, timeout=5)
res_body = res.read() res_body = res.read()
component = json.loads(res_body.decode("utf-8")) component = json.loads(res_body.decode("utf-8"))
if component: if component:
supertypes.append(component) comp = component['serviceTemplateOrNodeTypeOrNodeTypeImplementation'][0]
print(component) supertypes.append(comp)
self.get_super_types(component['type'],supertypes) if 'derivedFrom' in comp:
else: supertypes = self.get_super_types(comp['derivedFrom']['typeRef'], supertypes)
return supertypes else:
return supertypes
def get_super_types_requirements(self,component_type,requirements): def get_super_types_requirements(self, component_type, requirements):
if (requirements == None): if (requirements == None):
requirements = set() requirements = []
regex = r"\{(.*?)\}" regex = r"\{(.*?)\}"
matches = re.finditer(regex, component_type, re.MULTILINE | re.DOTALL) matches = re.finditer(regex, component_type, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1) namespace = next(matches).group(1)
id = component_type.replace("{"+namespace+"}", "") id = component_type.replace("{" + namespace + "}", "")
header={'accept': 'application/json'} header = {'accept': 'application/json'}
#winery needs it double percent-encoded #winery needs it double percent-encoded
encoded_namespace = urllib.parse.quote(namespace, safe='') encoded_namespace = urllib.parse.quote(namespace, safe='')
encoded_namespace = urllib.parse.quote(encoded_namespace, safe='') encoded_namespace = urllib.parse.quote(encoded_namespace, safe='')
type_name = namespace.rsplit('/', 1)[-1] type_name = namespace.rsplit('/', 1)[-1]
servicetemplate_url = self.tosca_reposetory_api_base_url+"/"+type_name+"/"+encoded_namespace+"/"+id+"/" servicetemplate_url = self.tosca_reposetory_api_base_url + "/" + type_name + "/" + encoded_namespace + "/" + id + "/"
req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET') req = urllib.request.Request(url=servicetemplate_url, headers=header, method='GET')
res = urllib.request.urlopen(req, timeout=5) res = urllib.request.urlopen(req, timeout=5)
res_body = res.read() res_body = res.read()
...@@ -168,37 +172,37 @@ class DumpPlanner: ...@@ -168,37 +172,37 @@ class DumpPlanner:
for c in component['serviceTemplateOrNodeTypeOrNodeTypeImplementation']: for c in component['serviceTemplateOrNodeTypeOrNodeTypeImplementation']:
if 'requirementDefinitions' in c and 'requirementDefinition' in c['requirementDefinitions']: if 'requirementDefinitions' in c and 'requirementDefinition' in c['requirementDefinitions']:
for req in c['requirementDefinitions']['requirementDefinition']: for req in c['requirementDefinitions']['requirementDefinition']:
requirements.add(req['requirementType']) requirements.append(req['requirementType'])
if 'derivedFrom' in c and c['derivedFrom'] and c['derivedFrom']['type']: if 'derivedFrom' in c and c['derivedFrom'] and c['derivedFrom']['type']:
self.get_super_types_requirements(c['derivedFrom']['type'],requirements) self.get_super_types_requirements(c['derivedFrom']['type'], requirements)
return requirements return requirements
def get_service_template(self,dict_tpl): def get_service_template(self, dict_tpl):
if(not self.service_templates): if(not self.service_templates):
self.service_templates = self.find(dict_tpl,self.service_template_names) self.service_templates = self.find(dict_tpl, self.service_template_names)
return self.service_templates return self.service_templates
def get_topology_template(self,dict_tpl): def get_topology_template(self, dict_tpl):
if (not self.topology_templates): if (not self.topology_templates):
self.topology_templates = self.find(dict_tpl,self.topology_template_names) self.topology_templates = self.find(dict_tpl, self.topology_template_names)
return self.topology_templates return self.topology_templates
def get_node_templates(self,dict_tpl): def get_node_templates(self, dict_tpl):
if (not self.node_templates): if (not self.node_templates):
self.node_templates = self.find(dict_tpl,self.node_template_names) self.node_templates = self.find(dict_tpl, self.node_template_names)
return self.node_templates return self.node_templates
def get_requirements(self,dict_tpl): 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): def get_relationships(self, dict_tpl):
return self.find(dict_tpl,self.relationships_names) return self.find(dict_tpl, self.relationships_names)
def get_requirement_type(self,dict_req): def get_requirement_type(self, dict_req):
return self.find(dict_req,self.type_names) return self.find(dict_req, self.type_names)
def find(self,dict_tpl,names): def find(self, dict_tpl, names):
if dict_tpl: if dict_tpl:
for name in names: for name in names:
if(name in dict_tpl): if(name in dict_tpl):
......
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>drip_planner2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">python3.6</pydev_property>
</pydev_project>
java.lib.path=
main.file=__main__.py
platform.active=Python_3.6.6
python.lib.path=
src.dir=src
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.python.project</type>
<configuration>
<data xmlns="http://nbpython.dev.java.net/ns/php-project/1">
<name>tosca_service</name>
<sources>
<root id="src.dir"/>
</sources>
<tests/>
</data>
</configuration>
</project>
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
import json
import logging
import os
import os.path
from os.path import expanduser
import pika
from winery.service import *
import sys
import tempfile
import time
if __name__ == "__main__":
tosca_reposetory_api_base_url = "http://localhost:8080/winery"
namespace = "http%253A%252F%252Fsne.uva.nl%252Fservicetemplates"
servicetemplate_id = "wordpress_w1-wip1"
repo = Service(tosca_reposetory_api_base_url)
servicetemplates = repo.get_servicetemplates(namespace,servicetemplate_id)
tt= repo.get_topology_template(servicetemplates)
node_templates = repo.get_node_templates(tt)
parents = repo.get_parents(node_templates[1])
print(parents)
from setuptools import setup, find_packages
setup (
name='drip_planner2',
version='0.1',
packages=find_packages(),
# Declare your packages' dependencies here, for eg:
install_requires=['foo>=3'],
# Fill in these to make your Egg ready for upload to
# PyPI
author='S. Koulouzis',
author_email='',
#summary = 'Just another Python package for the cheese shop',
url='',
license='',
long_description='Long description of the package',
# could also include long_description, download_url, classifiers, etc.
)
\ No newline at end of file
import json
import operator
import pdb
import re
from toscaparser.tosca_template import ToscaTemplate
import toscaparser.utils.yamlparser
import urllib
import urllib.parse
class Service:
service_template_names = ['serviceTemplateOrNodeTypeOrNodeTypeImplementation']
topology_template_names = ['topologyTemplate']
node_template_names = ['nodeTemplates']
requirement_names = ['requirements']
type_names = ['type']
relationships_names = ['relationshipTemplates']
service_templates = None
topology_templates = []
node_templates = []
requirements = {}
def __init__(self, tosca_reposetory_api_base_url):
self.tosca_reposetory_api_base_url = tosca_reposetory_api_base_url
def get_servicetemplates(self,namespace, servicetemplate_id):
servicetemplate_url = self.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()
self.dict_tpl = json.loads(res_body.decode("utf-8"))
return self.dict_tpl
def get_topology_template(self, dict_tpl):
if (not self.topology_templates):
self.topology_templates = self.find(dict_tpl['serviceTemplateOrNodeTypeOrNodeTypeImplementation'][0], self.topology_template_names)
return self.topology_templates
def get_node_templates(self, topology_templates):
if (not self.node_templates):
nodes = self.find(topology_templates,self.node_template_names)
self.node_templates = nodes
return self.node_templates
def get_parents(self,template):
node_type = template['type']
node = self.get_object(node_type)['serviceTemplateOrNodeTypeOrNodeTypeImplementation'][0]
return self.get_type_parents(node,None)
def get_type_parents(self,type,parents):
if 'derivedFrom' in type:
type_str = type['derivedFrom']['typeRef']
parent = self.get_object(type_str)['serviceTemplateOrNodeTypeOrNodeTypeImplementation'][0]
if(parents == None):
parents = []
parents.append(parent)
self.get_type_parents(parent,parents)
return parents
def get_object(self,type_str):
regex = r"\{(.*?)\}"
matches = re.finditer(regex, type_str, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1)
id = type_str.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='')
type_name = namespace.rsplit('/', 1)[-1]
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()
return json.loads(res_body.decode("utf-8"))
def find(self, dict_obj, names):
if dict_obj:
for name in names:
if(name in dict_obj):
return dict_obj[name]
\ 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