2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * ============LICENSE_END=========================================================
16 * Modifications copyright (c) 2021 Nordix Foundation
17 * ================================================================================
19 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
21 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
22 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
26 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.utils.fileutils.FileUtils;
31 import java.io.ByteArrayInputStream;
32 import java.nio.ByteBuffer;
33 import java.nio.charset.StandardCharsets;
34 import java.util.Optional;
35 import lombok.AllArgsConstructor;
37 import org.openecomp.core.utilities.json.JsonUtil;
38 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
39 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
40 import org.openecomp.sdc.be.csar.storage.ArtifactInfo;
41 import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager;
42 import org.openecomp.sdc.be.csar.storage.StorageFactory;
43 import org.openecomp.sdc.datatypes.model.ElementType;
44 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
45 import org.openecomp.sdc.logging.api.Logger;
46 import org.openecomp.sdc.logging.api.LoggerFactory;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
50 import org.openecomp.sdc.versioning.dao.types.Version;
52 public class OrchestrationTemplateCandidateDaoZusammenImpl implements OrchestrationTemplateCandidateDao {
54 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
55 private static final String EMPTY_DATA = "{}";
56 private final ZusammenAdaptor zusammenAdaptor;
57 private final ArtifactStorageManager artifactStorageManager;
59 public OrchestrationTemplateCandidateDaoZusammenImpl(final ZusammenAdaptor zusammenAdaptor) {
60 this.zusammenAdaptor = zusammenAdaptor;
61 LOGGER.info("Instantiating artifactStorageManager");
62 this.artifactStorageManager = new StorageFactory().createArtifactStorageManager();
66 public void registerVersioning(String versionableEntityType) {
67 // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
71 public Optional<OrchestrationTemplateCandidateData> get(String vspId, Version version) {
72 LOGGER.info("Getting orchestration template for vsp id {}", vspId);
73 SessionContext context = createSessionContext();
74 ElementContext elementContext = new ElementContext(vspId, version.getId());
75 Optional<Element> candidateElement = zusammenAdaptor
76 .getElementByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
77 if (!candidateElement.isPresent() || VspZusammenUtil.hasEmptyData(candidateElement.get().getData()) || candidateElement.get().getSubElements()
79 LOGGER.info("Orchestration template for vsp id {} does not exist / has empty data", vspId);
80 return Optional.empty();
82 OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
83 candidate.setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.get().getData())));
84 candidateElement.get().getSubElements().stream()
85 .map(element -> zusammenAdaptor.getElement(context, elementContext, element.getElementId().toString()))
86 .forEach(element -> element.ifPresent(candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, true)));
87 LOGGER.info("Finished getting orchestration template for vsp id {}", vspId);
88 return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
92 public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
93 LOGGER.info("Getting orchestration template info for vsp id {}", vspId);
94 SessionContext context = createSessionContext();
95 ElementContext elementContext = new ElementContext(vspId, version.getId());
96 Optional<ElementInfo> candidateElement = zusammenAdaptor
97 .getElementInfoByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
98 if (!candidateElement.isPresent() || candidateElement.get().getSubElements().isEmpty()) {
99 LOGGER.info("Orchestration template info for vsp id {} does not exist", vspId);
100 return Optional.empty();
102 OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
103 candidateElement.get().getSubElements().stream()
104 .map(elementInfo -> zusammenAdaptor.getElement(context, elementContext, elementInfo.getId().toString()))
105 .forEach(element -> element.ifPresent(candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, false)));
106 LOGGER.info("Finished getting orchestration template info for vsp id {}", vspId);
107 return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
110 private void populateCandidate(final OrchestrationTemplateCandidateData candidate, final Element candidateInfoElement, final boolean fullData) {
111 final String elementName = candidateInfoElement.getInfo().getName();
112 if (ElementType.OrchestrationTemplateCandidateContent.name().equals(elementName)) {
114 candidate.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
116 candidate.setFileSuffix(candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
117 candidate.setFileName(candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
118 } else if (ElementType.OrchestrationTemplateCandidateValidationData.name().equals(elementName)) {
119 candidate.setValidationData(new String(FileUtils.toByteArray(candidateInfoElement.getData())));
120 } else if (ElementType.ORIGINAL_ONBOARDED_PACKAGE.name().equals(elementName)) {
121 candidate.setOriginalFileName(candidateInfoElement.getInfo().getProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal()));
122 candidate.setOriginalFileSuffix(candidateInfoElement.getInfo().getProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal()));
124 candidate.setOriginalFileContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
130 public void delete(String vspId, Version version) {
131 ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
132 ZusammenElement candidateContentElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
133 candidateContentElement.setData(emptyData);
134 ZusammenElement validationData = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
135 validationData.setData(emptyData);
136 ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
137 candidateElement.setData(emptyData);
138 candidateElement.addSubElement(candidateContentElement);
139 candidateElement.addSubElement(validationData);
140 SessionContext context = createSessionContext();
141 ElementContext elementContext = new ElementContext(vspId, version.getId());
142 zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Delete Orchestration Template Candidate Elements's content");
146 public void update(final String vspId, final Version version, final OrchestrationTemplateCandidateData candidateData) {
147 LOGGER.info("Uploading candidate data entity for vsp id {}", vspId);
148 final ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
149 candidateElement.setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
150 final ZusammenElement candidateContentElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
151 candidateContentElement.setData(new ByteArrayInputStream(candidateData.getContentData().array()));
152 candidateContentElement.getInfo().addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
153 candidateContentElement.getInfo().addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());
154 final String versionId = version.getId();
155 if (OnboardingTypesEnum.CSAR.toString().equalsIgnoreCase(candidateData.getFileSuffix())) {
156 final ZusammenElement originalPackageElement = buildStructuralElement(ElementType.ORIGINAL_ONBOARDED_PACKAGE, Action.UPDATE);
157 final String originalFileName = candidateData.getOriginalFileName();
158 final String originalFileSuffix = candidateData.getOriginalFileSuffix();
159 originalPackageElement.getInfo().addProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal(), originalFileName);
160 originalPackageElement.getInfo().addProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal(), originalFileSuffix);
161 originalPackageElement.getInfo().addProperty("storeCsarsExternally", artifactStorageManager.isEnabled());
162 if (artifactStorageManager.isEnabled()) {
163 final ArtifactInfo candidateArtifactInfo = candidateData.getArtifactInfo();
164 if (candidateArtifactInfo == null) {
165 throw new OrchestrationTemplateCandidateDaoZusammenException("No artifact info provided");
167 final ArtifactInfo artifactInfo = artifactStorageManager.persist(vspId, versionId, candidateArtifactInfo);
168 originalPackageElement.setData(new ByteArrayInputStream(artifactInfo.getInfo().getBytes(StandardCharsets.UTF_8)));
170 originalPackageElement.setData(new ByteArrayInputStream(candidateData.getOriginalFileContentData().array()));
172 candidateElement.addSubElement(originalPackageElement);
174 final ZusammenElement validationData = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
175 if (candidateData.getValidationData() != null) {
176 validationData.setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
178 candidateElement.addSubElement(validationData);
179 candidateElement.addSubElement(candidateContentElement);
180 final var context = createSessionContext();
181 final var elementContext = new ElementContext(vspId, versionId);
182 zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate");
183 LOGGER.info("Finished uploading candidate data entity for vsp id {}", vspId);
187 public void updateValidationData(String vspId, Version version, ValidationStructureList validationData) {
188 LOGGER.info("Updating validation data of orchestration template candidate for VSP id {} ", vspId);
189 ZusammenElement validationDataElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
190 validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA.getBytes())
191 : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));
192 ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
193 candidateElement.addSubElement(validationDataElement);
194 SessionContext context = createSessionContext();
195 ElementContext elementContext = new ElementContext(vspId, version.getId());
196 zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate validation data");
197 LOGGER.info("Finished updating validation data of orchestration template candidate for VSP id {}", vspId);
201 public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
202 LOGGER.info("Updating orchestration template for VSP id {}", vspId);
203 ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
204 candidateElement.setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
205 SessionContext context = createSessionContext();
206 ElementContext elementContext = new ElementContext(vspId, version.getId());
207 zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate structure");
208 LOGGER.info("Finished uploading candidate data entity for vsp id {}", vspId);
212 public Optional<String> getStructure(String vspId, Version version) {
213 LOGGER.info("Getting orchestration template candidate structure for vsp id {}", vspId);
214 SessionContext context = createSessionContext();
215 ElementContext elementContext = new ElementContext(vspId, version.getId());
216 Optional<Element> element = zusammenAdaptor
217 .getElementByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
218 if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
219 return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
221 LOGGER.info("Finished getting orchestration template candidate structure for vsp id {}", vspId);
222 return Optional.empty();
227 private enum InfoPropertyName {
228 FILE_SUFFIX("fileSuffix"), FILE_NAME("fileName"), ORIGINAL_FILE_NAME("originalFilename"), ORIGINAL_FILE_SUFFIX("originalFileSuffix");
229 private final String val;