f49832dc010bf053f93370d37712d00faa52df61
[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 com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
22 import com.amdocs.zusammen.datatypes.SessionContext;
23 import com.amdocs.zusammen.datatypes.item.Action;
24 import com.amdocs.zusammen.datatypes.item.ElementContext;
25 import com.amdocs.zusammen.utils.fileutils.FileUtils;
26 import org.openecomp.core.utilities.json.JsonUtil;
27 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
28 import org.openecomp.sdc.datatypes.model.ElementType;
29 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37 import java.io.ByteArrayInputStream;
38 import java.nio.ByteBuffer;
39 import java.util.Optional;
40
41 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
42 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
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 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(OrchestrationTemplateCandidateData candidate,
122                                  Element candidateInfoElement,
123                                  boolean fullData) {
124     if (candidateInfoElement.getInfo().getName()
125         .equals(ElementType.OrchestrationTemplateCandidateContent.name())) {
126
127       if (fullData) {
128         candidate
129             .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
130       }
131       candidate.setFileSuffix(
132           candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
133       candidate.setFileName(
134           candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
135
136     } else if (candidateInfoElement.getInfo().getName()
137         .equals(ElementType.OrchestrationTemplateCandidateValidationData.name())) {
138
139       candidate
140           .setValidationData(new String(FileUtils.toByteArray(candidateInfoElement.getData())));
141     }
142   }
143
144   @Override
145   public void delete(String vspId, Version version) {
146     ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
147
148     ZusammenElement candidateContentElement =
149         buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
150     candidateContentElement.setData(emptyData);
151
152     ZusammenElement validationData = buildStructuralElement(ElementType
153         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
154     validationData.setData(emptyData);
155
156     ZusammenElement candidateElement =
157         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
158     candidateElement.setData(emptyData);
159     candidateElement.addSubElement(candidateContentElement);
160     candidateElement.addSubElement(validationData);
161
162     SessionContext context = createSessionContext();
163     ElementContext elementContext = new ElementContext(vspId, version.getId());
164     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
165         "Delete Orchestration Template Candidate Elements's content");
166   }
167
168   @Override
169   public void update(String vspId, Version version,
170                      OrchestrationTemplateCandidateData candidateData) {
171     logger.info("Uploading candidate data entity for vsp id {}", vspId);
172
173     ZusammenElement candidateElement =
174         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
175     candidateElement
176         .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
177
178     ZusammenElement candidateContentElement =
179         buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
180     candidateContentElement
181         .setData(new ByteArrayInputStream(candidateData.getContentData().array()));
182     candidateContentElement.getInfo()
183         .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
184     candidateContentElement.getInfo()
185         .addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());
186
187     ZusammenElement validationData = buildStructuralElement(ElementType
188         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
189     if (candidateData.getValidationData() != null) {
190       validationData
191           .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
192     }
193     candidateElement.addSubElement(candidateContentElement);
194     candidateElement.addSubElement(validationData);
195     SessionContext context = createSessionContext();
196     ElementContext elementContext = new ElementContext(vspId, version.getId());
197     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
198         "Update Orchestration Template Candidate");
199     logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
200   }
201
202   @Override
203   public void updateValidationData(String vspId, Version version,
204                                    ValidationStructureList validationData) {
205     logger
206         .info("Updating validation data of orchestration template candidate for VSP id {} ", vspId);
207
208     ZusammenElement validationDataElement = buildStructuralElement(ElementType
209         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
210     validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA
211         .getBytes()) : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));
212
213     ZusammenElement candidateElement =
214         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
215     candidateElement.addSubElement(validationDataElement);
216
217     SessionContext context = createSessionContext();
218     ElementContext elementContext = new ElementContext(vspId, version.getId());
219     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
220         "Update Orchestration Template Candidate validation data");
221     logger
222         .info("Finished updating validation data of orchestration template candidate for VSP id {}",
223             vspId);
224   }
225
226   @Override
227   public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
228     logger.info("Updating orchestration template for VSP id {}", vspId);
229
230     ZusammenElement candidateElement =
231         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
232     candidateElement
233         .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
234
235     SessionContext context = createSessionContext();
236     ElementContext elementContext = new ElementContext(vspId, version.getId());
237     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
238         "Update Orchestration Template Candidate structure");
239     logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
240   }
241
242
243   @Override
244   public Optional<String> getStructure(String vspId, Version version) {
245     logger.info("Getting orchestration template candidate structure for vsp id {}", vspId);
246
247     SessionContext context = createSessionContext();
248     ElementContext elementContext = new ElementContext(vspId, version.getId());
249
250     Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
251         ElementType.OrchestrationTemplateCandidate.name());
252
253     if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
254       return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
255     }
256
257     logger.info("Finished getting orchestration template candidate structure for vsp id {}", vspId);
258
259     return Optional.empty();
260   }
261
262   public enum InfoPropertyName {
263     FILE_SUFFIX("fileSuffix"),
264     FILE_NAME("fileName");
265
266     private final String val;
267
268     InfoPropertyName(String val) {
269       this.val = val;
270     }
271
272     String getVal() {
273       return val;
274     }
275   }
276 }