Commit adf701b9 authored by Spiros Koulouzis's avatar Spiros Koulouzis

added method to get interface instance with default values

parent 95ab9ebe
......@@ -66,10 +66,6 @@ public class ToscaHelper {
return id;
}
// public ToscaHelper(ToscaTemplate toscaTemplate, String sureToscaBasePath) throws JsonProcessingException, IOException, ApiException {
// init(sureToscaBasePath);
// uploadToscaTemplate(toscaTemplate);
// }
private void init(String sureToscaBasePath) {
Configuration.getDefaultApiClient().setBasePath(sureToscaBasePath);
api = new DefaultApi(Configuration.getDefaultApiClient());
......@@ -223,12 +219,28 @@ public class ToscaHelper {
return toscaTemplate;
}
public NodeTemplateMap setProvisionerInterfaceInVMTopology(NodeTemplateMap vmTopologyMap, Provisioner provisioner) throws ApiException {
public NodeTemplateMap setProvisionerInterfaceInVMTopology(NodeTemplateMap vmTopologyMap, Provisioner provisioner, String operation) throws ApiException {
List<String> toscaInterfaceTypes = new ArrayList<>();
toscaInterfaceTypes.add(provisioner.getToscaInterfaceType());
List<Map<String, Object>> definitions = getProvisionInterfaceDefinitions(toscaInterfaceTypes);
Map<String, Object> definition = getBestProvisionInterfaceDefinition(definitions);
Map<String, Object> provisionInterface = getProvisionInterfaceInstance(definition, operation);
return null;
}
private Map<String, Object> getBestProvisionInterfaceDefinition(List<Map<String, Object>> definitions) {
for (Map<String, Object> def : definitions) {
if (def.containsKey("tosca.interfaces.ARTICONF.CloudsStorm")) {
return def;
}
}
return null;
}
private Map<String, Object> getProvisionInterfaceInstance(Map<String, Object> definition, String operation) {
Map<String, Object> provisionInterface = new HashMap<>();
Map<String, Object> inputs = (Map<String, Object>) definition.get("inputs");
}
}
......@@ -100,7 +100,8 @@ public class ToscaHelperTest {
}
/**
* Test of getSupportedProvisionInterfaceDefinitions method, of class ToscaHelper.
* Test of getSupportedProvisionInterfaceDefinitions method, of class
* ToscaHelper.
*
* @throws nl.uva.sne.drip.sure.tosca.client.ApiException
*/
......@@ -109,7 +110,9 @@ public class ToscaHelperTest {
if (serviceUp) {
System.out.println("getProvisionInterfaceDefinitions");
String expected = "tosca.interfaces.ARTICONF.CloudsStorm";
List<Map<String, Object>> result = instance.getSupportedProvisionInterfaceDefinitions();
List<String> toscaInterfaceTypes = new ArrayList<>();
toscaInterfaceTypes.add(expected);
List<Map<String, Object>> result = instance.getProvisionInterfaceDefinitions(toscaInterfaceTypes);
assertNotNull(result);
String key = result.get(0).keySet().iterator().next();
// assertEquals(expected, key);
......
......@@ -48,6 +48,7 @@ public class DRIPService {
@Autowired
ProvisionerService provisionerService;
private static final String OPERATION_PROVISION = "provision";
private String execute(ToscaTemplate toscaTemplate) throws JsonProcessingException, ApiException, Exception {
......@@ -120,11 +121,11 @@ public class DRIPService {
String ymlToscaTemplate = toscaTemplateService.findByID(id);
ToscaTemplate toscaTemplate = toscaTemplateService.getYaml2ToscaTemplate(ymlToscaTemplate);
toscaTemplate = addCredentials(toscaTemplate);
toscaTemplate = addProvisionInterface(toscaTemplate);
toscaTemplate = addProvisionInterface(toscaTemplate, OPERATION_PROVISION);
return execute(toscaTemplate);
}
private ToscaTemplate addProvisionInterface(ToscaTemplate toscaTemplate) throws IOException, JsonProcessingException, ApiException, Exception {
private ToscaTemplate addProvisionInterface(ToscaTemplate toscaTemplate, String operation) throws IOException, JsonProcessingException, ApiException, Exception {
helper.uploadToscaTemplate(toscaTemplate);
List<NodeTemplateMap> vmTopologies = helper.getVMTopologyTemplates();
List<Provisioner> provisioners = null;
......@@ -132,7 +133,7 @@ public class DRIPService {
provisioners = provisionerService.findAll();
if (provisioners != null && provisioners.size() > 0) {
Provisioner provisioner = getBestProvisioners(vmTopologyMap.getNodeTemplate(), provisioners);
vmTopologyMap = helper.setProvisionerInterfaceInVMTopology(vmTopologyMap, provisioner);
vmTopologyMap = helper.setProvisionerInterfaceInVMTopology(vmTopologyMap, provisioner, operation);
toscaTemplate = helper.setVMTopologyInToscaTemplate(toscaTemplate, vmTopologyMap);
}
}
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="e478ccae-5352-4e8e-9efb-3f5cda44e877" name="Default Changelist" comment="" />
<list default="true" id="e478ccae-5352-4e8e-9efb-3f5cda44e877" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/../drip-commons/src/main/java/nl/uva/sne/drip/commons/utils/ToscaHelper.java" beforeDir="false" afterPath="$PROJECT_DIR$/../drip-commons/src/main/java/nl/uva/sne/drip/commons/utils/ToscaHelper.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../drip-commons/src/test/java/nl/uva/sne/drip/commons/utils/ToscaHelperTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/../drip-commons/src/test/java/nl/uva/sne/drip/commons/utils/ToscaHelperTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../drip-provisioner/src/main/java/nl/uva/sne/drip/provisioner/CloudStormService.java" beforeDir="false" afterPath="$PROJECT_DIR$/../drip-provisioner/src/main/java/nl/uva/sne/drip/provisioner/CloudStormService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/controllers/default_controller.py" beforeDir="false" afterPath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/controllers/default_controller.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_helper.py" beforeDir="false" afterPath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_helper.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_template_service.py" beforeDir="false" afterPath="$PROJECT_DIR$/../sure_tosca-flask-server/sure_tosca/service/tosca_template_service.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
......
......@@ -159,7 +159,6 @@ class CloudStormService {
}
cloudsStormMap.put("cloud_storm_vm", cloudsStormVMsList);
cloudsStormMap.put("cloud_storm_subtopologies", cloudsStormSubTopologies);
return cloudsStormMap;
}
......
......@@ -62,6 +62,46 @@ def get_ancestors_requirements(id, node_name): # noqa: E501
return 'Not Found', 404
def get_default_interface(id, interface_type, instance_name, operation_name): # noqa: E501
"""
returns an interface instance with the default required values. # noqa: E501
:param id: ID of topolog template uplodaed
:type id: str
:param interface_type: type to instantiate
:type interface_type: str
:param instance_name: the name of the instance to retrun
:type instance_name: str
:param operation_name: the name of operation
:type operation_name: str
:rtype: Dict[str, object]
"""
res = tosca_template_service.get_default_interface(id, interface_type, instance_name,operation_name)
if res:
return res
return 'Not Found', 404
def get_default_node_type(id, node_type, instance_name): # noqa: E501
"""
returns an node templaye instance with the default required values. # noqa: E501
:param id: ID of topolog template uplodaed
:type id: str
:param node_type: type to instantiate
:type node_type: str
:param instance_name: the name of tghe instance to retrun
:type instance_name: str
:rtype: NodeTemplateMap
"""
return 'do some magic!'
def get_dsl_definitions(id, anchors=None, derived_from=None): # noqa: E501
"""
......@@ -153,7 +193,8 @@ def get_node_requirements(id, node_name): # noqa: E501
def get_node_templates(id, type_name=None, node_name=None, has_interfaces=None, has_properties=None,
has_attributes=None, has_requirements=None, has_capabilities=None, has_artifacts=None): # noqa: E501
has_attributes=None, has_requirements=None, has_capabilities=None,
has_artifacts=None): # noqa: E501
"""get_node_templates
returns nodes templates in topology # noqa: E501
......@@ -402,4 +443,3 @@ def get_node_attributes(id, node_name): # noqa: E501
if res:
return res
return 'Not Found', 404
......@@ -55,7 +55,7 @@ def get_node_requirements(node):
return node_requirements
def get_parent_type(node):
def get_node_template_parent_type(node):
if isinstance(node, NodeTemplate):
if node.parent_type:
parent_type = node.parent_type.type
......@@ -106,9 +106,8 @@ def get_node_types_with_interface(nodes):
return node_types_with_interface
def node_dict_2_node_template(node_name,node_dict, all_custom_def):
node_dict = {node_name:node_dict}
def node_dict_2_node_template(node_name, node_dict, all_custom_def):
node_dict = {node_name: node_dict}
# node_type = node_dict[node_name]['type']
# for name_to_remove in node_type_key_names_to_remove:
......@@ -216,7 +215,7 @@ def get_nodes_by_type(node_type, nodes, all_node_types, all_custom_def):
def get_all_ancestors_types(child_node, all_node_types, all_custom_def, ancestors_types=None):
if not ancestors_types:
ancestors_types = [get_node_type_name(child_node)]
parent_type = get_parent_type(child_node)
parent_type = get_node_template_parent_type(child_node)
if parent_type:
ancestors_types.append(parent_type)
parent_type = node_type_2_node_template({'name': all_node_types[parent_type]}, all_custom_def)
......@@ -224,7 +223,8 @@ def get_all_ancestors_types(child_node, all_node_types, all_custom_def, ancestor
return ancestors_types
def get_all_ancestors_properties(node, all_nodes_templates, all_custom_def, ancestors_properties=None, ancestors_types=None):
def get_all_ancestors_properties(node, all_nodes_templates, all_custom_def, ancestors_properties=None,
ancestors_types=None):
if not ancestors_properties:
ancestors_properties = []
ancestors_properties_names = []
......@@ -293,4 +293,7 @@ def get_node_template_dict(node_template):
# print(dir(node_template))
# print(node_template.templates)
return node_template_dict
\ No newline at end of file
return node_template_dict
......@@ -285,7 +285,7 @@ def get_parent_type_name(id, node_root_key):
the_node, all_node_types, all_custom_def = node_dict_2_node_template(id, node_root_key)
except Exception as e:
return None
return tosca_helper.get_parent_type(the_node)
return tosca_helper.get_node_template_parent_type(the_node)
def get_node_outputs(id, node_name):
......@@ -385,3 +385,64 @@ def get_types(id, kind_of_type, has_interfaces, type_name, has_properties, has_a
if kind_of_type == 'interface_types':
return get_interface_types(id, interface_type=type_name)
return None
def get_default_entry_value(entry):
if 'default' in entry and 'required' in entry and entry['required']:
return entry['default']
return None
def get_all_interface_types(id, interface_type, parent_interfaces=None):
if parent_interfaces is None:
parent_interfaces = []
interface = get_interface_types(id, interface_type=interface_type)[0]
parent_interfaces.append(interface)
if 'derived_from' in interface[interface_type]:
return get_all_interface_types(id, interface[interface_type]['derived_from'], parent_interfaces)
else:
return parent_interfaces
def merge_interfaces(id, interface_type):
all_interfaces = get_all_interface_types(id, interface_type=interface_type)
if all_interfaces is None:
return None
all_inputs= {}
all_operations = {}
for interface in all_interfaces:
interface = interface[next(iter(interface))]
if 'inputs' in interface:
all_inputs.update(interface['inputs'])
for op in interface:
if op != 'description' and op != 'derived_from' and op != 'inputs':
all_operations[op] = interface[op]
the_interface = {interface_type: all_operations}
the_interface[interface_type]['inputs'] = all_inputs
return the_interface
def get_default_interface(id, interface_type, instance_name, operation_name):
the_interface = merge_interfaces(id, interface_type)
if the_interface is None:
return None
interface = the_interface[next(iter(the_interface))]
if operation_name not in interface:
raise Exception(
'Operation: ' + operation_name + ' not in interface: ' + interface_type + ' definition: ' + str(
the_interface))
inputs = interface['inputs']
instance_inputs_list = []
for key in inputs:
default_value = get_default_entry_value(inputs[key])
default_entry = {key: default_value}
instance_inputs_list.append(default_entry)
instance_inputs = {'inputs': instance_inputs_list}
operation = {operation_name: instance_inputs}
instance = {instance_name: operation}
return instance
......@@ -126,6 +126,80 @@ paths:
"405":
description: "Invalid input"
x-swagger-router-controller: "sure_tosca.controllers.default_controller"
/tosca_template/{id}/interface/{interface_type}/defaults:
get:
summary: ""
description: "returns an interface instance with the default required values."
operationId: "get_default_interface"
produces:
- "application/json"
parameters:
- name: "id"
in: "path"
description: "ID of topolog template uplodaed"
required: true
type: "string"
- name: "interface_type"
in: "path"
description: "type to instantiate"
required: true
type: "string"
- name: "instance_name"
in: "query"
description: "the name of the instance to retrun"
required: true
type: "string"
- name: "operation_name"
in: "query"
description: "the name of operation"
required: true
type: "string"
responses:
"200":
description: "successful operation"
schema:
type: "object"
additionalProperties:
type: "object"
properties: {}
"404":
description: "Not found"
"405":
description: "Invalid input"
x-swagger-router-controller: "sure_tosca.controllers.default_controller"
/tosca_template/{id}/node_types/{node_type}/defaults:
get:
summary: ""
description: "returns an node templaye instance with the default required values."
operationId: "get_default_node_type"
produces:
- "application/json"
parameters:
- name: "id"
in: "path"
description: "ID of topolog template uplodaed"
required: true
type: "string"
- name: "node_type"
in: "path"
description: "type to instantiate"
required: true
type: "string"
- name: "instance_name"
in: "query"
description: "the name of tghe instance to retrun"
required: true
type: "string"
responses:
"200":
description: "successful operation"
schema:
$ref: "#/definitions/NodeTemplateMap"
"404":
description: "Not found"
"405":
description: "Invalid input"
x-swagger-router-controller: "sure_tosca.controllers.default_controller"
/tosca_template/{id}/types:
get:
summary: ""
......
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