use of org.opentosca.container.core.next.model.PlanType in project container by OpenTOSCA.
the class PlanService method getPlanInstances.
public List<PlanInstance> getPlanInstances(final Csar csar, final PlanType... planTypes) {
final ServiceTemplateInstanceRepository repo = new ServiceTemplateInstanceRepository();
final Collection<ServiceTemplateInstance> serviceInstances = repo.findByCsarId(csar.id());
return serviceInstances.stream().flatMap(sti -> sti.getPlanInstances().stream()).filter(p -> {
final PlanType currentType = PlanType.fromString(p.getType().toString());
return Arrays.stream(planTypes).anyMatch(pt -> pt.equals(currentType));
}).collect(Collectors.toList());
}
use of org.opentosca.container.core.next.model.PlanType in project container by OpenTOSCA.
the class CsarController method transformCsar.
@POST
@javax.ws.rs.Path("/transform")
@ApiOperation(value = "Transform this CSAR to a new CSAR")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response transformCsar(@ApiParam(required = true) final CsarTransformRequest request) {
logger.debug("Invoking transform Csar");
final CsarId sourceCsarId = new CsarId(request.getSourceCsarName());
Csar sourceCsar = this.storage.findById(sourceCsarId);
final CsarId targetCsarId = new CsarId(request.getTargetCsarName());
Csar targetCsar = this.storage.findById(targetCsarId);
Collection<AbstractPlan> plansGenerated = this.planGenerationService.generateTransformationPlans(sourceCsar, targetCsar);
AbstractPlan planGenerated;
if (plansGenerated.isEmpty()) {
return Response.serverError().entity("Couldn't generate transformation plan").build();
} else {
// we should only have one plan here
planGenerated = plansGenerated.iterator().next();
}
Csar storedCsar = storage.findById(sourceCsarId);
List<TPlan> plans = this.storage.findById(sourceCsarId).entryServiceTemplate().getPlans();
TPlan plan = null;
for (TPlan tPlan : plans) {
if (tPlan.getId().equals(planGenerated.getId())) {
plan = tPlan;
break;
}
}
if (plan == null) {
return Response.serverError().entity("Couldn't generate transformation plan").build();
}
this.controlService.invokePlanDeployment(sourceCsarId, storedCsar.entryServiceTemplate(), plans, plan);
PlanType[] planTypes = { PlanType.TRANSFORMATION };
return Response.ok(Utils.getPlanDto(storedCsar, planTypes, plan.getId())).build();
}
use of org.opentosca.container.core.next.model.PlanType in project container by OpenTOSCA.
the class MBJavaApi method situationAdaption.
@Override
public void situationAdaption(Map<String, Object> eventValues) {
LOG.debug("Received SituationAware Adapation Event");
final ServiceTemplateInstance instance = (ServiceTemplateInstance) eventValues.get("SERVICEINSTANCE");
@SuppressWarnings("unchecked") final Map<String, Collection<Long>> nodeIds2situationIds = (Map<String, Collection<Long>>) eventValues.get("NODE2SITUATIONS");
Csar csar = this.storage.findById(instance.getCsarId());
final TTopologyTemplate topology = Lists.newArrayList(csar.entryDefinitions().getServiceTemplates()).get(0).getTopologyTemplate();
final ServiceTemplateInstanceConfiguration currentConfig = getCurrentServiceTemplateInstanceConfiguration(topology, instance);
final ServiceTemplateInstanceConfiguration targetConfig = getValidServiceTemplateInstanceConfiguration(topology, nodeIds2situationIds, csar);
final Collection<String> currentConfigNodeIds = currentConfig.nodeTemplates.stream().map(x -> x.getId()).collect(Collectors.toList());
final Collection<String> currentConfigRelationIds = currentConfig.relationshipTemplates.stream().map(x -> x.getId()).collect(Collectors.toList());
final Collection<String> targetConfigNodeIds = targetConfig.nodeTemplates.stream().map(x -> x.getId()).collect(Collectors.toList());
final Collection<String> targetConfigRelationIds = targetConfig.relationshipTemplates.stream().map(x -> x.getId()).collect(Collectors.toList());
if (currentConfigNodeIds.equals(targetConfigNodeIds) & currentConfigRelationIds.equals(targetConfigRelationIds)) {
LOG.debug("Current configuration is equal to target configuration, no adaptation is needed");
return;
}
final Endpoint endpoint = getAdaptationPlanEndpoint(currentConfigNodeIds, currentConfigRelationIds, targetConfigNodeIds, targetConfigRelationIds);
final String correlationID = String.valueOf(System.currentTimeMillis());
QName planId = null;
PlanType planType = null;
Map<String, String> inputs = null;
if (endpoint != null) {
planId = endpoint.getPlanId();
planType = PlanType.fromString(endpoint.getMetadata().get("PLANTYPE"));
inputs = new HashMap<>();
for (final String input : toStringCollection(endpoint.getMetadata().get("INPUTS"), ",")) {
inputs.put(input, null);
}
} else {
try {
// FIXME the QName conversion of the instance is probably a bad idea
final BPELPlan adaptationPlan = (BPELPlan) this.importer.generateAdaptationPlan(csar, QName.valueOf(instance.getTemplateId()), currentConfigNodeIds, currentConfigRelationIds, targetConfigNodeIds, targetConfigRelationIds);
planType = PlanType.fromString(adaptationPlan.getType().toString());
inputs = createInput(adaptationPlan);
final Path tempFile = Files.createTempFile(adaptationPlan.getId(), ".zip");
this.exporter.exportToPlanFile(tempFile.toUri(), adaptationPlan);
final Map<String, String> endpointMetadata = toEndpointMetadata(currentConfigNodeIds, currentConfigRelationIds, targetConfigNodeIds, targetConfigRelationIds);
endpointMetadata.put("PLANTYPE", planType.toString());
endpointMetadata.put("INPUTS", toCSV(inputs.keySet()));
planId = new QName(tempFile.getFileName().toString());
this.bpelDeployPlugin.deployPlanFile(tempFile, instance.getCsarId(), planId, endpointMetadata);
} catch (final SystemException e) {
LOG.error("Internal error", e);
return;
} catch (final IOException e) {
LOG.error("Couldn't read files", e);
return;
} catch (final JAXBException e) {
LOG.error("Couldn't parse files", e);
return;
}
}
final Map<String, String> requestBody = createRequestBody(instance.getCsarId(), instance.getTemplateId(), instance.getId(), inputs, correlationID);
// FIXME QName natural key replacement leftover!
invokePlan("adapt", correlationID, instance.getId(), QName.valueOf(instance.getTemplateId()), requestBody, instance.getCsarId(), planId, BPELNS);
}
use of org.opentosca.container.core.next.model.PlanType in project container by OpenTOSCA.
the class ServiceTemplateController method transformCsar.
@POST
@Path("/{servicetemplate}/transform")
@ApiOperation(value = "Generates a plan to adapt service template instances via the given the source and target nodes/relations")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response transformCsar(@ApiParam("ID of CSAR") @PathParam("csar") final String csar, @ApiParam("qualified name of the service template") @PathParam("servicetemplate") final String serviceTemplateId, @ApiParam(required = true) final ServiceTransformRequest request) {
CsarId csarId = new CsarId(csar);
Csar csarToTransform = this.storage.findById(csarId);
final AdaptationPlanGenerationResult result = this.planGenerationService.generateAdaptationPlan(csarToTransform, QName.valueOf(serviceTemplateId), request.getSourceNodeTemplates(), request.getSourceRelationshipTemplates(), request.getTargetNodeTemplates(), request.getTargetRelationshipTemplates());
if (result == null) {
return Response.serverError().build();
}
// FIXME maybe this only makes sense when we have generated plans :/
this.controlService.declareStored(result.csarId);
boolean success = this.controlService.invokeToscaProcessing(result.csarId);
Csar csarFile = this.storage.findById(result.csarId);
List<TPlan> plans = csarFile.entryServiceTemplate().getPlans();
TPlan plan = null;
for (TPlan tPlan : plans) {
if (tPlan.getId().equals(result.planId)) {
plan = tPlan;
break;
}
}
this.controlService.invokePlanDeployment(result.csarId, csarFile.entryServiceTemplate(), plans, plan);
if (success) {
PlanType[] planTypes = { PlanType.TRANSFORMATION };
return Response.ok(Utils.getPlanDto(storage.findById(result.csarId), planTypes, result.planId)).build();
} else {
return Response.serverError().build();
}
}
Aggregations