use of org.opentosca.toscana.core.parse.model.Entity in project TOSCAna by StuPro-TOSCAna.
the class LinkResolver method resolveRequirements.
private static void resolveRequirements(ServiceGraph graph) {
logger.debug(LogFormat.indent(1, "requirements"));
Iterator<Entity> nodeIt = graph.iterator(ToscaStructure.NODE_TEMPLATES);
while (nodeIt.hasNext()) {
Entity nodeEntity = nodeIt.next();
Optional<Entity> requirementsEntity = nodeEntity.getChild(RootNode.REQUIREMENTS.name);
if (requirementsEntity.isPresent()) {
Collection<Entity> requirements = requirementsEntity.get().getChildren();
for (Entity requirement : requirements) {
MappingEntity mappingRequirement = (MappingEntity) requirement;
ScalarEntity fulfillerEntity = (ScalarEntity) mappingRequirement.getChild(Requirement.NODE_NAME).get();
String fulfillerName = fulfillerEntity.getValue();
EntityId fulfillerId = ToscaStructure.NODE_TEMPLATES.descend(fulfillerName);
logger.debug(LogFormat.pointAt(2, mappingRequirement.getId() + "." + Requirement.NODE_NAME, fulfillerId));
Entity fulfiller = graph.getEntity(fulfillerId).orElseThrow(() -> new ToscaTemplateException(String.format("No node with name '%s' found, but required as fulfiller in requirement", fulfillerName)));
graph.removeVertex(fulfillerEntity);
graph.addConnection(mappingRequirement, fulfiller, Requirement.NODE_NAME);
}
}
}
}
use of org.opentosca.toscana.core.parse.model.Entity in project TOSCAna by StuPro-TOSCAna.
the class ScalarTypeConverter method convertScalarEntity.
/**
* Converts the value attached to given {@code scalarEntity } to an instance of a type which is specified by given {@code key}.
*/
static <T> T convertScalarEntity(ScalarEntity scalarEntity, ToscaKey<T> key, Entity parent) {
String value = scalarEntity.getValue();
Class targetType = key.getType();
if (String.class.isAssignableFrom(targetType)) {
return (T) value;
} else if (Integer.class.isAssignableFrom(targetType)) {
Integer number;
if (UNBOUNDED.equals(value)) {
number = Integer.MAX_VALUE;
} else {
number = Integer.valueOf(value);
}
return (T) number;
} else if (Boolean.class.isAssignableFrom(targetType)) {
return (T) Boolean.valueOf(value);
} else if (targetType.isEnum()) {
Map<String, T> enumMap = EnumUtils.getEnumMap(targetType);
Optional<T> result = enumMap.entrySet().stream().filter(entry -> value.equalsIgnoreCase(entry.getKey())).map(Map.Entry::getValue).findAny();
return result.orElseThrow(() -> new NoSuchElementException(String.format("No value with name '%s' in enum '%s'", value, targetType.getSimpleName())));
} else if (OperationVariable.class.isAssignableFrom(targetType)) {
Connection c = scalarEntity.getGraph().getEdge(parent, scalarEntity);
String name = null;
OperationVariable var;
if (c != null) {
name = c.getKey();
var = new OperationVariable(scalarEntity, name);
} else {
var = new OperationVariable(scalarEntity);
}
return (T) var;
} else if (SizeUnit.class.isAssignableFrom(targetType)) {
SizeUnit.Unit fromDefaultUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.FROM);
SizeUnit.Unit toUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.TO);
if (fromDefaultUnit == null || toUnit == null) {
throw new IllegalStateException("ToscaKey defining a SizeUnit is illegal: No directive set for source and target units");
}
return (T) SizeUnit.convert(value, fromDefaultUnit, toUnit);
} else if (Port.class.isAssignableFrom(targetType)) {
return (T) new Port(Integer.valueOf(value));
} else {
throw new UnsupportedOperationException(String.format("Cannot convert value of type %s: currently unsupported", targetType.getSimpleName()));
}
}
use of org.opentosca.toscana.core.parse.model.Entity in project TOSCAna by StuPro-TOSCAna.
the class NavigationUtil method getEnclosingNode.
/**
* @return the MappingEntity representing the parent tosca node of given entity
*/
public static MappingEntity getEnclosingNode(Entity current) {
Entity parent = current;
do {
current = parent;
parent = current.getParent().orElseThrow(() -> new IllegalStateException("Entity does not have a parent"));
} while (!ToscaStructure.NODE_TEMPLATES.equals(parent.getId()));
return (MappingEntity) current;
}
Aggregations