Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / dao / impl / zusammen / OrchestrationTemplateCandidateDaoZusammenImpl.java
1 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
2
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;
20
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;
27
28 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
29 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
30
31 public class OrchestrationTemplateCandidateDaoZusammenImpl
32     implements OrchestrationTemplateCandidateDao {
33
34   private static final Logger logger =
35       LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
36
37   private ZusammenAdaptor zusammenAdaptor;
38
39   public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
40     this.zusammenAdaptor = zusammenAdaptor;
41   }
42
43   @Override
44   public void registerVersioning(String versionableEntityType) {
45
46   }
47
48   @Override
49   public OrchestrationTemplateCandidateData get(String vspId, Version version) {
50     logger.info("Getting orchestration template for VendorSoftwareProduct id -> " + vspId);
51
52     SessionContext context = createSessionContext();
53     ElementContext elementContext = new ElementContext(vspId, version.getId());
54
55     Optional<Element> candidateElement =
56         zusammenAdaptor.getElementByName(context, elementContext, null,
57             ElementType.OrchestrationTemplateCandidate.name());
58     if (candidateElement.isPresent()) {
59       if (hasEmptyData(candidateElement.get().getData())) {
60         return null;
61       }
62       OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
63       candidateData.setFilesDataStructure(
64           new String(FileUtils.toByteArray(candidateElement.get().getData())));
65
66       Optional<Element> candidateContentElement = zusammenAdaptor
67           .getElementByName(context, elementContext, candidateElement.get().getElementId(),
68               ElementType.OrchestrationTemplateCandidateContent.name());
69
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()));
77       }
78       logger
79           .info("Finished getting orchestration template for VendorSoftwareProduct id -> " + vspId);
80       return candidateData;
81     }
82     logger.info(String
83         .format("Orchestration template for VendorSoftwareProduct id %s does not exist", vspId));
84     return null;
85   }
86
87   @Override
88   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
89     logger.info("Getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
90
91     SessionContext context = createSessionContext();
92     ElementContext elementContext = new ElementContext(vspId, version.getId());
93
94     Optional<ElementInfo> candidateElement =
95         zusammenAdaptor.getElementInfoByName(context, elementContext, null,
96             ElementType.OrchestrationTemplateCandidate.name());
97     if (candidateElement.isPresent()) {
98       OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
99
100       Optional<Element> candidateContentElement = zusammenAdaptor
101           .getElementByName(context, elementContext, candidateElement.get().getId(),
102               ElementType.OrchestrationTemplateCandidateContent.name());
103
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()));
109       }
110       logger.info(
111           "Finished getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
112       return candidateData;
113     }
114     logger.info(String
115         .format("Orchestration template info for VendorSoftwareProduct id %s does not exist",
116             vspId));
117     return null;
118   }
119
120   @Override
121   public void update(String vspId, Version version,
122                      OrchestrationTemplateCandidateData candidateData) {
123     logger.info("Uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
124
125     ZusammenElement candidateElement =
126         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
127     candidateElement
128         .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
129
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);
139
140     SessionContext context = createSessionContext();
141     ElementContext elementContext = new ElementContext(vspId, version.getId());
142     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
143         "Update Orchestration Template Candidate");
144     logger
145         .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
146   }
147
148
149   @Override
150   public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
151     logger.info("Updating orchestration template for VSP id -> " + vspId);
152
153     ZusammenElement candidateElement =
154         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
155     candidateElement
156         .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
157
158     SessionContext context = createSessionContext();
159     ElementContext elementContext = new ElementContext(vspId, version.getId());
160     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
161         "Update Orchestration Template Candidate structure");
162     logger
163         .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
164   }
165
166
167   @Override
168   public Optional<String> getStructure(String vspId, Version version) {
169     logger
170         .info("Getting orchestration template structure for VendorSoftwareProduct id -> " + vspId);
171
172     SessionContext context = createSessionContext();
173     ElementContext elementContext = new ElementContext(vspId, version.getId());
174
175     logger.info(
176         "Finished getting orchestration template structure for VendorSoftwareProduct id -> " +
177             vspId);
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();
183       }
184       return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
185     } else {
186       return Optional.empty();
187     }
188   }
189
190   private boolean hasEmptyData(InputStream elementData) {
191     String emptyData = "{}";
192     byte[] byteElementData;
193     try {
194       byteElementData = IOUtils.toByteArray(elementData);
195     } catch (IOException ex) {
196       ex.printStackTrace();
197       return false;
198     }
199     if (Arrays.equals(emptyData.getBytes(), byteElementData)) {
200       return true;
201     }
202     return false;
203   }
204
205   public enum InfoPropertyName {
206     fileSuffix,
207     fileName
208   }
209 }