Commit 632a6a56 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added more tests

parent 6321c6f2
......@@ -17,7 +17,7 @@ topology_template:
protocol: "ssh"
keys:
public_key: "c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFBZ1FDV3l6VEoxS1pycDVvVHVBVm15dmxKL2prMnRNdktZSlAvSWtHZ1JKeTVFNUgrbjl0eU1QRXdyUDN4c0lUMVZzRjlVNVhLTkkvVDVpZDgxNko5RmxjbEhOSHVrbEFTMlhqV3QzNnlZekorMkhsa3JET0I4Q25DdmgvQlQ5K2liNkpjVFpUd21Xb1FQbjFuVXpSelRSVGU5NUlyOUl4djgrTnNLVWYxdVYzNDBRPT0gYXV0byBnZW5lcmF0ZWQgdXNlciBhY2NlZXMga2V5cwo="
private_key: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlDV3dJQkFBS0JnUUNXeXpUSjFLWnJwNW9UdUFWbXl2bEovamsydE12S1lKUC9Ja0dnUkp5NUU1SCtuOXR5Ck1QRXdyUDN4c0lUMVZzRjlVNVhLTkkvVDVpZDgxNko5RmxjbEhOSHVrbEFTMlhqV3QzNnlZekorMkhsa3JET0IKOENuQ3ZoL0JUOStpYjZKY1RaVHdtV29RUG4xblV6UnpUUlRlOTVJcjlJeHY4K05zS1VmMXVWMzQwUUlEQVFBQgpBb0dBSUJDMStQVXZMbFZGZmt2eldoaHZTankxRGQwRU9OU3dvK0EwOUUraXcvaFZZeis2K2wxTjdIdmN4RGcxCkRiYVZEb0ZucU1ma2picXpTdWpQdEttN2ZlVThldjF3a21aOHNRdzhac1JNdXd3NUxDUzZpMENnem5RMlJ3WmIKU2dqSlVrLy81VVF2Z2x2YkFEMXJpUlJ1TzR5N0cwTFZpa2Y4b3UvM0pBM21iQUVDUVFESm1VY2dBQjJONmpSYQpydWxhNklnYzFYYjdTUGl0TEpFQjFNZytyUW5EZDZWWXZqcVVGaW9zWVdhYlJSdGtPQ1VPdWt5cklLY1puZ3Q4CjdrdHN6T1c1QWtFQXYzeEFHYUdyWkk1KzdjSTF4Si9NaWVRa1pkaFFWTEpEUnpwT3FJVjVKaFIzbCt2R3N2R2MKWWhwUG9IL1VoN3hsV3NLUVF2Q3Y2azhyaVh1dmZ6aTMyUUpBQTJDb0NtYzJGNHlXOS9YNlB1V3FjaUU1YnhoWAo0ajdlNXhMemorbENRcDRjalJJUzFTeFI1SkIxTWFERXhyY1NGOThUTGNMTVNlZVY5YS9nWDZCSE1RSkFadHNVCkIzQ2JBdW16N255cXF4SjNLUDUrT3FLbUM4aXhjTm8yRkRtVjZNbUhyL2c3QkRYYUhhd3FaM2tpclVLYVl0VXQKN25jZzEwaHRoMjBEeU4rcmlRSkFRMHVvNW9HaUxJSHNEQUR5Nkw1azdrNTZjVUlhZnhpeVRHdzlWQ2tUY3puVQpPS1liYXFVM3lyaDRqbzRYTHlGd3JYcHJOdkNsb29pY1JIMTdSUkMxUHc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo="
private_key: "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlDV3dJQkFBS0JnUUNXeXpUSjFLWnJwNW9UdUFWbXl2bEovamsydE12S1lKUC9Ja0dnUkp5NUU1SCtuOXR5Ck1QRXdyUDN4c0lUMVZzRjlVNVhLTkkvVDVpZDgxNko5RmxjbEhOSHVrbEFTMlhqV3QzNnlZekorMkhsa3JET0IKOENuQ3ZoL0JUOStpYjZKY1RaVHdtV29RUG4xblV6UnpUUlRlOTVJcjlJeHY4K05zS1VmMXVWMzQwUUlEQVFBQgpBb0dBSUJDMStQVXZMbFZGZmt2eldoaHZTankxRGQwRU9OU3dvK0EwOUUraXcvaFZZeis2K2wxTjdIdmN4RGcxCkRiYVZEb0ZucU1ma2picXpTdWpQdEttN2ZlVThldjF3a21aOHNRdzhac1JNdXd3NUxDUzZpMENnem5RMlJ3WmIKU2dqSlVrLy81VVF2Z2x2YkFEMXJpUlJ1TzR5N0cwTFZpa2Y4b3UvM0pBM21iQUVDUVFESm1VY2dBQjJONmpSYQpydWxhNklnYzFYYjdTUGl0TEpFQjFNZytyUW5EZDZWWXZqcVVGaW9zWVdhYlJSdGtPQ1VPdWt5cklLY1puZ3Q4CjdrdHN6T1c1QWtFQXYzeEFHYUdyWkk1KzdjSTF4Si9NaWVRa1pkaFFWTEpEUnpwT3FJVjVKaFIzbCt2R3N2R2MKWWhwUG9IL1VoN3hsV3NLUVF2Q3Y2azhyaVh1dmZ6aTMyUUpBQTJDb0NtYzJGNHlXOS9YNlB1V3FjaUU1YnhoWAo0ajdlNXhMemorbENRcDRjalJJUzFTeFI1SkIxTWFERXhyY1NGOThUTGNMTVNlZVY5YS9nWDZCSE1RSkFadHNVCkIzQ2JBdW16N255cXF4SjNLUDUrT3FLbUM4aXhjTm8yRkRtVjZNbUhyL2c3QkRYYUhhd3FaM2tpclVLYVl0VXQKN25jZzEwaHRoMjBEeU4rcmlRSkFRMHVvNWf9HaUxJSHNEQUR5Nkw1azdrNTZjVUlhZnhpeVRHdzlWQ2tUY3puVQpPS1liYXFVM3lyaDRqbzRYTHlGd3JYcHJOdkNsb29pY1JIMTdSUkMxUHc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo="
public_ip: "192.125.18.246"
role: "master"
node_type: "t2.medium"
......@@ -82,7 +82,7 @@ topology_template:
- "/var/run/docker.sock:/var/run/docker.sock"
requirements:
- host:
capability: "tosca.capabilities.ARTICONF.Orchestrator"
capability: "tosca.capabilities.ARTICONF.docker.Orchestrator"
node: "kubernetes"
relationship: "tosca.relationships.HostedOn"
type: "tosca.nodes.ARTICONF.Container.Application.Docker"
......@@ -104,7 +104,7 @@ topology_template:
- "db_data:/var/lib/mysql"
requirements:
- host:
capability: "tosca.capabilities.ARTICONF.Orchestrator"
capability: "tosca.capabilities.ARTICONF.docker.Orchestrator"
node: "kubernetes"
relationship: "tosca.relationships.HostedOn"
type: "tosca.nodes.ARTICONF.Container.Application.Docker"
......
description: TOSCA example
imports:
- nodes: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/nodes.yaml
- data: https://raw.githubusercontent.com/skoulouzis/CONF/develop/TOSCA/types/data.yml
- capabilities: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/capabilities.yaml
- policies: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/policies.yaml
- interfaces: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/interfaces.yml
topology_template:
node_templates:
compute:
interfaces:
Standard:
create: dumy.yaml
properties:
disk_size: 50000 MB
mem_size: 6000 MB
num_cores: 2
os: Ubuntu 18.04
user_name: vm_user
type: tosca.nodes.ARTICONF.VM.Compute
tosca_definitions_version: tosca_simple_yaml_1_0
description: TOSCA example
imports:
- nodes: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/nodes.yaml
- data: https://raw.githubusercontent.com/skoulouzis/CONF/develop/TOSCA/types/data.yml
- capabilities: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/capabilities.yaml
- policies: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/policies.yaml
- interfaces: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/interfaces.yml
topology_template:
node_templates:
kubernetes:
interfaces:
Kubernetes:
configure:
inputs:
playbook: https://raw.githubusercontent.com/skoulouzis/CONF/develop/ansible_playbooks/dashboard.yaml
create:
inputs:
playbook: https://raw.githubusercontent.com/skoulouzis/CONF/develop/ansible_playbooks/create_k8s.yml
install:
inputs:
playbook: https://raw.githubusercontent.com/skoulouzis/CONF/develop/ansible_playbooks/install_k8s.yml
properties:
min_masters_num: 1
min_workers_num: 1
type: tosca.nodes.ARTICONF.docker.Orchestrator.Kubernetes
tosca_definitions_version: tosca_simple_yaml_1_0
tosca_definitions_version: tosca_simple_yaml_1_0
tosca_definitions_version: "tosca_simple_yaml_1_0"
description: "TOSCA example"
imports:
- nodes: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/nodes.yaml
- data: https://raw.githubusercontent.com/skoulouzis/CONF/develop/TOSCA/types/data.yml
- capabilities: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/capabilities.yaml
- policies: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/policies.yaml
- interfaces: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/interfaces.yml
repositories:
docker_hub: https://hub.docker.com/
description: >
TOSCA example
- nodes: "https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/nodes.yaml"
- data: "https://raw.githubusercontent.com/skoulouzis/CONF/develop/TOSCA/types/data.yml"
- capabilities: "https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/capabilities.yaml"
- policies: "https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/policies.yaml"
- interfaces: "https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/interfaces.yml"
topology_template:
node_templates:
mongo:
type: tosca.nodes.ARTICONF.Container.Application.Docker
properties:
ports:
- "30001:27017"
volumes:
- db-data:/data/db
environment:
ROOT_PASSWORD: root_passwd
DATABASE: db
USER: user
PASSWORD: passwd
artifacts:
image:
type: tosca.artifacts.Deployment.Image.Container.Docker
file: mongo:3
repository: docker_hub
node_templates:
kubernetes:
properties:
min_masters_num: 1
min_workers_num: 1
#requirements:
#- host:
#capability: "tosca.capabilities.ARTICONF.VM.topology"
#node: "topology"
#relationship: "tosca.relationships.HostedOn"
interfaces:
Kubernetes:
create:
inputs:
playbook: "https://raw.githubusercontent.com/skoulouzis/CONF/develop/ansible_playbooks/create_k8s.yml"
install:
inputs:
playbook: "https://raw.githubusercontent.com/skoulouzis/CONF/develop/ansible_playbooks/install_k8s.yml"
type: "tosca.nodes.ARTICONF.Orchestrator.Kubernetes"
#mysql:
#type: "tosca.nodes.ARTICONF.Container.Application.Docker"
#properties:
#environment:
#MYSQL_DATABASE: "wordpress"
#MYSQL_PASSWORD: "wordpress"
#MYSQL_ROOT_PASSWORD: "somewordpress"
#MYSQL_USER: "wordpress"
#ports:
#- "3306:3306"
#volumes:
#- "db_data:/var/lib/mysql"
#requirements:
#- host:
#capability: "tosca.capabilities.ARTICONF.docker.Orchestrator"
#node: "kubernetes"
#relationship: "tosca.relationships.HostedOn"
#artifacts:
#image:
#file: "mysql:5.7"
#repository: "docker_hub"
#type: "tosca.artifacts.Deployment.Image.Container.Docker"
#topology:
#properties:
#domain: "Frankfurt"
#provider: "EC2"
#interfaces:
#CloudsStorm:
#provision:
#inputs:
#code_type: "SEQ"
#object_type: "SubTopology"
#type: "tosca.nodes.ARTICONF.VM.topology"
description: TOSCA example
imports:
- nodes: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/nodes.yaml
- data: https://raw.githubusercontent.com/skoulouzis/CONF/develop/TOSCA/types/data.yml
- capabilities: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/capabilities.yaml
- policies: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/policies.yaml
- interfaces: https://raw.githubusercontent.com/skoulouzis/DRIP/develop/TOSCA/types/interfaces.yml
topology_template:
node_templates:
topology:
interfaces:
CloudsStorm:
provision:
inputs:
code_type: SEQ
object_type: SubTopology
properties:
domain: Frankfurt
provider: EC2
type: tosca.nodes.ARTICONF.VM.topology
tosca_definitions_version: tosca_simple_yaml_1_0
......@@ -104,7 +104,7 @@ def handle_delivery(message, sys=None):
spec_service = SpecService(conf)
test_planner = Planner(tosca_path=input_tosca_file_path, spec_service=spec_service)
tosca_template = test_planner.resolve_requirements()
tosca_template = test_planner.set_infrastructure_specifications()
tosca_template = test_planner.set_node_templates_properties()
template_dict = tosca_helper.get_tosca_template_2_topology_template_dictionary(tosca_template)
Planner(yaml_dict_tpl=template_dict, spec_service=spec_service)
......@@ -135,7 +135,7 @@ if __name__ == "__main__":
spec_service = SpecService(conf)
test_planner = Planner(input_tosca_file_path, spec_service)
test_tosca_template = test_planner.resolve_requirements()
test_tosca_template = test_planner.set_infrastructure_specifications()
test_tosca_template = test_planner.set_node_templates_properties()
template_dict = tosca_helper.get_tosca_template_2_topology_template_dictionary(test_tosca_template)
logger.info("template ----: \n" + yaml.dump(template_dict))
......
......@@ -6,11 +6,13 @@ from toscaparser.tosca_template import ToscaTemplate
from toscaparser.topology_template import TopologyTemplate
import operator
# import matplotlib.pyplot as plt
from service.simple_spec_alayzer import SimpleAnalyzer
from util import tosca_helper
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
def add_requirements(node, missing_requirements, capable_node_name):
"""Add the requirements to the node """
......@@ -39,6 +41,19 @@ def add_requirements(node, missing_requirements, capable_node_name):
return node
def get_default_value(node_property):
if node_property and node_property.required and isinstance(node_property.value, dict) and 'required' in \
node_property.value and 'type' in node_property.value:
if node_property.default:
return {node_property.name: node_property.default}
if node_property.constraints:
for constraint in node_property.constraints:
print(constraint)
if node_property and node_property.required and node_property.value:
return {node_property.name: node_property.value}
return None
class Planner:
def __init__(self, tosca_path=None, yaml_dict_tpl=None, spec_service=None):
......@@ -47,7 +62,7 @@ class Planner:
self.tosca_template = ToscaTemplate(tosca_path)
elif yaml_dict_tpl:
self.yaml_dict_tpl = yaml_dict_tpl
logging.info('yaml_dict_tpl:\n' + str(yaml.dump(yaml_dict_tpl)))
logger.info('yaml_dict_tpl:\n' + str(yaml.dump(yaml_dict_tpl)))
self.tosca_template = ToscaTemplate(yaml_dict_tpl=yaml_dict_tpl)
self.tosca_node_types = self.tosca_template.nodetemplates[0].type_definition.TOSCA_DEF
......@@ -65,17 +80,47 @@ class Planner:
self.tosca_template.nodetemplates.append(node_template)
return self.tosca_template
def set_infrastructure_specifications(self):
# Start bottom up and (node without requirements leaf) and find the root of the graph.
# Get root performance, version requirements and set specs to required node
specification_analyzer = SimpleAnalyzer(self.tosca_template)
nodes_with_new_specifications = specification_analyzer.set_node_specifications()
for new_spec_node in nodes_with_new_specifications:
for index, node_in_temple in enumerate(self.tosca_template.nodetemplates):
if new_spec_node.name == node_in_temple.name:
self.tosca_template.nodetemplates[index] = new_spec_node
break
def set_default_node_properties(self, node):
logger.info('Setting properties for: ' + str(node.type))
ancestors_properties = tosca_helper.get_all_ancestors_properties(node, self.all_node_types,
self.all_custom_def)
default_properties = {}
if ancestors_properties:
for ancestors_property in ancestors_properties:
for node_prop in node.get_properties_objects():
if ancestors_property.name == node_prop.name and isinstance(node_prop.value,dict) \
and 'required' in node_prop.value and 'type' in node_prop.value:
default_property = get_default_value(ancestors_property)
if default_property:
node_prop.value = default_property
default_properties[next(iter(default_property))] = default_property[
next(iter(default_property))]
if default_properties:
for default_property in default_properties:
for node_prop_obj in node.get_properties_objects():
if default_property == node_prop_obj.name and not node_prop_obj.value:
node.get_properties_objects().append(default_property)
node_name = next(iter(node.templates))
if 'properties' in node.templates[node_name]:
for prop_name in node.templates[node_name]['properties']:
if isinstance(node.templates[node_name]['properties'][prop_name], dict) or \
isinstance(node.templates[node_name]['properties'][prop_name], str):
if 'required' in node.templates[node_name]['properties'][prop_name] and \
node.templates[node_name]['properties'][prop_name]['required'] and \
'default' in node.templates[node_name]['properties'][prop_name] and \
prop_name not in default_properties:
default_properties[prop_name] = node.templates[node_name]['properties'][prop_name][
'default']
logger.info(
'Adding to : ' + str(node.templates[node_name]) + ' properties: ' + str(default_properties))
node.templates[node_name]['properties'] = default_properties
return node
def set_node_templates_properties(self):
for node_template in self.tosca_template.nodetemplates:
self.set_default_node_properties(node_template)
specification_analyzer = SimpleAnalyzer(self.tosca_template)
nodes_with_new_relationship_occurrences = specification_analyzer.set_relationship_occurrences()
......@@ -93,6 +138,7 @@ class Planner:
self.tosca_template.nodetemplates.append(new_spec_occurrences)
return self.tosca_template
def get_node_template_property(self, prop_key, node_prop_dict):
prop_value = self.spec_service.get_property(prop_key)
if prop_value:
......@@ -118,13 +164,13 @@ class Planner:
def add_required_nodes(self, node):
"""Adds the required nodes in self.required_nodes for an input node."""
if isinstance(node, NodeTemplate):
logging.info('Resolving requirements for: ' + node.name)
logger.info('Resolving requirements for: ' + node.name)
elif isinstance(node, dict):
logging.info('Resolving requirements for: ' + str(next(iter(node))))
logger.info('Resolving requirements for: ' + str(next(iter(node))))
# Get all requirements for node.
all_requirements = self.get_all_requirements(node)
if not all_requirements:
logging.debug('Node: ' + tosca_helper.get_node_type_name(node) + ' has no requirements')
logger.debug('Node: ' + tosca_helper.get_node_type_name(node) + ' has no requirements')
return
matching_node = self.find_best_node_for_requirements(all_requirements)
......@@ -135,7 +181,7 @@ class Planner:
node = add_requirements(node, all_requirements, matching_node_template.name)
if not tosca_helper.contains_node_type(self.required_nodes, matching_node_type_name) and \
not tosca_helper.contains_node_type(self.tosca_template.nodetemplates, matching_node_type_name):
logging.info(' Adding: ' + str(matching_node_template.name))
logger.info(' Adding: ' + str(matching_node_template.name))
self.required_nodes.append(matching_node)
# Find matching nodes for the new node's requirements
self.add_required_nodes(matching_node)
......@@ -144,13 +190,13 @@ class Planner:
"""Returns all requirements for an input node including all parents requirements"""
node_type_name = tosca_helper.get_node_type_name(node)
logging.info(' Looking for requirements for node: ' + node_type_name)
logger.info(' Looking for requirements for node: ' + node_type_name)
# Get the requirements for this node from its definition e.g. docker: hostedOn k8s
def_type = self.all_node_types[node_type_name]
all_requirements = []
if 'requirements' in def_type.keys():
all_requirements = def_type['requirements']
logging.info(' Found requirements: ' + str(all_requirements) + ' for node: ' + node_type_name)
logger.info(' Found requirements: ' + str(all_requirements) + ' for node: ' + node_type_name)
# Get the requirements for this node from the template. e.g. wordpress: connectsTo mysql
# node_requirements = tosca_helper.get_node_requirements(node)
......@@ -161,7 +207,7 @@ class Planner:
parent_requirements = tosca_helper.get_ancestors_requirements(node, self.all_node_types, self.all_custom_def)
parent_type = tosca_helper.get_node_type_name(node)
if parent_type and parent_requirements:
logging.info(
logger.info(
' Adding to : ' + str(node_type_name) + ' parent requirements from: ' + str(parent_type))
if not all_requirements:
all_requirements += parent_requirements
......@@ -174,7 +220,7 @@ class Planner:
'capability'] != parent_requirement[parent_requirement_key]['capability']:
all_requirements.append(parent_requirement)
logging.debug(' all_requirements: ' + str(all_requirements))
logger.debug(' all_requirements: ' + str(all_requirements))
return all_requirements
def get_node_types_by_capability(self, cap):
......@@ -183,13 +229,13 @@ class Planner:
candidate_nodes = {}
for tosca_node_type in self.all_node_types:
if tosca_node_type.startswith('tosca.nodes') and 'capabilities' in self.all_node_types[tosca_node_type]:
logging.debug(' Node: ' + str(tosca_node_type))
logger.debug(' Node: ' + str(tosca_node_type))
for caps in self.all_node_types[tosca_node_type]['capabilities']:
logging.debug(' ' + str(
logger.debug(' ' + str(
self.all_node_types[tosca_node_type]['capabilities'][caps]['type']) + ' == ' + cap)
if self.all_node_types[tosca_node_type]['capabilities'][caps]['type'] == cap:
candidate_nodes[tosca_node_type] = self.all_node_types[tosca_node_type]
logging.debug(' candidate_node: ' + str(tosca_node_type))
logger.debug(' candidate_node: ' + str(tosca_node_type))
candidate_child_nodes = {}
for node in candidate_nodes:
......@@ -213,11 +259,11 @@ class Planner:
if 'capability' in req[next(iter(req))]:
capability = req[next(iter(req))]['capability']
# Find nodes in node_templates that have the capability
logging.info(' Looking for nodes in node_templates with capability: ' + capability)
logger.info(' Looking for nodes in node_templates with capability: ' + capability)
capable_nodes = self.get_node_templates_by_capability(capability)
if not capable_nodes:
# Find all nodes in the definitions that have the capability: capability
logging.info(' Looking for nodes in node types with capability: ' + capability)
logger.info(' Looking for nodes in node types with capability: ' + capability)
capable_nodes = self.get_node_types_by_capability(capability)
if capable_nodes:
# Add number of matching capabilities for each node.
......@@ -231,10 +277,10 @@ class Planner:
matching_requirement_count += 1
number_of_matching_requirement[node_type] = matching_requirement_count
logging.info(' Found: ' + str(node_type))
logger.info(' Found: ' + str(node_type))
matching_nodes.update(capable_nodes)
else:
logging.error('Did not find any node with required capability: ' + str(capability))
logger.error('Did not find any node with required capability: ' + str(capability))
raise Exception('Did not find any node with required capability: ' + str(capability))
# if we only found 1 return it
if len(matching_nodes) == 1:
......@@ -255,8 +301,8 @@ class Planner:
return child_nodes
def get_node_templates_by_capability(self, capability):
capable_nodes = []
for node_template in self.tosca_template.nodetemplates:
for node_capability in node_template.get_capabilities():
print(node_capability)
return capable_nodes
# capable_nodes = []
# for node_template in self.tosca_template.nodetemplates:
# for node_capability in node_template.get_capabilities():
# print(node_capability)
return None
......@@ -33,61 +33,63 @@ class SimpleAnalyzer(SpecificationAnalyzer):
orchestrator_nodes = tosca_helper.get_nodes_by_type('tosca.nodes.ARTICONF.docker.Orchestrator',
self.tosca_template.nodetemplates, self.all_node_types,
self.all_custom_def)
if 'properties' in orchestrator_nodes[0].entity_tpl:
if 'min_masters_num' in orchestrator_nodes[0].entity_tpl['properties']:
min_masters_num = orchestrator_nodes[0].entity_tpl['properties']['min_masters_num']
if 'min_workers_num' in orchestrator_nodes[0].entity_tpl['properties']:
workers_num = orchestrator_nodes[0].entity_tpl['properties']['min_workers_num']
else:
min_masters_num = orchestrator_nodes[0].get_property_value('min_masters_num')
workers_num = orchestrator_nodes[0].get_property_value('min_workers_num')
min_masters_num = 1
workers_num = 0
if orchestrator_nodes:
if 'properties' in orchestrator_nodes[0].entity_tpl:
if 'min_masters_num' in orchestrator_nodes[0].entity_tpl['properties']:
min_masters_num = orchestrator_nodes[0].entity_tpl['properties']['min_masters_num']
if 'min_workers_num' in orchestrator_nodes[0].entity_tpl['properties']:
workers_num = orchestrator_nodes[0].entity_tpl['properties']['min_workers_num']
else:
min_masters_num = orchestrator_nodes[0].get_property_value('min_masters_num')
workers_num = orchestrator_nodes[0].get_property_value('min_workers_num')
topology_nodes = tosca_helper.get_nodes_by_type('tosca.nodes.ARTICONF.VM.topology',
self.tosca_template.nodetemplates, self.all_node_types,
self.all_custom_def)
vm_nodes = tosca_helper.get_nodes_by_type('tosca.nodes.ARTICONF.VM.Compute',
self.tosca_template.nodetemplates, self.all_node_types,
self.all_custom_def)
if vm_nodes:
for i in range(len(vm_nodes), min_masters_num):
old_vm_name = vm_nodes[0].name
new_vm = copy.deepcopy(vm_nodes[0])
new_vm_name = new_vm.name + '_' + str(i)
new_vm.name = new_vm_name
templates = new_vm.templates.pop(old_vm_name)
new_vm.templates[new_vm_name] = templates
return_nodes.append(new_vm)
for requirement in topology_nodes[0].requirements:
requirement_key = next(iter(requirement))
requirement_value = requirement[requirement_key]
if requirement_value['capability'] == 'tosca.capabilities.ARTICONF.VM':
new_requirement = copy.deepcopy(requirement)
new_requirement[requirement_key]['node'] = new_vm.name
topology_nodes[0].requirements.append(new_requirement)
return_nodes.append(topology_nodes[0])
break
for i in range(len(vm_nodes), workers_num + 1):
old_vm_name = vm_nodes[0].name
new_vm = copy.deepcopy(vm_nodes[0])
new_vm_name = new_vm.name + '_' + str(i)
new_vm.name = new_vm_name
templates = new_vm.templates.pop(old_vm_name)
new_vm.templates[new_vm_name] = templates
return_nodes.append(new_vm)
for requirement in topology_nodes[0].requirements:
requirement_key = next(iter(requirement))
requirement_value = requirement[requirement_key]
if requirement_value['capability'] == 'tosca.capabilities.ARTICONF.VM':
new_requirement = copy.deepcopy(requirement)
new_requirement[requirement_key]['node'] = new_vm.name
topology_nodes[0].requirements.append(new_requirement)
return_nodes.append(topology_nodes[0])
break
if topology_nodes:
vm_nodes = tosca_helper.get_nodes_by_type('tosca.nodes.ARTICONF.VM.Compute',
self.tosca_template.nodetemplates, self.all_node_types,
self.all_custom_def)
if vm_nodes:
for i in range(len(vm_nodes), min_masters_num):
old_vm_name = vm_nodes[0].name
new_vm = copy.deepcopy(vm_nodes[0])
new_vm_name = new_vm.name + '_' + str(i)
new_vm.name = new_vm_name
templates = new_vm.templates.pop(old_vm_name)
new_vm.templates[new_vm_name] = templates
return_nodes.append(new_vm)
for requirement in topology_nodes[0].requirements:
requirement_key = next(iter(requirement))
requirement_value = requirement[requirement_key]
if requirement_value['capability'] == 'tosca.capabilities.ARTICONF.VM':
new_requirement = copy.deepcopy(requirement)
new_requirement[requirement_key]['node'] = new_vm.name
topology_nodes[0].requirements.append(new_requirement)
return_nodes.append(topology_nodes[0])
break
for i in range(len(vm_nodes), workers_num + 1):
old_vm_name = vm_nodes[0].name
new_vm = copy.deepcopy(vm_nodes[0])
new_vm_name = new_vm.name + '_' + str(i)
new_vm.name = new_vm_name
templates = new_vm.templates.pop(old_vm_name)
new_vm.templates[new_vm_name] = templates
return_nodes.append(new_vm)
for requirement in topology_nodes[0].requirements:
requirement_key = next(iter(requirement))
requirement_value = requirement[requirement_key]
if requirement_value['capability'] == 'tosca.capabilities.ARTICONF.VM':
new_requirement = copy.deepcopy(requirement)
new_requirement[requirement_key]['node'] = new_vm.name
topology_nodes[0].requirements.append(new_requirement)
return_nodes.append(topology_nodes[0])
break
return return_nodes
......@@ -97,32 +99,32 @@ class SimpleAnalyzer(SpecificationAnalyzer):
nodes.append(self.set_default_node_properties(node_template))
return nodes
def set_default_node_properties(self, affected_node):
logging.info('Setting properties for: ' + str(affected_node.type))
ancestors_properties = tosca_helper.get_all_ancestors_properties(affected_node, self.all_node_types,
self.all_custom_def)
default_properties = {}
for ancestors_property in ancestors_properties:
default_property = get_default_value(ancestors_property)
if default_property:
default_properties[next(iter(default_property))] = default_property[next(iter(default_property))]
if default_properties:
for default_property in default_properties:
affected_node.get_properties_objects().append(default_property)
node_name = next(iter(affected_node.templates))
if 'properties' in affected_node.templates[node_name]:
for prop_name in affected_node.templates[node_name]['properties']:
if isinstance(affected_node.templates[node_name]['properties'][prop_name], dict) or \
isinstance(affected_node.templates[node_name]['properties'][prop_name], str):
if 'required' in affected_node.templates[node_name]['properties'][prop_name] and \
affected_node.templates[node_name]['properties'][prop_name]['required'] and \
'default' in affected_node.templates[node_name]['properties'][prop_name] and \
prop_name not in default_properties:
default_properties[prop_name] = affected_node.templates[node_name]['properties'][prop_name][
'default']
logging.info(
'Adding to : ' + str(affected_node.templates[node_name]) + ' properties: ' + str(default_properties))
affected_node.templates[node_name]['properties'] = default_properties
return affected_node
# def set_default_node_properties(self, node):
# logging.info('Setting properties for: ' + str(node.type))
# ancestors_properties = tosca_helper.get_all_ancestors_properties(node, self.all_node_types,
# self.all_custom_def)
# default_properties = {}
# for ancestors_property in ancestors_properties:
# default_property = get_default_value(ancestors_property)
# if default_property:
# default_properties[next(iter(default_property))] = default_property[next(iter(default_property))]
#
# if default_properties:
# for default_property in default_properties:
# node.get_properties_objects().append(default_property)
# node_name = next(iter(node.templates))
# if 'properties' in node.templates[node_name]:
# for prop_name in node.templates[node_name]['properties']:
# if isinstance(node.templates[node_name]['properties'][prop_name], dict) or \
# isinstance(node.templates[node_name]['properties'][prop_name], str):
# if 'required' in node.templates[node_name]['properties'][prop_name] and \
# node.templates[node_name]['properties'][prop_name]['required'] and \
# 'default' in node.templates[node_name]['properties'][prop_name] and \
# prop_name not in default_properties:
# default_properties[prop_name] = node.templates[node_name]['properties'][prop_name][
# 'default']
#
# logging.info(
# 'Adding to : ' + str(node.templates[node_name]) + ' properties: ' + str(default_properties))
# node.templates[node_name]['properties'] = default_properties
# return node
......@@ -15,20 +15,38 @@ from service.spec_service import SpecService
from util import tosca_helper
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
class MyTestCase(unittest.TestCase):
# def test_only_docker_node(self):
# file_name = 'application_example_updated.yaml'
# input_tosca_file_path = self.get_input_tosca_file_path(file_name)
# self.run_test(input_tosca_file_path)
def test_docker(self):
file_name = 'application_example_updated.yaml'
input_tosca_file_path = self.get_input_tosca_file_path(file_name)
self.run_test(input_tosca_file_path)
def test_with_existing_nodes(self):
file_name = 'lifeWatch_vre1.yaml'
def test_kubernetes(self):
file_name = 'kubernetes.yaml'
input_tosca_file_path = self.get_input_tosca_file_path(file_name)
self.run_test(input_tosca_file_path)
def test_topology(self):
file_name = 'topology.yaml'
input_tosca_file_path = self.get_input_tosca_file_path(file_name)
self.run_test(input_tosca_file_path)
def test_compute(self):
file_name = 'compute.yaml'
input_tosca_file_path = self.get_input_tosca_file_path(file_name)
self.run_test(input_tosca_file_path)
# def test_lifeWatch(self):
# file_name = 'lifeWatch_vre1.yaml'
# input_tosca_file_path = self.get_input_tosca_file_path(file_name)
# self.run_test(input_tosca_file_path)
def get_input_tosca_file_path(self, file_name):
tosca_path = "../../TOSCA/"
input_tosca_file_path = tosca_path + file_name
......@@ -46,10 +64,11 @@ class MyTestCase(unittest.TestCase):
test_planner = Planner(input_tosca_file_path, spec_service)
test_tosca_template = test_planner.resolve_requirements()
template_dict = tosca_helper.get_tosca_template_2_topology_template_dictionary(test_tosca_template)
test_tosca_template = test_planner.set_infrastructure_specifications()
test_tosca_template = test_planner.set_node_templates_properties()
template_dict = tosca_helper.get_tosca_template_2_topology_template_dictionary(test_tosca_template)
logger.info("template ----: \n" + yaml.dump(template_dict))
print(yaml.dump(template_dict))
ToscaTemplate(yaml_dict_tpl=copy.deepcopy(template_dict))
test_response = {'toscaTemplate': template_dict}
......@@ -58,5 +77,5 @@ class MyTestCase(unittest.TestCase):
output_current_milli_time = int(round(time.time() * 1000))
response["creationDate"] = output_current_milli_time
response["parameters"] = []
print("Output message:" + json.dumps(response))
# print("Output message:" + json.dumps(response))
self.assertEqual(True, True)
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