+ public Service updateServiceFromToscaModel(final String serviceId, final User modifier, final @NotNull InputStream fileToUpload) {
+ final Either<Service, ResponseFormat> serviceResponseFormatEither = serviceBusinessLogic.getService(serviceId, modifier);
+ if (serviceResponseFormatEither.isRight()) {
+ throw new ByActionStatusComponentException(ActionStatus.SERVICE_NOT_FOUND, serviceId);
+ }
+ final Service serviceOriginal = serviceResponseFormatEither.left().value();
+ Map<String, byte[]> csar = null;
+ try {
+ csar = ZipUtils.readZip(fileToUpload.readAllBytes(), false);
+ } catch (final ZipException | IOException e) {
+ log.info("Failed to unzip received csar {}", serviceId, e);
+ }
+ if (csar == null) {
+ throw new ByActionStatusComponentException(ActionStatus.CSAR_NOT_FOUND);
+ }
+ final byte[] mainYamlBytes = readMainYamlFile(csar);
+ final Map<String, String> metadata = (Map<String, String>) new Yaml().loadAs(new String(mainYamlBytes), Map.class).get("metadata");
+ validateServiceMetadataBeforeCreate(serviceOriginal, metadata);
+ final Service newService = cloneServiceIdentifications(serviceOriginal);
+ updateServiceMetadata(newService, metadata);
+ return createService(newService, AuditingActionEnum.UPDATE_SERVICE_TOSCA_MODEL, modifier, csar, null);
+ }
+
+ private byte[] readMainYamlFile(final Map<String, byte[]> csar) {
+ final Pattern pattern = Pattern.compile(TOSCA_METADATA_PATH_PATTERN);
+ final Optional<String> keyOp = csar.keySet().stream().filter(k -> pattern.matcher(k).matches()).findAny();
+ if (keyOp.isEmpty()) {
+ throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, TOSCA_METADATA_PATH_PATTERN, "");
+ }
+ final Properties props = new Properties();
+ try {
+ final String propStr = new String(csar.get(keyOp.get()));
+ props.load(new StringReader(propStr.replace("\\", "\\\\")));
+ } catch (IOException e) {
+ throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, TOSCA_META_ENTRY_DEFINITIONS, "");
+ }
+ final String mainYamlFileName = props.getProperty(TOSCA_META_ENTRY_DEFINITIONS);
+ final byte[] mainYamlBytes = csar.get(mainYamlFileName);
+ if (mainYamlBytes == null) {
+ throw new ByActionStatusComponentException(ActionStatus.ARTIFACT_NOT_FOUND_IN_CSAR, mainYamlFileName, "");
+ }
+ return mainYamlBytes;
+ }
+