1 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.SessionContext;
7 import com.amdocs.zusammen.datatypes.item.Action;
8 import com.amdocs.zusammen.datatypes.item.ElementContext;
9 import com.amdocs.zusammen.utils.fileutils.FileUtils;
10 import org.apache.commons.io.IOUtils;
11 import org.openecomp.core.utilities.json.JsonUtil;
12 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
13 import org.openecomp.sdc.datatypes.model.ElementType;
14 import org.openecomp.sdc.logging.api.Logger;
15 import org.openecomp.sdc.logging.api.LoggerFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
19 import org.openecomp.sdc.versioning.dao.types.Version;
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.ByteBuffer;
25 import java.util.Arrays;
26 import java.util.Optional;
28 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
29 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
31 public class OrchestrationTemplateCandidateDaoZusammenImpl
32 implements OrchestrationTemplateCandidateDao {
34 private static final Logger logger =
35 LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
37 private ZusammenAdaptor zusammenAdaptor;
39 public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
40 this.zusammenAdaptor = zusammenAdaptor;
44 public void registerVersioning(String versionableEntityType) {
49 public OrchestrationTemplateCandidateData get(String vspId, Version version) {
50 logger.info("Getting orchestration template for VendorSoftwareProduct id -> " + vspId);
52 SessionContext context = createSessionContext();
53 ElementContext elementContext = new ElementContext(vspId, version.getId());
55 Optional<Element> candidateElement =
56 zusammenAdaptor.getElementByName(context, elementContext, null,
57 ElementType.OrchestrationTemplateCandidate.name());
58 if (candidateElement.isPresent()) {
59 if (hasEmptyData(candidateElement.get().getData())) {
62 OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
63 candidateData.setFilesDataStructure(
64 new String(FileUtils.toByteArray(candidateElement.get().getData())));
66 Optional<Element> candidateContentElement = zusammenAdaptor
67 .getElementByName(context, elementContext, candidateElement.get().getElementId(),
68 ElementType.OrchestrationTemplateCandidateContent.name());
70 if (candidateContentElement.isPresent()) {
71 candidateData.setContentData(
72 ByteBuffer.wrap(FileUtils.toByteArray(candidateContentElement.get().getData())));
73 candidateData.setFileSuffix(candidateContentElement.get().getInfo()
74 .getProperty(InfoPropertyName.fileSuffix.name()));
75 candidateData.setFileName(candidateContentElement.get().getInfo()
76 .getProperty(InfoPropertyName.fileName.name()));
79 .info("Finished getting orchestration template for VendorSoftwareProduct id -> " + vspId);
83 .format("Orchestration template for VendorSoftwareProduct id %s does not exist", vspId));
88 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
89 logger.info("Getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
91 SessionContext context = createSessionContext();
92 ElementContext elementContext = new ElementContext(vspId, version.getId());
94 Optional<ElementInfo> candidateElement =
95 zusammenAdaptor.getElementInfoByName(context, elementContext, null,
96 ElementType.OrchestrationTemplateCandidate.name());
97 if (candidateElement.isPresent()) {
98 OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
100 Optional<Element> candidateContentElement = zusammenAdaptor
101 .getElementByName(context, elementContext, candidateElement.get().getId(),
102 ElementType.OrchestrationTemplateCandidateContent.name());
104 if (candidateContentElement.isPresent()) {
105 candidateData.setFileSuffix(candidateContentElement.get().getInfo()
106 .getProperty(InfoPropertyName.fileSuffix.name()));
107 candidateData.setFileName(candidateContentElement.get().getInfo()
108 .getProperty(InfoPropertyName.fileName.name()));
111 "Finished getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
112 return candidateData;
115 .format("Orchestration template info for VendorSoftwareProduct id %s does not exist",
121 public void update(String vspId, Version version,
122 OrchestrationTemplateCandidateData candidateData) {
123 logger.info("Uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
125 ZusammenElement candidateElement =
126 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
128 .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
130 ZusammenElement candidateContentElement =
131 buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
132 candidateContentElement
133 .setData(new ByteArrayInputStream(candidateData.getContentData().array()));
134 candidateContentElement.getInfo()
135 .addProperty(InfoPropertyName.fileSuffix.name(), candidateData.getFileSuffix());
136 candidateContentElement.getInfo()
137 .addProperty(InfoPropertyName.fileName.name(), candidateData.getFileName());
138 candidateElement.addSubElement(candidateContentElement);
140 SessionContext context = createSessionContext();
141 ElementContext elementContext = new ElementContext(vspId, version.getId());
142 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
143 "Update Orchestration Template Candidate");
145 .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
150 public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
151 logger.info("Updating orchestration template for VSP id -> " + vspId);
153 ZusammenElement candidateElement =
154 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
156 .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
158 SessionContext context = createSessionContext();
159 ElementContext elementContext = new ElementContext(vspId, version.getId());
160 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
161 "Update Orchestration Template Candidate structure");
163 .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
168 public Optional<String> getStructure(String vspId, Version version) {
170 .info("Getting orchestration template structure for VendorSoftwareProduct id -> " + vspId);
172 SessionContext context = createSessionContext();
173 ElementContext elementContext = new ElementContext(vspId, version.getId());
176 "Finished getting orchestration template structure for VendorSoftwareProduct id -> " +
178 Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
179 ElementType.OrchestrationTemplateCandidate.name());
180 if (element.isPresent()) {
181 if (hasEmptyData(element.get().getData())) {
182 return Optional.empty();
184 return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
186 return Optional.empty();
190 private boolean hasEmptyData(InputStream elementData) {
191 String emptyData = "{}";
192 byte[] byteElementData;
194 byteElementData = IOUtils.toByteArray(elementData);
195 } catch (IOException ex) {
196 ex.printStackTrace();
199 if (Arrays.equals(emptyData.getBytes(), byteElementData)) {
205 public enum InfoPropertyName {