4c1e2e1ef23eeaac61a37c6f070f398b13374f84
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
18
19 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
21
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;
43
44 public class OrchestrationTemplateCandidateDaoZusammenImpl
45     implements OrchestrationTemplateCandidateDao {
46
47   private static final Logger logger =
48       LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
49
50   private final ZusammenAdaptor zusammenAdaptor;
51
52   private static final String EMPTY_DATA = "{}";
53
54   public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
55     this.zusammenAdaptor = zusammenAdaptor;
56   }
57
58   @Override
59   public void registerVersioning(String versionableEntityType) {
60     // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
61   }
62
63   @Override
64   public Optional<OrchestrationTemplateCandidateData> get(String vspId, Version version) {
65     logger.info("Getting orchestration template for vsp id {}", vspId);
66
67     SessionContext context = createSessionContext();
68     ElementContext elementContext = new ElementContext(vspId, version.getId());
69
70     Optional<Element> candidateElement =
71         zusammenAdaptor.getElementByName(context, elementContext, null,
72             ElementType.OrchestrationTemplateCandidate.name());
73
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();
79     }
80
81     OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
82     candidate.setFilesDataStructure(
83         new String(FileUtils.toByteArray(candidateElement.get().getData())));
84
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)));
90
91     logger.info("Finished getting orchestration template for vsp id {}", vspId);
92     return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
93   }
94
95   @Override
96   public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
97     logger.info("Getting orchestration template info for vsp id {}", vspId);
98
99     SessionContext context = createSessionContext();
100     ElementContext elementContext = new ElementContext(vspId, version.getId());
101
102     Optional<ElementInfo> candidateElement =
103         zusammenAdaptor.getElementInfoByName(context, elementContext, null,
104             ElementType.OrchestrationTemplateCandidate.name());
105
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();
109     }
110
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);
119   }
120
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)) {
126       if (fullData) {
127         candidate.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
128       }
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()));
140       if (fullData) {
141         candidate.setOriginalFileContentData(
142             ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData()))
143         );
144       }
145     }
146   }
147
148   @Override
149   public void delete(String vspId, Version version) {
150     ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
151
152     ZusammenElement candidateContentElement =
153         buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
154     candidateContentElement.setData(emptyData);
155
156     ZusammenElement validationData = buildStructuralElement(ElementType
157         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
158     validationData.setData(emptyData);
159
160     ZusammenElement candidateElement =
161         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
162     candidateElement.setData(emptyData);
163     candidateElement.addSubElement(candidateContentElement);
164     candidateElement.addSubElement(validationData);
165
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");
170   }
171
172   @Override
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);
178     candidateElement
179         .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
180
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());
189
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);
199     }
200     final ZusammenElement validationData = buildStructuralElement(ElementType
201         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
202     if (candidateData.getValidationData() != null) {
203       validationData
204           .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
205     }
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);
213   }
214
215   @Override
216   public void updateValidationData(String vspId, Version version,
217                                    ValidationStructureList validationData) {
218     logger
219         .info("Updating validation data of orchestration template candidate for VSP id {} ", vspId);
220
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()));
225
226     ZusammenElement candidateElement =
227         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
228     candidateElement.addSubElement(validationDataElement);
229
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");
234     logger
235         .info("Finished updating validation data of orchestration template candidate for VSP id {}",
236             vspId);
237   }
238
239   @Override
240   public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
241     logger.info("Updating orchestration template for VSP id {}", vspId);
242
243     ZusammenElement candidateElement =
244         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
245     candidateElement
246         .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
247
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);
253   }
254
255
256   @Override
257   public Optional<String> getStructure(String vspId, Version version) {
258     logger.info("Getting orchestration template candidate structure for vsp id {}", vspId);
259
260     SessionContext context = createSessionContext();
261     ElementContext elementContext = new ElementContext(vspId, version.getId());
262
263     Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
264         ElementType.OrchestrationTemplateCandidate.name());
265
266     if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
267       return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
268     }
269
270     logger.info("Finished getting orchestration template candidate structure for vsp id {}", vspId);
271
272     return Optional.empty();
273   }
274
275   public enum InfoPropertyName {
276     FILE_SUFFIX("fileSuffix"),
277     FILE_NAME("fileName"),
278     ORIGINAL_FILE_NAME("originalFilename"),
279     ORIGINAL_FILE_SUFFIX("originalFileSuffix");
280
281     private final String val;
282
283     InfoPropertyName(String val) {
284       this.val = val;
285     }
286
287     private String getVal() {
288       return val;
289     }
290   }
291 }