use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.
the class BackendUtils method getAllReferencedArtifactTemplates.
@NonNull
private static Collection<QName> getAllReferencedArtifactTemplates(TDeploymentArtifacts tDeploymentArtifacts) {
if (tDeploymentArtifacts == null) {
return Collections.emptyList();
}
List<TDeploymentArtifact> deploymentArtifacts = tDeploymentArtifacts.getDeploymentArtifact();
if (deploymentArtifacts == null) {
return Collections.emptyList();
}
Collection<QName> res = new ArrayList<>();
for (TDeploymentArtifact da : deploymentArtifacts) {
QName artifactRef = da.getArtifactRef();
if (artifactRef != null) {
res.add(artifactRef);
}
}
return res;
}
use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.
the class TOSCATransformer method createTOSCAGraph.
public static TOSCAGraph createTOSCAGraph(TTopologyTemplate topologyTemplate) {
TOSCAGraph graph = new TOSCAGraph(new TOSCAEdgeFactory());
@NonNull List<TRelationshipTemplate> relationshipTemplates = topologyTemplate.getRelationshipTemplates();
@NonNull List<TNodeTemplate> nodeTemplates = topologyTemplate.getNodeTemplates();
Map<TNodeTemplate, TOSCANode> nodes = new HashMap<>();
for (TNodeTemplate nodeTemplate : nodeTemplates) {
TOSCANode node = createAndInitializeTOSCANode(nodeTemplate);
nodes.put(nodeTemplate, node);
graph.addVertex(node);
}
for (TRelationshipTemplate tRelationshipTemplate : relationshipTemplates) {
TOSCAEdge edge = graph.addEdge(nodes.get(tRelationshipTemplate.getSourceElement().getRef()), nodes.get(tRelationshipTemplate.getTargetElement().getRef()));
initializeTOSCAEdge(tRelationshipTemplate, edge);
}
return graph;
}
use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.
the class X2YConverter method convert.
/**
* Converts TOSCA XML Definitions to TOSCA YAML ServiceTemplates
*/
@NonNull
public Map<Path, TServiceTemplate> convert(Definitions node, Path outPath, QName name) throws MultiException {
if (Objects.isNull(node))
return new LinkedHashMap<>();
QName tmpName = name;
LOGGER.debug("Convert TServiceTemplate: {}", node.getIdFromIdOrNameField());
TServiceTemplate.Builder builder = new TServiceTemplate.Builder(Defaults.TOSCA_DEFINITIONS_VERSION).setDescription(convertDocumentation(node.getDocumentation())).setArtifactTypes(convert(node.getArtifactTypes())).setCapabilityTypes(convert(node.getCapabilityTypes())).setRelationshipTypes(convert(node.getRelationshipTypes())).setNodeTypes(convert(node.getNodeTypes())).setPolicyTypes(convert(node.getPolicyTypes()));
if (node.getServiceTemplates().size() == 1) {
builder.setTopologyTemplate(convert(node.getServiceTemplates().get(0)));
if (Objects.nonNull(node.getServiceTemplates().get(0).getName())) {
tmpName = new QName(node.getServiceTemplates().get(0).getName());
}
builder.addMetadata("targetNamespace", node.getTargetNamespace());
}
MultiException exception = importDefinitions.entrySet().stream().map((entry) -> {
Path path = this.path.resolve(entry.getKey().getGroup()).resolve(entry.getKey().getNamespace().getEncoded());
try {
Map<Path, TServiceTemplate> map = new X2YConverter(repository, this.path).convert(entry.getValue(), path, entry.getKey().getQName());
Optional.ofNullable(map).orElse(new LinkedHashMap<>()).forEach((Path key, TServiceTemplate value) -> builder.addImports(key.toFile().getName(), new TImportDefinition.Builder(this.path.relativize(key).toString()).setNamespaceUri(entry.getValue().getTargetNamespace()).setNamespacePrefix(getNamespacePrefix(entry.getValue().getTargetNamespace())).build()));
} catch (MultiException e) {
return e;
}
return null;
}).filter(Objects::nonNull).reduce(new MultiException(), (a, b) -> a.add(b.getException()));
if (exception.hasException()) {
throw exception;
}
TServiceTemplate serviceTemplate = builder.build();
Path filePath = outPath.resolve(tmpName.getLocalPart().concat(".yml"));
try {
Files.createDirectories(filePath.getParent());
Writer writer = new Writer();
writer.write(serviceTemplate, filePath);
return Collections.singletonMap(filePath, serviceTemplate);
} catch (IOException e) {
LOGGER.error("Could not create file path {}", filePath.getParent());
}
return new LinkedHashMap<>();
}
use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.
the class Y2XConverter method convert.
/**
* Converts TOSCA YAML ServiceTemplates to TOSCA XML Definitions
*
* @return TOSCA XML Definitions
*/
@NonNull
public Definitions convert(org.eclipse.winery.model.tosca.yaml.TServiceTemplate node, String id, String target_namespace, Path path, Path outPath) {
if (node == null)
return new Definitions();
LOGGER.debug("Converting TServiceTemplate");
// Reset
this.reset();
this.referenceVisitor = new ReferenceVisitor(node, target_namespace, path);
this.namespace = target_namespace;
this.path = path;
this.outPath = outPath;
init(node);
Definitions definitions = new Definitions.Builder(id + "_Definitions", target_namespace).setImport(convert(node.getImports())).addTypes(convert(node.getDataTypes())).addTypes(convert(node.getGroupTypes())).addServiceTemplates(convertServiceTemplate(node, id, target_namespace)).addNodeTypes(convert(node.getNodeTypes())).addNodeTypeImplementations(this.nodeTypeImplementations).addRelationshipTypes(convert(node.getRelationshipTypes())).addCapabilityTypes(convert(node.getCapabilityTypes())).addArtifactTypes(convert(node.getArtifactTypes())).addArtifactTemplates(this.artifactTemplates.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList())).addPolicyTypes(convert(node.getPolicyTypes())).setName(id).addImports(this.imports).addRequirementTypes(this.requirementTypes).addPolicyTemplates(this.policyTemplates).build();
WriterUtils.storeDefinitions(definitions, true, path);
return definitions;
}
use of org.eclipse.jdt.annotation.NonNull in project winery by eclipse.
the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.
// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
Objects.requireNonNull(id);
Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
if (!ref.isPresent()) {
return Collections.emptyList();
}
short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
Date lastUpdate = RepositoryFactory.getRepository().getLastUpdate(ref.get());
@NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
@NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
boolean cacheNeedsUpdate = true;
if (RepositoryFactory.getRepository().exists(cacheRef)) {
Date lastUpdateCache = RepositoryFactory.getRepository().getLastUpdate(cacheRef);
if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
cacheNeedsUpdate = false;
}
}
List<String> result;
if (cacheNeedsUpdate) {
final Optional<XSModel> model = BackendUtils.getXSModel(ref.get());
if (!model.isPresent()) {
return Collections.emptyList();
}
XSNamedMap components = model.get().getComponents(type);
// @SuppressWarnings("unchecked")
int len = components.getLength();
result = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
XSObject item = components.item(i);
// We want to return only types defined in the namespace of this resource
if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
result.add(item.getName());
}
}
String cacheContent = null;
try {
cacheContent = BackendUtils.mapper.writeValueAsString(result);
} catch (JsonProcessingException e) {
LOGGER.error("Could not generate cache content", e);
}
try {
RepositoryFactory.getRepository().putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
} catch (IOException e) {
LOGGER.error("Could not update cache", e);
}
} else {
// cache should contain most recent information
try (InputStream is = RepositoryFactory.getRepository().newInputStream(cacheRef)) {
result = BackendUtils.mapper.readValue(is, java.util.List.class);
} catch (IOException e) {
LOGGER.error("Could not read from cache", e);
result = Collections.emptyList();
}
}
return result;
}
Aggregations