use of org.opentosca.container.core.common.NotFoundException in project container by OpenTOSCA.
the class ManagementPlanController method addManagementPlanLogEntry.
@POST
@Path("/{plan}/instances/{instance}/logs")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML })
@ApiOperation(hidden = true, value = "")
public Response addManagementPlanLogEntry(@PathParam("plan") final String plan, @PathParam("instance") final String instance, @Context final UriInfo uriInfo, final CreatePlanInstanceLogEntryRequest logEntry) throws NotFoundException {
final String entry = logEntry.getLogEntry();
if (entry == null || entry.length() <= 0) {
LOGGER.error("Log entry is empty!");
return Response.status(Status.BAD_REQUEST).build();
}
PlanInstance pi = planInstanceService.getPlanInstanceWithLogsByCorrelationId(instance);
if (pi == null) {
LOGGER.error("No plan instance found");
throw new NotFoundException("No plan instance found");
}
final PlanInstanceEvent event = new PlanInstanceEvent("INFO", "PLAN_LOG", entry);
planInstanceService.addLogToPlanInstance(pi, event);
final URI resourceUri = uriInfo.getAbsolutePath();
return Response.ok(resourceUri).build();
}
use of org.opentosca.container.core.common.NotFoundException in project container by OpenTOSCA.
the class ManagementBusServiceImpl method internalInvokeIA.
/**
* Searches for the NodeType/RelationshipType of the given operation, updates the input parameters and passes the
* request on to invoke the corresponding IA.
*
* @param exchange exchange containing the header fields which identify the current operation
* @param arguments a bundle-object containing all relevant invocation arguments
*/
private PlanInstanceEvent internalInvokeIA(IAInvocationArguments arguments, Exchange exchange) {
LOG.debug("Starting Management Bus: InvokeIA");
final Message message = exchange.getIn();
// log event to monitor the IA execution time
final PlanInstanceEvent event = new PlanInstanceEvent("INFO", "IA_DURATION_LOG", "Finished execution of IA for NodeTemplate '" + arguments.nodeTemplateId + "' interface '" + arguments.interfaceName + "' and operation '" + arguments.operationName + "'");
final Csar csar = this.storage.findById(arguments.csarId);
final TServiceTemplate serviceTemplate = csar.entryServiceTemplate();
QName typeID = null;
TEntityType type = null;
if (Objects.nonNull(arguments.nodeTemplateId)) {
final TNodeTemplate nodeTemplate = serviceTemplate.getTopologyTemplate().getNodeTemplate(arguments.nodeTemplateId);
if (nodeTemplate != null) {
typeID = nodeTemplate.getType();
}
type = csar.nodeTypes().stream().filter(x -> x.getQName().equals(nodeTemplate.getTypeAsQName())).findFirst().orElse(null);
} else if (Objects.nonNull(arguments.relationshipTemplateId)) {
final TRelationshipTemplate relationshipTemplate = serviceTemplate.getTopologyTemplate().getRelationshipTemplate(arguments.relationshipTemplateId);
if (relationshipTemplate != null) {
typeID = relationshipTemplate.getType();
}
type = csar.relationshipTypes().stream().filter(x -> x.getQName().equals(relationshipTemplate.getTypeAsQName())).findFirst().orElse(null);
}
if (typeID == null) {
LOG.error(String.format("Could not resolve a type for the given nodeTemplateId/relationshipTemplateId [%s/%s]", arguments.nodeTemplateId, arguments.relationshipTemplateId));
handleResponse(exchange);
event.setEndTimestamp(new Date());
return event;
}
// invocation is only possible with retrieved type which contains the operation
if (!Objects.nonNull(typeID) || !Objects.nonNull(type)) {
LOG.error("Unable to retrieve the NodeType/RelationshipType for NodeTemplate: {} and RelationshipTemplate: {}", arguments.nodeTemplateId, arguments.relationshipTemplateId);
handleResponse(exchange);
event.setEndTimestamp(new Date());
return event;
}
// get NodeTemplateInstance object for the deployment distribution decision
NodeTemplateInstance nodeInstance;
final RelationshipTemplateInstance relationshipInstance;
if (Objects.nonNull(arguments.nodeTemplateId)) {
nodeInstance = mbUtils.getNodeTemplateInstance(arguments.serviceTemplateInstanceId, arguments.nodeTemplateId);
relationshipInstance = null;
} else if (Objects.nonNull(arguments.relationshipTemplateId)) {
relationshipInstance = mbUtils.getRelationshipTemplateInstance(arguments.serviceTemplateInstanceId, arguments.relationshipTemplateId);
// assuming type is a TRelationshipType, because otherwise this should be unreachable
final TRelationshipType relationshipType = (TRelationshipType) type;
if (Objects.nonNull(relationshipInstance) && Objects.nonNull(relationshipType)) {
nodeInstance = ContainerEngine.resolveRelationshipOperationTarget(relationshipInstance, relationshipType, arguments.interfaceName, arguments.operationName);
} else {
nodeInstance = null;
}
} else {
relationshipInstance = null;
nodeInstance = null;
}
Csar replacementCsar = null;
if (typeID.equals(Types.abstractOperatingSystemNodeType)) {
// replace abstract operating system node instance
nodeInstance = mbUtils.getAbstractOSReplacementInstance(nodeInstance);
// if not, we're fucked anyways
assert nodeInstance != null;
final ServiceTemplateInstance replacementSTI = nodeInstance.getServiceTemplateInstance();
replacementCsar = this.storage.findById(replacementSTI.getCsarId());
try {
final TServiceTemplate replacementST = ToscaEngine.resolveServiceTemplate(replacementCsar, replacementSTI.getTemplateId());
final TNodeTemplate replacementTemplate = ToscaEngine.resolveNodeTemplate(replacementST, nodeInstance.getTemplateId());
type = ToscaEngine.resolveNodeType(replacementCsar, replacementTemplate);
} catch (final NotFoundException e) {
LOG.error("Could not compute replacing type for abstract Operating System Node replacement. Aborting IA invocation.", e);
handleResponse(exchange);
event.setEndTimestamp(new Date());
return event;
}
}
// update input parameters for the operation call
if (message.getBody() instanceof HashMap) {
@SuppressWarnings("unchecked") Map<String, String> inputParams = (Map<String, String>) message.getBody();
inputParams = this.parameterHandler.updateInputParams(inputParams, replacementCsar == null ? csar : replacementCsar, nodeInstance, relationshipInstance, arguments.interfaceName, arguments.operationName);
message.setBody(inputParams);
} else {
LOG.warn("There are no input parameters specified.");
}
internalInvokeIA(exchange, replacementCsar != null ? replacementCsar : csar, arguments.serviceTemplateInstanceId, type, nodeInstance, arguments.interfaceName, arguments.operationName);
event.setEndTimestamp(new Date());
return event;
}
use of org.opentosca.container.core.common.NotFoundException in project container by OpenTOSCA.
the class ManagementBusServiceImpl method internalInvokeIA.
/**
* Searches the right IA for the given operation and invokes it with the given parameters.
*
* @param exchange exchange containing the input parameters of the operation
* @param csar the CSAR
* @param serviceTemplateInstanceID ID of the service instance
* @param type NodeType/RelationshipType that implements the operation
* @param nodeTemplateInstance NodeTemplateInstance for the deployment distribution decision
* @param neededInterface the interface of the searched operation
* @param neededOperation the searched operation
*/
private void internalInvokeIA(final Exchange exchange, final Csar csar, final Long serviceTemplateInstanceID, final TEntityType type, final NodeTemplateInstance nodeTemplateInstance, final String neededInterface, final String neededOperation) {
LOG.debug("NodeType/RelationshipType: {}", type.getQName());
final Message message = exchange.getIn();
// check whether operation has output parameters
final boolean hasOutputParams;
try {
final TInterface nodeTypeInterface = ToscaEngine.resolveInterface(csar, type, neededInterface);
final TOperation operation = ToscaEngine.resolveOperation(nodeTypeInterface, neededOperation);
hasOutputParams = operation.getOutputParameters() != null && !operation.getOutputParameters().isEmpty();
} catch (final NotFoundException notFound) {
LOG.error("Tried to invoke unknown operation '{}' in interface '{}!", neededOperation, neededInterface);
return;
}
message.setHeader(MBHeader.HASOUTPUTPARAMS_BOOLEAN.toString(), hasOutputParams);
final List<? extends TEntityTypeImplementation> typeImplementations = ToscaEngine.getTypeImplementations(csar, type);
LOG.debug("List of Node/RelationshipTypeImplementations: {}", typeImplementations.toString());
// invokable by available plug-ins
for (final TEntityTypeImplementation implementation : typeImplementations) {
message.setHeader(MBHeader.TYPEIMPLEMENTATIONID_QNAME.toString(), implementation.getQName());
final List<? extends TImplementationArtifact> ias = Optional.ofNullable(implementation.getImplementationArtifacts()).orElse(Collections.emptyList());
LOG.debug("List of Implementation Artifacts: {}", ias.stream().map(ia -> {
return String.format("{%s, %s %s}", ia.getIdFromIdOrNameField(), ia.getOperationName(), ia.getArtifactRef());
}).collect(Collectors.joining(", ")));
for (final TImplementationArtifact ia : ias) {
// try to invoke the operation on the current IA
if (invokeIAOperation(exchange, csar, serviceTemplateInstanceID, type, nodeTemplateInstance, implementation, ia, neededInterface, neededOperation)) {
LOG.info("Successfully invoked Operation {} on IA {}", neededOperation, ia.getName());
return;
}
}
}
LOG.warn("No invokable implementation artifact found that provides required interface/operation.");
handleResponse(exchange);
}
use of org.opentosca.container.core.common.NotFoundException in project container by OpenTOSCA.
the class ManagementBusInvocationPluginScript method invoke.
@Override
public Exchange invoke(final Exchange exchange) {
LOG.debug("Management Bus Script Plugin getting information...");
final Message message = exchange.getIn();
final CsarId csarID = message.getHeader(MBHeader.CSARID.toString(), CsarId.class);
LOG.debug("CsarID: {}", csarID);
final QName artifactTemplateID = message.getHeader(MBHeader.ARTIFACTTEMPLATEID_QNAME.toString(), QName.class);
LOG.debug("ArtifactTemplateID: {}", artifactTemplateID);
final String relationshipTemplateID = message.getHeader(MBHeader.RELATIONSHIPTEMPLATEID_STRING.toString(), String.class);
LOG.debug("RelationshipTemplateID: {}", relationshipTemplateID);
final QName serviceTemplateID = message.getHeader(MBHeader.SERVICETEMPLATEID_QNAME.toString(), QName.class);
LOG.debug("ServiceTemplateID: {}", serviceTemplateID);
final String interfaceName = message.getHeader(MBHeader.INTERFACENAME_STRING.toString(), String.class);
LOG.debug("InterfaceName: {}", interfaceName);
final String operationName = message.getHeader(MBHeader.OPERATIONNAME_STRING.toString(), String.class);
LOG.debug("OperationName: {}", operationName);
final Csar csar = storage.findById(csarID);
try {
final TServiceTemplate serviceTemplate = ToscaEngine.resolveServiceTemplate(csar, serviceTemplateID);
final TArtifactTemplate artifactTemplate = ToscaEngine.resolveArtifactTemplate(csar, artifactTemplateID);
final TArtifactType artifactType = ToscaEngine.resolveArtifactType(csar, artifactTemplate.getType());
// the relationship template does not need to be present
final TRelationshipTemplate relationshipTemplate = ToscaEngine.getRelationshipTemplate(serviceTemplate, relationshipTemplateID).orElse(null);
final TNodeTemplate nodeTemplate = getNodeTemplate(message, csar, relationshipTemplate, serviceTemplate, interfaceName, operationName);
final TNodeType nodeType = ToscaEngine.resolveNodeTypeReference(csar, nodeTemplate.getType());
final TOperation operation = ToscaEngine.resolveOperation(csar, nodeType, interfaceName, operationName);
return handleExchangeInternal(exchange, message, csarID, serviceTemplateID, csar, serviceTemplate, artifactTemplate, artifactType, nodeTemplate, nodeType, operation);
} catch (NotFoundException | UnsupportedEncodingException e) {
LOG.warn("Failed to resolve a strongly typed CSAR content reference, invocation failed!", e);
return exchange;
}
}
use of org.opentosca.container.core.common.NotFoundException in project container by OpenTOSCA.
the class MBUtils method getOperatingSystemNodeTemplate.
/**
* Finds the operating system node template, optionally requiring that it has a NodeInstance associated with a given
* serviceTemplateInstanceId.
*
* @return The OperatingSystem NodeTemplate.
*/
@Nullable
public TNodeTemplate getOperatingSystemNodeTemplate(final Csar csar, final TServiceTemplate serviceTemplate, final TNodeTemplate nodeTemplate, boolean mustHaveNodeInstance, Long serviceTemplateInstanceId) throws NotFoundException {
// Need to do exhaustive checking of all osNodeTypes for NodeInstance criteria
final Queue<TNodeTemplate> osNodeTemplates = new LinkedList<>();
final Queue<TNodeTemplate> nodeTemplateGraph = new LinkedList<>();
final Set<TNodeTemplate> traversedTemplates = new HashSet<>();
nodeTemplateGraph.add(nodeTemplate);
while (!nodeTemplateGraph.isEmpty()) {
final TNodeTemplate current = nodeTemplateGraph.poll();
if (!traversedTemplates.add(current)) {
// skip templates we already traversed
continue;
}
final TNodeType currentNodeType = ToscaEngine.resolveNodeType(csar, current);
if (isOperatingSystemNodeType(csar, currentNodeType)) {
// just return the first result if we don't need to check for a node instance
if (!mustHaveNodeInstance) {
return current;
}
osNodeTemplates.add(current);
continue;
}
// nodeType was not an OS node type, therefore traverse the Graph "downwards"
ToscaEngine.getRelatedNodeTemplates(serviceTemplate, current, Types.hostedOnRelationType, Types.deployedOnRelationType, Types.dependsOnRelationType).filter(t -> !traversedTemplates.contains(t)).forEach(nodeTemplateGraph::add);
}
// return the first result that has an instance
for (TNodeTemplate osTemplate : osNodeTemplates) {
if (getNodeTemplateInstance(serviceTemplateInstanceId, osTemplate) != null) {
return osTemplate;
}
}
return null;
}
Aggregations