Skip to content

Commit

Permalink
Remove old node_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Feb 27, 2024
1 parent 8ccca74 commit 9788d65
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 125 deletions.
96 changes: 1 addition & 95 deletions IM/tosca/Tosca.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,8 +1525,7 @@ def _final_function_result(self, func, node, inf_info=None):

def _find_host_node(self, node, nodetemplates, base_root_type="tosca.nodes.Compute"):
"""
Select the node to host each node, using the node requirements
In most of the cases the are directly specified, otherwise "node_filter" is used
Select the node to host each node
"""

# check for a HosteOn relation
Expand All @@ -1543,99 +1542,6 @@ def _find_host_node(self, node, nodetemplates, base_root_type="tosca.nodes.Compu
else:
return self._find_host_node(n, nodetemplates)

# There are no direct HostedOn node
# check node_filter requirements
if node.requirements and base_root_type == "tosca.nodes.Compute":
for requires in node.requirements:
if 'host' in requires:
value = requires.get('host')
if isinstance(value, dict):
if 'node_filter' in value:
node_filter = value.get('node_filter')
return self._get_compute_from_node_filter(node_filter, nodetemplates)

return None

def _node_fulfill_filter(self, node, node_filter):
"""
Check if a node fulfills the features of a node filter
"""

# Get node properties
node_props = {}
for cap_type in ['os', 'host']:
if node.get_capability(cap_type):
for prop in node.get_capability(cap_type).get_properties_objects():
if prop.value is not None:
unit = None
value = self._final_function_result(prop.value, node)
if prop.name in ['disk_size', 'mem_size']:
value, unit = Tosca._get_size_and_unit(value)
node_props[prop.name] = (value, unit)

filter_props = {}
# Get node_filter properties
for elem in node_filter:
if isinstance(elem, dict):
for cap_type in ['os', 'host']:
if cap_type in elem:
for p in elem.get(cap_type).get('properties'):
p_name = list(p.keys())[0]
p_value = list(p.values())[0]
if isinstance(p_value, dict):
filter_props[p_name] = (list(p_value.keys())[0],
list(p_value.values())[0])
else:
filter_props[p_name] = ("equal", p_value)

operator_map = {
'equal': operator.eq,
'greater_than': operator.gt,
'greater_or_equal': operator.ge,
'less_than': operator.lt,
'less_or_equal': operator.le
}

# Compare the properties
for name, value in filter_props.items():
op, filter_value = value
if name in ['disk_size', 'mem_size']:
filter_value, _ = Tosca._get_size_and_unit(filter_value)

if name in node_props:
node_value, _ = node_props[name]
conv_operator = operator_map.get(op, None)
if conv_operator:
comparation = conv_operator(node_value, filter_value)
else:
if op == "in_range":
comparation = node_value >= filter_value[0] and node_value <= filter_value[1]
elif op == "valid_values":
comparation = node_value in filter_value
else:
Tosca.logger.warning("Logical operator %s not supported." % op)

if not comparation:
return False
else:
# if this property is not specified in the node, return False
# TODO: we must think about default values
return False

return True

def _get_compute_from_node_filter(self, node_filter, nodetemplates):
"""
Select the first node that fulfills the specified "node_filter"
"""

for node in nodetemplates:
root_type = Tosca._get_root_parent_type(node).type

if root_type == "tosca.nodes.Compute":
if self._node_fulfill_filter(node, node_filter.get('capabilities')):
return node

return None

@staticmethod
Expand Down
15 changes: 1 addition & 14 deletions examples/tosca.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,7 @@ topology_template:
properties:
root_password: { get_input: mysql_root_password }
requirements:
- host:
node_filter:
capabilities:
# Constraints for selecting “host” (Container Capability)
- host:
properties:
- num_cpus: { in_range: [1,4] }
- mem_size: { greater_or_equal: 1 GB }
# Constraints for selecting “os” (OperatingSystem Capability)
- os:
properties:
- architecture: { equal: x86_64 }
- type: linux
- distribution: ubuntu
- host: db_server

db_server:
type: tosca.nodes.Compute
Expand Down
16 changes: 0 additions & 16 deletions test/files/tosca_long.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,6 @@ topology_template:
size: 10 GB
type: ssd

mysql:
type: tosca.nodes.DBMS
requirements:
- host:
node_filter:
capabilities:
# Constraints for selecting "host" (Container Capability)
- host:
properties:
- num_cpus: { in_range: [ 1, 4 ] }
- mem_size: { greater_or_equal: 2 GB }
# Constraints for selecting "os" (OperatingSystem Capability)
- os:
properties:
- type: linux

outputs:
galaxy_url:
value: { concat: [ 'http://', get_attribute: [ lrms_server, public_address, 0 ], ':8080' ] }
Expand Down

0 comments on commit 9788d65

Please sign in to comment.