use of org.eclipse.winery.repository.common.RepositoryFileReference 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 = owner.getLastUpdate(ref.get());
@NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
@NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
boolean cacheNeedsUpdate = true;
if (owner.exists(cacheRef)) {
Date lastUpdateCache = owner.getLastUpdate(cacheRef);
if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
cacheNeedsUpdate = false;
}
}
List<String> result;
if (cacheNeedsUpdate) {
final Optional<XSModel> model = BackendUtils.getXSModel(ref.get(), owner);
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 = JacksonProvider.mapper.writeValueAsString(result);
} catch (JsonProcessingException e) {
LOGGER.error("Could not generate cache content", e);
}
try {
owner.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 = owner.newInputStream(cacheRef)) {
result = JacksonProvider.mapper.readValue(is, java.util.List.class);
} catch (IOException e) {
LOGGER.error("Could not read from cache", e);
result = Collections.emptyList();
}
}
return result;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class ScriptPlugin method createDeploymentArtifact.
private String createDeploymentArtifact(ArtifactTemplateId artifactTemplate, IRepository repository, GeneratedArtifacts generatedArtifacts, String packageToInstall) {
QName packageDaId = new QName(artifactTemplate.getQName().getNamespaceURI(), VersionSupport.getNewComponentVersionId(artifactTemplate, packageToInstall + "-DA"));
ArtifactTemplateId deployArtId = new ArtifactTemplateId(packageDaId);
String generatedPackage = packageToInstall + ".tar.gz";
if (!repository.exists(deployArtId)) {
String cmd = "/usr/bin/apt download $(/usr/bin/apt-cache depends --recurse --no-recommends --no-suggests --no-conflicts " + "--no-breaks --no-replaces --no-enhances --no-pre-depends " + packageToInstall + " | /bin/grep \"^\\w\")";
logger.info("Executing command: \"{}\"", cmd);
Path tempDirectory = null;
try {
repository.setElement(deployArtId, new TArtifactTemplate.Builder(deployArtId.getXmlId().getDecoded(), ToscaBaseTypes.archiveArtifactType).build());
tempDirectory = Files.createTempDirectory(packageDaId.getLocalPart());
ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(deployArtId);
try {
Utils.execute(tempDirectory.toString(), "bash", "-c", cmd);
} catch (IOException | InterruptedException e) {
logger.info("Cannot perform download, skipping it! You must add the DA contents yourself!");
logger.debug("Root cause:", e);
}
// Ensure the folder structure exists.
RepositoryFileReference tarFileRef = new RepositoryFileReference(filesId, generatedPackage);
if (repository.id2AbsolutePath(filesId).toFile().mkdirs()) {
compressFolderContents(tempDirectory.toString(), repository.ref2AbsolutePath(tarFileRef).toString());
BackendUtils.synchronizeReferences(repository, deployArtId);
} else {
logger.error("Could not create folders: {}", repository.id2AbsolutePath(filesId).toFile().getAbsolutePath());
}
} catch (Exception e) {
logger.error("Error while downloading artifacts...", e);
} finally {
if (tempDirectory != null) {
FileUtils.forceDelete(tempDirectory);
}
}
}
generatedArtifacts.deploymentArtifactsToAdd.add(packageDaId);
return generatedPackage;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class UbuntuVMPlugin method downloadDependenciesBasedOnNodeType.
@Override
public void downloadDependenciesBasedOnNodeType(TNodeTypeImplementation nodeTypeImplementation, IRepository repository) {
QName nodeType = nodeTypeImplementation.getNodeType();
WineryVersion nodeTypeVersion = VersionUtils.getVersion(nodeType.getLocalPart());
String componentVersion = nodeTypeVersion.getComponentVersion();
if (componentVersion != null) {
String codeName = getCodeName(componentVersion);
if (codeName != null) {
logger.info("Found code name '{}' for Ubuntu Node Type {}", codeName, nodeType);
String nameWithoutVersion = VersionUtils.getNameWithoutVersion(nodeType.getLocalPart());
WineryVersion artifactVersion = new WineryVersion(nodeTypeVersion.getComponentVersion() + "-CloudImage", 1, 1);
ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId(OpenToscaBaseTypes.artifactTemplateNamespace, nameWithoutVersion + "-DA" + WineryVersion.WINERY_NAME_FROM_VERSION_SEPARATOR + artifactVersion, false);
TArtifactTemplate element = repository.getElement(artifactTemplateId);
element.setType(OpenToscaBaseTypes.cloudImageArtifactType);
logger.info("Generated ArtifactTemplate {}", artifactTemplateId.getQName());
if (!repository.exists(artifactTemplateId)) {
logger.info("Trying to download image file...");
String baseUrl = "https://cloud-images.ubuntu.com/" + codeName + "/current/" + codeName + "-server-cloudimg-amd64";
try {
URL url = new URL(baseUrl + imageFileType);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
connection.disconnect();
logger.info("Image not found, trying with '-disk' suffix...");
url = new URL(baseUrl + imageDiskType);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
responseCode = connection.getResponseCode();
}
if (responseCode == 200) {
repository.setElement(artifactTemplateId, element);
ArtifactTemplateFilesDirectoryId filesId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
String fileName = url.getFile().substring(url.getFile().lastIndexOf("/") + 1);
RepositoryFileReference repositoryFileReference = new RepositoryFileReference(filesId, fileName);
try (InputStream inputStream = url.openStream()) {
repository.putContentToFile(repositoryFileReference, inputStream, MediaType.parse("application/x-image"));
}
BackendUtils.synchronizeReferences(repository, artifactTemplateId);
TDeploymentArtifact imageDa = new TDeploymentArtifact.Builder("CloudImage", OpenToscaBaseTypes.cloudImageArtifactType).setArtifactRef(artifactTemplateId.getQName()).build();
List<TDeploymentArtifact> deploymentArtifacts = nodeTypeImplementation.getDeploymentArtifacts();
if (deploymentArtifacts == null) {
deploymentArtifacts = new ArrayList<>();
nodeTypeImplementation.setDeploymentArtifacts(deploymentArtifacts);
}
deploymentArtifacts.add(imageDa);
} else {
logger.info("Could not download image -- the URLs do not exist: \n\t{}\n\t{}", baseUrl + imageFileType, baseUrl + imageDiskType);
}
} catch (IOException e) {
logger.info("Error while downloading image file!", e);
}
}
} else {
logger.info("Could not identify code name of given Ubuntu Node Type! {}", nodeType);
}
}
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class RestUtils method cloneServiceTemplate.
public static ServiceTemplateId cloneServiceTemplate(ServiceTemplateId serviceTemplate, String newName, String artifactName) throws JAXBException, IllegalArgumentException, IOException {
ServiceTemplateId newServiceTemplateId = new ServiceTemplateId(serviceTemplate.getNamespace().getDecoded(), newName, false);
RepositoryFileReference fileRef = new RepositoryFileReference(newServiceTemplateId, "ServiceTemplate.tosca");
TDefinitions defs = new ServiceTemplateResource(serviceTemplate).getDefinitions();
defs.setId(newName + "Definitions");
defs.setName(newName + "Definitions generated from Artifact " + artifactName);
TServiceTemplate oldSTModel = null;
for (TExtensibleElements el : defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation()) {
if (el instanceof TServiceTemplate) {
oldSTModel = (TServiceTemplate) el;
}
}
if (oldSTModel != null) {
oldSTModel.setId(newName);
oldSTModel.setName(newName + " generated from Artifact " + artifactName);
// remove xaaspackager tags
Collection<TTag> toRemove = new ArrayList<>();
for (TTag tag : oldSTModel.getTags()) {
switch(tag.getName()) {
case "xaasPackageNode":
case "xaasPackageArtifactType":
case "xaasPackageDeploymentArtifact":
toRemove.add(tag);
break;
default:
break;
}
}
oldSTModel.getTags().removeAll(toRemove);
}
JAXBContext context = JAXBContext.newInstance(TDefinitions.class);
Marshaller m = context.createMarshaller();
StringWriter sw = new StringWriter();
m.marshal(defs, sw);
String xmlString = sw.toString();
RepositoryFactory.getRepository().putContentToFile(fileRef, xmlString, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
return newServiceTemplateId;
}
use of org.eclipse.winery.repository.common.RepositoryFileReference in project winery by eclipse.
the class GenericFileResource method putContentToFile.
public Response putContentToFile(String fileName, InputStream uploadedInputStream, String mediaTypeString) {
RepositoryFileReference ref = this.fileName2fileRef(fileName, false);
// TODO: instead of fixing the media type, we could overwrite the browser's mediatype by using some user configuration
BufferedInputStream bis = new BufferedInputStream(uploadedInputStream);
org.apache.tika.mime.MediaType mediaType = BackendUtils.getFixedMimeType(bis, fileName, org.apache.tika.mime.MediaType.parse(mediaTypeString));
return RestUtils.putContentToFile(ref, bis, mediaType);
}
Aggregations