use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class BackendUtils method createWrapperDefinitionsAndInitialEmptyElement.
public static TDefinitions createWrapperDefinitionsAndInitialEmptyElement(IRepository repository, DefinitionsChildId id) {
final TDefinitions definitions = createWrapperDefinitions(id, repository);
HasIdInIdOrNameField element;
if (id instanceof RelationshipTypeImplementationId) {
element = new TRelationshipTypeImplementation();
} else if (id instanceof NodeTypeImplementationId) {
element = new TNodeTypeImplementation();
} else if (id instanceof RequirementTypeId) {
element = new TRequirementType();
} else if (id instanceof NodeTypeId) {
element = new TNodeType();
} else if (id instanceof RelationshipTypeId) {
element = new TRelationshipType();
} else if (id instanceof CapabilityTypeId) {
element = new TCapabilityType();
} else if (id instanceof DataTypeId) {
element = new TDataType();
} else if (id instanceof ArtifactTypeId) {
element = new TArtifactType();
} else if (id instanceof PolicyTypeId) {
element = new TPolicyType();
} else if (id instanceof PolicyTemplateId) {
element = new TPolicyTemplate();
} else if (id instanceof ServiceTemplateId) {
element = new TServiceTemplate();
} else if (id instanceof ArtifactTemplateId) {
element = new TArtifactTemplate();
} else if (id instanceof ComplianceRuleId) {
element = new OTComplianceRule(new OTComplianceRule.Builder(id.getXmlId().getDecoded()));
} else if (id instanceof PatternRefinementModelId) {
element = new OTPatternRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TopologyFragmentRefinementModelId) {
element = new OTTopologyFragmentRefinementModel(new OTPatternRefinementModel.Builder());
} else if (id instanceof TestRefinementModelId) {
element = new OTTestRefinementModel(new OTTestRefinementModel.Builder());
} else if (id instanceof InterfaceTypeId) {
element = new TInterfaceType();
} else if (id instanceof XSDImportId) {
// TImport has no id; thus directly generating it without setting an id
TImport tImport = new TImport();
definitions.setElement(tImport);
return definitions;
} else {
throw new IllegalStateException("Unhandled id branch. Could happen for XSDImportId");
}
copyIdToFields(element, id);
definitions.setElement((TExtensibleElements) element);
return definitions;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ComplianceRuleId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
OTComplianceRule complianceRule = this.getElement(id);
// TODO extend to required Structure
if (complianceRule.getIdentifier() != null) {
for (TEntityTemplate entityTemplate : complianceRule.getIdentifier().getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through deployment artifacts
List<TDeploymentArtifact> deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
getReferencedRequirementTypeIds(ids, n);
getCapabilitiesReferences(ids, n);
// crawl through policies
List<TPolicy> policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class IRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(NodeTypeImplementationId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// There may be multiple DAs/IAs referencing the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
final TNodeTypeImplementation element = this.getElement(id);
// DAs
List<TDeploymentArtifact> deploymentArtifacts = element.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
for (TDeploymentArtifact da : deploymentArtifacts) {
QName qname;
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
ids.add(new ArtifactTypeId(da.getArtifactType()));
}
}
// IAs
return this.getReferencedTOSCAComponentImplementationArtifactIds(ids, element.getImplementationArtifacts(), id);
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class ScriptPlugin method downloadDependenciesBasedOnArtifact.
@Override
public GeneratedArtifacts downloadDependenciesBasedOnArtifact(QName artifactTemplate, IRepository repository) {
ArtifactTemplateId originalId = new ArtifactTemplateId(artifactTemplate);
QName selfContainedVersion = VersionSupport.getSelfContainedVersion(originalId);
ArtifactTemplateId selfContainedId = new ArtifactTemplateId(selfContainedVersion);
if (!repository.exists(selfContainedId)) {
try {
repository.duplicate(originalId, selfContainedId);
} catch (IOException e) {
logger.error("Could not create self-containd artifact template {}", selfContainedId, e);
}
}
ArtifactTemplateFilesDirectoryId originalFilesId = new ArtifactTemplateFilesDirectoryId(selfContainedId);
GeneratedArtifacts generatedArtifacts = new GeneratedArtifacts(artifactTemplate);
generatedArtifacts.selfContainedArtifactQName = selfContainedVersion;
boolean createdSelfContainedVersion = false;
for (RepositoryFileReference containedFile : repository.getContainedFiles(originalFilesId)) {
if (containedFile.getFileName().endsWith(".sh")) {
StringBuilder newScriptContents = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(repository.ref2AbsolutePath(containedFile).toFile()))) {
String line;
ArrayList<String> packageNames = new ArrayList<>();
int packageNameCount = 0;
while ((line = reader.readLine()) != null) {
List<String> strings = Arrays.asList(line.replaceAll("[;&]", "").split("\\s+"));
Iterator<String> words = strings.iterator();
if (words.hasNext() && StringUtils.isNotBlank(line) && line.contains("apt")) {
String word = words.next();
while ("sudo".equals(word) || word.startsWith("-")) {
word = words.next();
}
if (words.hasNext() && ("apt-get".equals(word) || "apt".equals(word))) {
word = words.next();
while (word.startsWith("-")) {
word = words.next();
}
if (word.equals("install") && words.hasNext()) {
words.forEachRemaining(packageToInstall -> {
if (!packageToInstall.startsWith("-")) {
packageNames.add(createDeploymentArtifact(originalId, repository, generatedArtifacts, packageToInstall));
}
});
}
}
}
if (!packageNames.isEmpty() && packageNameCount++ < packageNames.size()) {
createdSelfContainedVersion = true;
packageNames.forEach(packet -> newScriptContents.append(this.updateScriptFile(packet)));
} else {
newScriptContents.append(line).append("\n");
}
}
} catch (IOException e) {
logger.error("Error while reading script file {}", repository.ref2AbsolutePath(containedFile), e);
}
if (newScriptContents.length() > 0) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(repository.ref2AbsolutePath(containedFile).toFile()))) {
writer.write(newScriptContents.toString());
writer.flush();
} catch (IOException e) {
logger.error("Error while writing to script file {}", repository.ref2AbsolutePath(containedFile), e);
}
}
}
}
if (createdSelfContainedVersion) {
return generatedArtifacts;
}
try {
repository.forceDelete(selfContainedId);
} catch (IOException e) {
logger.error("Could not delete not required self-contained {}!", selfContainedId, e);
}
return null;
}
use of org.eclipse.winery.model.ids.definitions.ArtifactTemplateId in project winery by eclipse.
the class YamlRepository method exists.
/**
* Checks if YAML Definition exists Artifact Templates are searched in Type
*
* @param id generic id of target
* @return boolean if target exists
*/
@Override
public boolean exists(GenericId id) {
Path targetPath = id2AbsolutePath(id);
if (id instanceof ArtifactTemplateId) {
GenericId convertedId = convertGenericId(id);
if (convertedId != null) {
String convertedFilename = BackendUtils.getFileNameOfDefinitions((DefinitionsChildId) convertedId);
targetPath = targetPath.resolve(convertedFilename);
return artifactTemplateExistsInType(targetPath, ((ArtifactTemplateId) id).getQName());
}
}
return Files.exists(targetPath);
}
Aggregations