use of org.eclipse.winery.model.tosca.TInterface in project winery by eclipse.
the class InterfacesResource method onPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onPost(List<TInterface> interfaceApiData) {
if (!interfaceApiData.isEmpty()) {
for (TInterface tInt : interfaceApiData) {
if (!tInt.getOperation().isEmpty()) {
for (TOperation tOp : tInt.getOperation()) {
if (tOp.getInputParameters() == null || tOp.getInputParameters().getInputParameter().isEmpty()) {
tOp.setInputParameters(null);
}
if (tOp.getOutputParameters() == null || tOp.getOutputParameters().getOutputParameter().isEmpty()) {
tOp.setOutputParameters(null);
}
}
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("No operation provided!").build();
}
}
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("No interface provided!").build();
}
if (this.res instanceof RelationshipTypeResource) {
TRelationshipType relationshipType = (TRelationshipType) this.res.getElement();
switch(this.interfaceType) {
case "source":
TRelationshipType.SourceInterfaces sourceInterfaces = new TRelationshipType.SourceInterfaces();
sourceInterfaces.getInterface().clear();
sourceInterfaces.getInterface().addAll(interfaceApiData);
relationshipType.setSourceInterfaces(sourceInterfaces);
break;
default:
// it will be target
TRelationshipType.TargetInterfaces targetInterfaces = new TRelationshipType.TargetInterfaces();
targetInterfaces.getInterface().clear();
targetInterfaces.getInterface().addAll(interfaceApiData);
relationshipType.setTargetInterfaces(targetInterfaces);
break;
}
} else if (this.res instanceof NodeTypeResource) {
TNodeType nodeType = (TNodeType) this.res.getElement();
TNodeType.Interfaces interfaces = new TNodeType.Interfaces();
interfaces.getInterface().clear();
interfaces.getInterface().addAll(interfaceApiData);
nodeType.setInterfaces(interfaces);
} else {
throw new IllegalStateException("Interfaces are not supported for this element type!");
}
return RestUtils.persist(this.res);
}
use of org.eclipse.winery.model.tosca.TInterface in project winery by eclipse.
the class GeneratorTest method testMultipleOperationsInOrOut.
@Test
public void testMultipleOperationsInOrOut() throws Exception {
TInterface tinterface = new TInterface();
tinterface.setName("TestInOrOut");
TOperation opIn = new TOperation();
opIn.setName("OpIn");
tinterface.getOperation().add(opIn);
TOperation.InputParameters op1InputParameters = new TOperation.InputParameters();
TParameter op1ip1 = new TParameter();
op1ip1.setName("op1ip1");
op1ip1.setType("xs:string");
op1InputParameters.getInputParameter().add(op1ip1);
TParameter op1ip2 = new TParameter();
op1ip2.setName("op1ip2");
op1ip2.setType("xs:string");
op1InputParameters.getInputParameter().add(op1ip2);
opIn.setInputParameters(op1InputParameters);
TOperation opOut = new TOperation();
opOut.setName("OpOut");
tinterface.getOperation().add(opOut);
TOperation.OutputParameters op1OutputParameters = new TOperation.OutputParameters();
TParameter op1op1 = new TParameter();
op1op1.setName("op1op1");
op1op1.setType("xs:string");
op1OutputParameters.getOutputParameter().add(op1op1);
TParameter op1op2 = new TParameter();
op1op2.setName("op1op2");
op1op1.setType("xs:string");
op1OutputParameters.getOutputParameter().add(op1op2);
opOut.setOutputParameters(op1OutputParameters);
TNodeType nodeType = new TNodeType();
nodeType.setName("test");
nodeType.setTargetNamespace("http://asd.com");
Generator gen = new Generator(tinterface, "org.opentosca.ia", new URL("http://asd.com"), "testname", WORKING_DIR.toFile());
Path generateProject = gen.generateProject();
System.out.println(generateProject);
}
use of org.eclipse.winery.model.tosca.TInterface in project winery by eclipse.
the class GeneratorTest method testNoParams.
@Test
public void testNoParams() throws Exception {
TInterface tinterface = new TInterface();
tinterface.setName("TestNoParams");
TOperation opIn = new TOperation();
opIn.setName("OpNoParams");
tinterface.getOperation().add(opIn);
TNodeType nodeType = new TNodeType();
nodeType.setName("test");
nodeType.setTargetNamespace("http://asd.com");
Generator gen = new Generator(tinterface, "org.opentosca.ia", new URL("http://asd.com"), "testname", WORKING_DIR.toFile());
Path generateProject = gen.generateProject();
System.out.println(generateProject);
}
use of org.eclipse.winery.model.tosca.TInterface in project winery by eclipse.
the class EnhancementUtils method createFeatureNodeType.
/**
* This method merges the Basic-NodeType of the given nodeTemplate with the selected Feature-NodeTypes and generates
* respective implementations.
*
* @param nodeTemplate The NodeTemplate that is updated with the selected features.
* @param featureTypes The list of selected features as generated by {@link #getAvailableFeaturesForTopology(TTopologyTemplate,
* List}.
* @return The mapping of the generated merged NodeType and the QName of the NodeType it replaces.
*/
public static TNodeType createFeatureNodeType(TNodeTemplate nodeTemplate, Map<QName, String> featureTypes) {
IRepository repository = RepositoryFactory.getRepository();
Map<QName, TNodeType> nodeTypes = repository.getQNameToElementMapping(NodeTypeId.class);
Map<QName, TNodeTypeImplementation> nodeTypeImplementations = repository.getQNameToElementMapping(NodeTypeImplementationId.class);
StringBuilder featureNames = new StringBuilder();
featureTypes.values().forEach(featureName -> {
if (!featureNames.toString().isEmpty()) {
featureNames.append("-");
}
featureNames.append(featureName.replaceAll("\\s", "_"));
});
// merge type
String namespace = generateNewGeneratedNamespace(nodeTemplate.getType());
TNodeType featureEnrichedNodeType = nodeTypes.get(nodeTemplate.getType());
featureEnrichedNodeType.setTargetNamespace(namespace);
featureEnrichedNodeType.setName(nodeTemplate.getType().getLocalPart() + "-" + nodeTemplate.getId() + "-" + featureNames + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1");
// prepare Properties
if (Objects.isNull(featureEnrichedNodeType.getWinerysPropertiesDefinition())) {
WinerysPropertiesDefinition props = new WinerysPropertiesDefinition();
props.setPropertyDefinitions(new ArrayList<>());
ModelUtilities.replaceWinerysPropertiesDefinition(featureEnrichedNodeType, props);
}
List<PropertyDefinitionKV> baseProperties = featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
// prepare Interfaces
if (Objects.isNull(featureEnrichedNodeType.getInterfaces())) {
featureEnrichedNodeType.setInterfaces(new ArrayList<>());
}
List<TInterface> baseInterfaces = featureEnrichedNodeType.getInterfaces();
// merge impl accordingly
TNodeTypeImplementation generatedImplementation = new TNodeTypeImplementation.Builder(featureEnrichedNodeType.getName() + "_Impl" + WineryVersion.WINERY_VERSION_SEPARATOR + WineryVersion.WINERY_VERSION_PREFIX + "1", featureEnrichedNodeType.getQName()).build();
// ensure that the lists are initialized
generatedImplementation.setImplementationArtifacts(new ArrayList<>());
generatedImplementation.setDeploymentArtifacts(new ArrayList<>());
Collection<NodeTypeImplementationId> baseTypeImplementations = repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, nodeTemplate.getType());
if (baseTypeImplementations.size() > 0) {
for (NodeTypeImplementationId id : baseTypeImplementations) {
if (Objects.isNull(generatedImplementation.getTargetNamespace())) {
generatedImplementation.setTargetNamespace(generateNewGeneratedNamespace(id.getQName()));
}
addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName()));
}
} else {
// This should never be the case. However, we implement it as a valid fallback.
generatedImplementation.setTargetNamespace(namespace.replace("nodetypes", "nodetypeimplementations"));
}
featureTypes.keySet().forEach(featureTypeQName -> {
TNodeType nodeType = nodeTypes.get(featureTypeQName);
// merge Properties
if (Objects.nonNull(nodeType.getWinerysPropertiesDefinition())) {
List<PropertyDefinitionKV> kvList = nodeType.getWinerysPropertiesDefinition().getPropertyDefinitions();
if (Objects.nonNull(kvList) && !kvList.isEmpty()) {
for (PropertyDefinitionKV kv : kvList) {
boolean listContainsProperty = baseProperties.stream().anyMatch(property -> property.getKey().equals(kv.getKey()));
if (!listContainsProperty) {
baseProperties.add(kv);
}
}
}
}
// merge Interfaces
if (Objects.nonNull(nodeType.getInterfaces()) && !nodeType.getInterfaces().isEmpty()) {
baseInterfaces.addAll(nodeType.getInterfaces());
}
// merge implementations
repository.getAllElementsReferencingGivenType(NodeTypeImplementationId.class, featureTypeQName).forEach(id -> addAllDAsAndIAsToImplementation(generatedImplementation, nodeTypeImplementations.get(id.getQName())));
});
// remove them from the type to ensure a compliant XML.
if (Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition()) && Objects.nonNull(featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions()) && featureEnrichedNodeType.getWinerysPropertiesDefinition().getPropertyDefinitions().isEmpty()) {
ModelUtilities.removeWinerysPropertiesDefinition(featureEnrichedNodeType);
}
try {
repository.setElement(new NodeTypeId(featureEnrichedNodeType.getQName()), featureEnrichedNodeType);
repository.setElement(new NodeTypeImplementationId(generatedImplementation.getQName()), generatedImplementation);
} catch (IOException e) {
logger.error("Error while saving generated definitions.", e);
}
return featureEnrichedNodeType;
}
use of org.eclipse.winery.model.tosca.TInterface in project winery by eclipse.
the class ServiceTemplateResource method generatePlaceholdersWithCapability.
@POST
@Path("placeholder/generator")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response generatePlaceholdersWithCapability() {
Splitting splitting = new Splitting();
TTopologyTemplate topologyTemplate = this.getServiceTemplate().getTopologyTemplate();
if (topologyTemplate == null) {
return Response.notModified().build();
}
try {
// get all open requirements and the respective node templates with open requirements
Map<TRequirement, TNodeTemplate> requirementsAndItsNodeTemplates = splitting.getOpenRequirementsAndItsNodeTemplate(topologyTemplate);
IRepository repo = RepositoryFactory.getRepository();
// iterate over all open requirements
for (Map.Entry<TRequirement, TNodeTemplate> entry : requirementsAndItsNodeTemplates.entrySet()) {
List<PropertyDefinitionKV> propertyDefinitionKVList = new ArrayList<>();
LinkedHashMap<String, String> placeholderNodeTemplateProperties = new LinkedHashMap<>();
// current node template with open requirements
TNodeTemplate nodeTemplateWithOpenReq = entry.getValue();
// get type of node template with open requirements
NodeTypeId id = new NodeTypeId(nodeTemplateWithOpenReq.getType());
TNodeType sourceNodeType = repo.getElement(id);
List<TInterface> sourceNodeTypeInterfaces = sourceNodeType.getInterfaces();
if (sourceNodeTypeInterfaces != null) {
for (TInterface tInterface : sourceNodeTypeInterfaces) {
// TODO: make this more safe
for (TOperation tOperation : tInterface.getOperations()) {
List<TParameter> inputParameters = tOperation.getInputParameters();
if (inputParameters != null) {
for (TParameter inputParameter : inputParameters) {
generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
}
}
}
}
}
List<TRelationshipTemplate> incomingRelationshipTemplates = ModelUtilities.getIncomingRelationshipTemplates(topologyTemplate, nodeTemplateWithOpenReq);
List<TParameter> inputParameters = splitting.getInputParamListofIncomingRelationshipTemplates(topologyTemplate, incomingRelationshipTemplates);
for (TParameter inputParameter : inputParameters) {
String prefixTARGET = "TARGET_";
String prefixSOURCE = "SOURCE_";
String inputParamName = inputParameter.getName();
if (inputParamName.contains(prefixTARGET)) {
inputParamName = inputParamName.replaceAll(prefixTARGET, "");
}
if (inputParamName.contains(prefixSOURCE)) {
inputParamName = inputParamName.replaceAll(prefixSOURCE, "");
}
inputParameter.setName(inputParamName);
generateInputParameters(propertyDefinitionKVList, placeholderNodeTemplateProperties, sourceNodeType, inputParameter);
}
// get required capability type of open requirement
QName capabilityType = splitting.getRequiredCapabilityTypeQNameOfRequirement(entry.getKey());
// create new placeholder node type
TNodeType placeholderNodeType = splitting.createPlaceholderNodeType(nodeTemplateWithOpenReq.getName());
QName placeholderQName = new QName(placeholderNodeType.getTargetNamespace(), placeholderNodeType.getName());
WinerysPropertiesDefinition winerysPropertiesDefinition = sourceNodeType.getWinerysPropertiesDefinition();
// add properties definition
placeholderNodeType.setProperties(null);
if (winerysPropertiesDefinition != null) {
winerysPropertiesDefinition.setPropertyDefinitions(propertyDefinitionKVList);
placeholderNodeType.setProperties(winerysPropertiesDefinition);
String namespace = placeholderNodeType.getWinerysPropertiesDefinition().getNamespace();
NamespaceManager namespaceManager = RepositoryFactory.getRepository().getNamespaceManager();
if (!namespaceManager.hasPermanentProperties(namespace)) {
namespaceManager.addPermanentNamespace(namespace);
}
}
NodeTypeId placeholderId = new NodeTypeId(placeholderQName);
// check if placeholder node type exists
if (repo.exists(placeholderId)) {
// delete and create new
RestUtils.delete(placeholderId);
}
repo.setElement(placeholderId, placeholderNodeType);
// create placeholder node template
TNodeTemplate placeholderNodeTemplate = splitting.createPlaceholderNodeTemplate(topologyTemplate, nodeTemplateWithOpenReq.getName(), placeholderQName);
// create capability of placeholder node template
TCapability capa = splitting.createPlaceholderCapability(topologyTemplate, capabilityType);
ModelUtilities.setPropertiesKV(placeholderNodeTemplate, placeholderNodeTemplateProperties);
if (placeholderNodeTemplate.getCapabilities() == null) {
placeholderNodeTemplate.setCapabilities(new ArrayList<>());
}
placeholderNodeTemplate.getCapabilities().add(capa);
for (Map.Entry<QName, String> targetLocation : nodeTemplateWithOpenReq.getOtherAttributes().entrySet()) {
placeholderNodeTemplate.getOtherAttributes().put(targetLocation.getKey(), targetLocation.getValue());
}
// add placeholder to node template and connect with source node template with open requirements
topologyTemplate.addNodeTemplate(placeholderNodeTemplate);
ModelUtilities.createRelationshipTemplateAndAddToTopology(nodeTemplateWithOpenReq, placeholderNodeTemplate, ToscaBaseTypes.hostedOnRelationshipType, topologyTemplate);
}
LOGGER.debug("PERSISTING");
RestUtils.persist(this);
LOGGER.debug("PERSISTED");
String responseId = this.getServiceTemplate().getId();
return Response.ok().entity(responseId).build();
} catch (Exception e) {
LOGGER.error("Could not fetch requirements and capabilities", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
}
Aggregations