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.
17 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
19 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.SessionContext;
26 import com.amdocs.zusammen.datatypes.item.Action;
27 import com.amdocs.zusammen.datatypes.item.ElementContext;
28 import com.amdocs.zusammen.utils.fileutils.FileUtils;
29 import java.io.ByteArrayInputStream;
30 import java.nio.ByteBuffer;
31 import java.util.Optional;
32 import org.openecomp.core.utilities.json.JsonUtil;
33 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
34 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
35 import org.openecomp.sdc.datatypes.model.ElementType;
36 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
42 import org.openecomp.sdc.versioning.dao.types.Version;
44 public class OrchestrationTemplateCandidateDaoZusammenImpl
45 implements OrchestrationTemplateCandidateDao {
47 private static final Logger logger =
48 LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
50 private final ZusammenAdaptor zusammenAdaptor;
52 private static final String EMPTY_DATA = "{}";
54 public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
55 this.zusammenAdaptor = zusammenAdaptor;
59 public void registerVersioning(String versionableEntityType) {
60 // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
64 public Optional<OrchestrationTemplateCandidateData> get(String vspId, Version version) {
65 logger.info("Getting orchestration template for vsp id {}", vspId);
67 SessionContext context = createSessionContext();
68 ElementContext elementContext = new ElementContext(vspId, version.getId());
70 Optional<Element> candidateElement =
71 zusammenAdaptor.getElementByName(context, elementContext, null,
72 ElementType.OrchestrationTemplateCandidate.name());
74 if (!candidateElement.isPresent() ||
75 VspZusammenUtil.hasEmptyData(candidateElement.get().getData()) ||
76 candidateElement.get().getSubElements().isEmpty()) {
77 logger.info("Orchestration template for vsp id {} does not exist / has empty data", vspId);
78 return Optional.empty();
81 OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
82 candidate.setFilesDataStructure(
83 new String(FileUtils.toByteArray(candidateElement.get().getData())));
85 candidateElement.get().getSubElements().stream()
86 .map(element -> zusammenAdaptor
87 .getElement(context, elementContext, element.getElementId().toString()))
88 .forEach(element -> element.ifPresent(
89 candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, true)));
91 logger.info("Finished getting orchestration template for vsp id {}", vspId);
92 return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
96 public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
97 logger.info("Getting orchestration template info for vsp id {}", vspId);
99 SessionContext context = createSessionContext();
100 ElementContext elementContext = new ElementContext(vspId, version.getId());
102 Optional<ElementInfo> candidateElement =
103 zusammenAdaptor.getElementInfoByName(context, elementContext, null,
104 ElementType.OrchestrationTemplateCandidate.name());
106 if (!candidateElement.isPresent() || candidateElement.get().getSubElements().isEmpty()) {
107 logger.info("Orchestration template info for vsp id {} does not exist", vspId);
108 return Optional.empty();
111 OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
112 candidateElement.get().getSubElements().stream()
113 .map(elementInfo -> zusammenAdaptor
114 .getElement(context, elementContext, elementInfo.getId().toString()))
115 .forEach(element -> element.ifPresent(
116 candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, false)));
117 logger.info("Finished getting orchestration template info for vsp id {}", vspId);
118 return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
121 private void populateCandidate(final OrchestrationTemplateCandidateData candidate,
122 final Element candidateInfoElement,
123 final boolean fullData) {
124 final String elementName = candidateInfoElement.getInfo().getName();
125 if (ElementType.OrchestrationTemplateCandidateContent.name().equals(elementName)) {
127 candidate.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
129 candidate.setFileSuffix(candidateInfoElement.getInfo()
130 .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
131 candidate.setFileName(candidateInfoElement.getInfo()
132 .getProperty(InfoPropertyName.FILE_NAME.getVal()));
133 } else if (ElementType.OrchestrationTemplateCandidateValidationData.name().equals(elementName)) {
134 candidate.setValidationData(new String(FileUtils.toByteArray(candidateInfoElement.getData())));
135 } else if (ElementType.ORIGINAL_ONBOARDED_PACKAGE.name().equals(elementName)) {
136 candidate.setOriginalFileName(candidateInfoElement.getInfo()
137 .getProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal()));
138 candidate.setOriginalFileSuffix(candidateInfoElement.getInfo()
139 .getProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal()));
141 candidate.setOriginalFileContentData(
142 ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData()))
149 public void delete(String vspId, Version version) {
150 ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
152 ZusammenElement candidateContentElement =
153 buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
154 candidateContentElement.setData(emptyData);
156 ZusammenElement validationData = buildStructuralElement(ElementType
157 .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
158 validationData.setData(emptyData);
160 ZusammenElement candidateElement =
161 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
162 candidateElement.setData(emptyData);
163 candidateElement.addSubElement(candidateContentElement);
164 candidateElement.addSubElement(validationData);
166 SessionContext context = createSessionContext();
167 ElementContext elementContext = new ElementContext(vspId, version.getId());
168 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
169 "Delete Orchestration Template Candidate Elements's content");
173 public void update(final String vspId, final Version version,
174 final OrchestrationTemplateCandidateData candidateData) {
175 logger.info("Uploading candidate data entity for vsp id {}", vspId);
176 final ZusammenElement candidateElement =
177 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
179 .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
181 final ZusammenElement candidateContentElement =
182 buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
183 candidateContentElement
184 .setData(new ByteArrayInputStream(candidateData.getContentData().array()));
185 candidateContentElement.getInfo()
186 .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
187 candidateContentElement.getInfo()
188 .addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());
190 if (OnboardingTypesEnum.CSAR.toString().equalsIgnoreCase(candidateData.getFileSuffix())) {
191 final ZusammenElement originalPackageElement =
192 buildStructuralElement(ElementType.ORIGINAL_ONBOARDED_PACKAGE, Action.UPDATE);
193 originalPackageElement.getInfo()
194 .addProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal(), candidateData.getOriginalFileName());
195 originalPackageElement.getInfo()
196 .addProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal(), candidateData.getOriginalFileSuffix());
197 originalPackageElement.setData(new ByteArrayInputStream(candidateData.getOriginalFileContentData().array()));
198 candidateElement.addSubElement(originalPackageElement);
200 final ZusammenElement validationData = buildStructuralElement(ElementType
201 .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
202 if (candidateData.getValidationData() != null) {
204 .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
206 candidateElement.addSubElement(validationData);
207 candidateElement.addSubElement(candidateContentElement);
208 SessionContext context = createSessionContext();
209 ElementContext elementContext = new ElementContext(vspId, version.getId());
210 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
211 "Update Orchestration Template Candidate");
212 logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
216 public void updateValidationData(String vspId, Version version,
217 ValidationStructureList validationData) {
219 .info("Updating validation data of orchestration template candidate for VSP id {} ", vspId);
221 ZusammenElement validationDataElement = buildStructuralElement(ElementType
222 .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
223 validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA
224 .getBytes()) : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));
226 ZusammenElement candidateElement =
227 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
228 candidateElement.addSubElement(validationDataElement);
230 SessionContext context = createSessionContext();
231 ElementContext elementContext = new ElementContext(vspId, version.getId());
232 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
233 "Update Orchestration Template Candidate validation data");
235 .info("Finished updating validation data of orchestration template candidate for VSP id {}",
240 public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
241 logger.info("Updating orchestration template for VSP id {}", vspId);
243 ZusammenElement candidateElement =
244 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
246 .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
248 SessionContext context = createSessionContext();
249 ElementContext elementContext = new ElementContext(vspId, version.getId());
250 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
251 "Update Orchestration Template Candidate structure");
252 logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
257 public Optional<String> getStructure(String vspId, Version version) {
258 logger.info("Getting orchestration template candidate structure for vsp id {}", vspId);
260 SessionContext context = createSessionContext();
261 ElementContext elementContext = new ElementContext(vspId, version.getId());
263 Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
264 ElementType.OrchestrationTemplateCandidate.name());
266 if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
267 return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
270 logger.info("Finished getting orchestration template candidate structure for vsp id {}", vspId);
272 return Optional.empty();
275 public enum InfoPropertyName {
276 FILE_SUFFIX("fileSuffix"),
277 FILE_NAME("fileName"),
278 ORIGINAL_FILE_NAME("originalFilename"),
279 ORIGINAL_FILE_SUFFIX("originalFileSuffix");
281 private final String val;
283 InfoPropertyName(String val) {
287 private String getVal() {