Commit 311272e2 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Try to use parser

parent 35812d5f
...@@ -18,7 +18,7 @@ RUN pip install numpy ...@@ -18,7 +18,7 @@ RUN pip install numpy
RUN pip install networkx==1.10 RUN pip install networkx==1.10
RUN pip install flask RUN pip install flask
RUN pip install tosca-parser RUN pip install tosca-parser
RUN pip install pika RUN pip install pika==0.11.2
RUN export LC_ALL="en_US.UTF-8" RUN export LC_ALL="en_US.UTF-8"
RUN pip install paramiko RUN pip install paramiko
......
tosca_definitions_version: tosca_simple_yaml_1_0 tosca_definitions_version: tosca_simple_yaml_1_0
imports: imports:
- types/nodes.yaml - indigo_custom_types: types/nodes.yaml
- types/interfaces.yaml
repositories: description: >
docker_hub: https://hub.docker.com/ TOSCA example
topology_template: topology_template:
node_templates: node_templates:
wordpress:
wordpres: type: tosca.nodes.SWITCH.Container.Application.Docker
type: tosca.nodes.Container.Application.Docker requirements:
- service:
node: mysql
relationship:
type: tosca.relationships.ConnectsTo
artifacts:
my_image:
file: wordpress:latest
type: tosca.artifacts.Deployment.Image.Container.Docker
repository: docker_hub
properties: properties:
ports: ports:
- "8000:80" - "8000:80"
environment: environment:
WORDPRESS_DB_HOST: mysql-db:3306 WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wordpress WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_NAME: wordpress
artifacts:
image:
type: tosca.artifacts.Deployment.Image.Container.Docker
file: wordpress:latest
repository: docker_hub
requirements:
- db:
node: mysql-db
relationship:
type: tosca.relationships.ConnectsTo
mysql-db: mysql:
type: tosca.nodes.Container.Application.Docker type: tosca.nodes.SWITCH.Container.Application.Docker
properties: properties:
volumes: volumes:
- db_data:/var/lib/mysql - db_data:/var/lib/mysql
...@@ -47,3 +45,9 @@ topology_template: ...@@ -47,3 +45,9 @@ topology_template:
type: tosca.artifacts.Deployment.Image.Container.Docker type: tosca.artifacts.Deployment.Image.Container.Docker
file: mysql:5.7 file: mysql:5.7
repository: docker_hub repository: docker_hub
...@@ -2,9 +2,12 @@ tosca_definitions_version: tosca_simple_yaml_1_0 ...@@ -2,9 +2,12 @@ tosca_definitions_version: tosca_simple_yaml_1_0
node_types: node_types:
tosca.nodes.Container.Application.Docker: tosca.nodes.SWITCH.Container.Application.Docker:
derived_from: tosca.nodes.Container derived_from: tosca.nodes.Container.Application.Docker
description: description of container description: description of container
capabilities:
service:
type: tosca.capabilities.Container.Docker
properties: properties:
ports: ports:
description: ports to be forwarded description: ports to be forwarded
...@@ -26,106 +29,12 @@ node_types: ...@@ -26,106 +29,12 @@ node_types:
description: volume description: volume
type: list type: list
required: false required: false
capabilities:
service:
type: tosca.capabilities.Container.Docker
requirements: requirements:
- service:
capability: tosca.capabilities.Container.Docker
node: tosca.nodes.SWITCH.Container.Application.Docker
relationship: tosca.relationships.ConnectsTo
- host: - host:
capability: tosca.capabilities.Container.Orchestrator.Docker capability: tosca.capabilities.Compute
node: tosca.nodes.Container.Orchestrator.Docker node: tosca.nodes.Compute
relationship: tosca.relationships.HostedOn relationship: tosca.relationships.HostedOn
tosca.nodes.Container.Orchestrator.Docker.Swarm:
derived_from: tosca.nodes.Container.Orchestrator.Docker
description: The swarm Docker Orchestrator
interfaces:
Ansible:
type: tosca.interfaces.Ansible
tosca.nodes.Container.Orchestrator.Docker:
derived_from: tosca.nodes.Container.Orchestrator
description: Docker Orchestrator
capabilities:
host:
type: tosca.capabilities.Container.Orchestrator.Docker
requirements:
- host:
capability: tosca.capabilities.Compute.Cluster
node: tosca.nodes.Compute.VM.Cluster
relationship: tosca.relationships.HostedOn
tosca.nodes.Compute.VM.Cluster:
derived_from:
description: A cluster of VM able
properties:
vms:
description: a list of VMs in the cluster. At lest one of them must be the master
type: list
required: true
capabilities:
host:
type: tosca.capabilities.Compute.Cluster
tosca.nodes.Compute.VM:
derived_from: tosca.nodes.Compute
description: VM
properties:
name:
description: host name
type: string
required: true
user_names:
description: list of user name
type: list
required: true
id:
description: vm id
type: string
required: true
num_of_cpu:
description: number of cpu
type: int
required: true
mem_size:
description: ram size in MB
type: int
required: true
disk_size:
description: disk size in MB
type: int
required: true
ip_addresses:
description: available IP addresses
type: int
required: true
role:
description: available IP addresses
type: string
required: true
os_type:
description: OS example: Ubuntu 16.04
type: string
required: true
capabilities:
host:
type: tosca.capabilities.Compute
tosca.nodes.Compute.VM.ExoGENI:
derived_from: tosca.nodes.Compute.VM
description: VM
properties:
domain:
description: site where the VM is hosted, example: RENCI (Chapel Hill, NC USA) XO Rack
type: string
required: true
state:
description: the state of the VM, running, paused, stopped
type: string
required: true
ssh_key_pair_id:
description: the id of the ssh key
type: string
required: true
capabilities:
host:
type: tosca.capabilities.Scale
tosca_definitions_version: tosca_simple_yaml_1_1
policy_types:
tosca.policies.root:
description: From 'http://docs.oasis-open.org/tosca/TOSCA/v1.0/os/TOSCA-v1.0-os.html#_Toc356403653': Non-functional behavior or quality-of-services are defined in TOSCA by means of policies. A Policy can express such diverse things like monitoring behavior, payment conditions, scalability, or continuous availability.
A Node Template can be associated with a set of Policies collectively expressing the non-functional behavior or quality-of-services that each instance of the Node Template will expose. Each Policy specifies the actual properties of the non-functional behavior, like the concrete payment information (payment period, currency, amount etc) about the individual instances of the Node Template.
Policy Templates provide actual values of properties of the types defined by Policy Types. For example, a Policy Template for monthly payments for US customers will set the “payment period” property to “monthly” and the “currency” property to “US$”, leaving the “amount” property open. The “amount” property will be set when the corresponding Policy Template is used for a Policy within a Node Template. Thus, a Policy Template defines the invariant properties of a Policy, while the Policy sets the variant properties resulting from the actual usage of a Policy Template in a Node Template.
tosca.policies.Requirement.Network.connection:
description: Connection Requirement
derived_from: tosca.policies.Root
properties:
ip:
type: string
description: IP and Netmask (e.g. 10.0.0.1/8) that should be reachable from the container.
tosca.policies.Requirement.Location:
description: Location Requirement
derived_from: tosca.policies.Root
properties:
desired_lat:
type: double
description: desired latitude coordinates of container
desired_lon:
type: double
description: desired longitude coordinates of container
min_distance:
type: double
description: the minimum accepted distance from the desired location
tosca.policies.Requirement.Network.Bandwidth:
description: Bandwidth Requirement
derived_from: tosca.policies.Root
properties:
download_speed:
type: integer
description: max download speed in bit/s
upload_speed:
type: integer
description: max upload speed bit/s
target:
type: string
description: target host or IP to download or upload data
tosca.policies.Container.Scale:
description: spawn new container instance when a specified metric is grater or less than a threshold
derived_from: tosca.policies.Root
properties:
threshold:
type: double
description: threshold to spawn new container
condition:
type: string
description: grater (gt) or less (lt) than the set threshold
metric_name:
type: string
description: the metric to monitor
node_name:
type: string
description: the container to scale (should be type tosca.nodes.Container)
tosca.policies.VM.Scale:
description: spawn new container instance when a specified metric is grater or less than a threshold
derived_from: tosca.policies.Root
properties:
threshold:
type: double
description: threshold to spawn new container
condition:
type: string
description: grater (gt) or less (lt) than the set threshold
metric_name:
type: string
description: the metric to monitor
node_name:
type: string
description: the container to scale (should be type tosca.nodes.VM)
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/drip_planner2.iml" filepath="$PROJECT_DIR$/.idea/drip_planner2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed.
...@@ -3,20 +3,26 @@ import operator ...@@ -3,20 +3,26 @@ import operator
import pdb import pdb
import re import re
from toscaparser.tosca_template import ToscaTemplate from toscaparser.tosca_template import ToscaTemplate
from toscaparser.topology_template import TopologyTemplate
import toscaparser.utils.yamlparser import toscaparser.utils.yamlparser
import urllib import urllib
import urllib.parse import urllib.parse
import sys import sys
import pdb
class BasicPlanner: class BasicPlanner:
def __init__(self, path): def __init__(self, path):
dict_tpl = toscaparser.utils.yamlparser.load_yaml(path) self.template = ToscaTemplate(path)
# print(dict_tpl) print("Description:"+self.template.description)
node_templates = dict_tpl['topology_template']['node_templates'] for node in self.template.nodetemplates:
# print(node_templates) print("Node: "+node.name+" Type:"+node.type+" Req: "+str(node.requirements))
requirements = self.get_all_requirements(node_templates) # print(node.get_properties().keys())
# print(node.get_capabilities().keys)
...@@ -43,13 +49,13 @@ class BasicPlanner: ...@@ -43,13 +49,13 @@ class BasicPlanner:
return node return node
def get_all_requirements(self, node_templates): def get_all_requirements(self, node_templates):
requirements = {}
for node_template_name in node_templates: for node_template_name in node_templates:
node_template = node_templates[node_template_name] node_template = node_templates[node_template_name]
node_type = node_template['type'] node_type = node_template['type']
if 'requirements' in node_template: if 'requirements' in node_template:
requirements = node_template['requirements'] requirements[node_template_name] = node_template['requirements']
print(requirements) self.get_super_types_requirements(node_type, None)
# all_requirements = self.get_super_types_requirements(node_type, None)
# id = node_template['id'] # id = node_template['id']
# req = self.get_requirements(node_template) # req = self.get_requirements(node_template)
# if(req): # if(req):
...@@ -118,58 +124,12 @@ class BasicPlanner: ...@@ -118,58 +124,12 @@ class BasicPlanner:
all_relationships.append(rel) all_relationships.append(rel)
return all_relationships return all_relationships
def get_super_types(self, type_qName, supertypes): def get_super_types(self, type, supertypes):
if (supertypes == None): print(type)
supertypes = []
regex = r"\{(.*?)\}"
matches = re.finditer(regex, type_qName, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1)
id = type_qName.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()
component = json.loads(res_body.decode("utf-8"))
if component:
comp = component['serviceTemplateOrNodeTypeOrNodeTypeImplementation'][0]
supertypes.append(comp)
if 'derivedFrom' in comp:
return self.get_super_types(comp['derivedFrom']['typeRef'], supertypes)
else:
return supertypes
def get_super_types_requirements(self, type_qName, requirements):
if (requirements == None):
requirements = []
regex = r"\{(.*?)\}"
matches = re.finditer(regex, type_qName, re.MULTILINE | re.DOTALL)
namespace = next(matches).group(1)
id = type_qName.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] def get_super_types_requirements(self, node_type, requirements):
print(node_type)
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.append(req['requirementType'])
if 'derivedFrom' in c and c['derivedFrom'] and c['derivedFrom']['type']:
return self.get_super_types_requirements(c['derivedFrom']['type'], requirements)
return requirements
def get_service_template(self, dict_tpl): def get_service_template(self, dict_tpl):
......
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