bf592e21691b4d55404d834d8cc80ce0f7643249
[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.Collection;
40 import java.util.Optional;
41
42 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
44
45 public class OrchestrationTemplateCandidateDaoZusammenImpl
46     implements OrchestrationTemplateCandidateDao {
47
48   private static final Logger logger =
49       LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
50
51   private ZusammenAdaptor zusammenAdaptor;
52
53   private static final String EMPTY_DATA = "{}";
54
55   public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
56     this.zusammenAdaptor = zusammenAdaptor;
57   }
58
59   @Override
60   public void registerVersioning(String versionableEntityType) {
61     // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
62   }
63
64   @Override
65   public OrchestrationTemplateCandidateData get(String vspId, Version version) {
66     logger.info("Getting orchestration template for VendorSoftwareProduct id -> " + vspId);
67
68     SessionContext context = createSessionContext();
69     ElementContext elementContext = new ElementContext(vspId, version.getId());
70
71     Optional<Element> candidateElement =
72         zusammenAdaptor.getElementByName(context, elementContext, null,
73             ElementType.OrchestrationTemplateCandidate.name());
74     if (candidateElement.isPresent()) {
75       if (VspZusammenUtil.hasEmptyData(candidateElement.get().getData())) {
76         return null;
77       }
78       OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
79       candidateData.setFilesDataStructure(
80           new String(FileUtils.toByteArray(candidateElement.get().getData())));
81
82       Collection<Element> subElements = candidateElement.get().getSubElements();
83       if (subElements.isEmpty()) {
84         return candidateData;
85       }
86
87       for (Element element : subElements) {
88         Optional<Element> subElement = zusammenAdaptor.getElement(context,
89             elementContext, element.getElementId().toString());
90
91         if (subElement.get().getInfo().getName()
92             .equals(ElementType.OrchestrationTemplateCandidateContent
93                 .name())) {
94           candidateData.setContentData(
95               ByteBuffer.wrap(FileUtils.toByteArray(subElement.get().getData())));
96           candidateData.setFileSuffix(subElement.get().getInfo()
97               .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
98           candidateData.setFileName(subElement.get().getInfo()
99               .getProperty(InfoPropertyName.FILE_NAME.getVal()));
100         } else if (subElement.get().getInfo().getName()
101             .equals(ElementType.OrchestrationTemplateCandidateValidationData.name())) {
102           candidateData.setValidationData(new String(FileUtils.toByteArray(subElement
103               .get().getData())));
104         }
105       }
106
107       logger
108           .info("Finished getting orchestration template for VendorSoftwareProduct id -> " + vspId);
109       return candidateData;
110     }
111     logger.info(String
112         .format("Orchestration template for VendorSoftwareProduct id %s does not exist", vspId));
113     return null;
114   }
115
116   @Override
117   public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
118     logger.info("Getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
119
120     SessionContext context = createSessionContext();
121     ElementContext elementContext = new ElementContext(vspId, version.getId());
122
123     OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
124
125     Optional<ElementInfo> candidateElement =
126         zusammenAdaptor.getElementInfoByName(context, elementContext, null,
127             ElementType.OrchestrationTemplateCandidate.name());
128
129     if (candidateElement.isPresent()) {
130       Collection<ElementInfo> subElements = candidateElement.get().getSubElements();
131       if (subElements.isEmpty()) {
132         return candidateData;
133       }
134
135       for (ElementInfo elementInfo : subElements) {
136         Optional<Element> subElement = zusammenAdaptor.getElement(context,
137             elementContext, elementInfo.getId().toString());
138
139         if (subElement.get().getInfo().getName().equals(ElementType
140             .OrchestrationTemplateCandidateContent.name())) {
141
142           candidateData.setFileSuffix(subElement.get().getInfo()
143               .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
144           candidateData.setFileName(subElement.get().getInfo()
145               .getProperty(InfoPropertyName.FILE_NAME.getVal()));
146         } else if (subElement.get().getInfo().getName().equals(ElementType
147             .OrchestrationTemplateCandidateValidationData.name())) {
148           candidateData.setValidationData(new String(FileUtils.toByteArray(subElement.get()
149               .getData())));
150         }
151       }
152
153       logger.info(
154           "Finished getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
155       return candidateData;
156     }
157     logger.info(String
158         .format("Orchestration template info for VendorSoftwareProduct id %s does not exist",
159             vspId));
160     return null;
161   }
162
163   @Override
164   public void delete(String vspId, Version version) {
165     ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
166
167     ZusammenElement candidateContentElement =
168         buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
169     candidateContentElement.setData(emptyData);
170
171     ZusammenElement validationData = buildStructuralElement(ElementType
172         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
173     validationData.setData(emptyData);
174
175     ZusammenElement candidateElement =
176         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
177     candidateElement.setData(emptyData);
178     candidateElement.addSubElement(candidateContentElement);
179     candidateElement.addSubElement(validationData);
180
181     SessionContext context = createSessionContext();
182     ElementContext elementContext = new ElementContext(vspId, version.getId());
183     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
184         "Delete Orchestration Template Candidate Elements's content");
185   }
186
187   @Override
188   public void update(String vspId, Version version,
189                      OrchestrationTemplateCandidateData candidateData) {
190     logger.info("Uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
191
192     ZusammenElement candidateElement =
193         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
194     candidateElement
195         .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
196
197     ZusammenElement candidateContentElement =
198         buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
199     candidateContentElement
200         .setData(new ByteArrayInputStream(candidateData.getContentData().array()));
201     candidateContentElement.getInfo()
202         .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
203     candidateContentElement.getInfo()
204         .addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());
205
206     ZusammenElement validationData = buildStructuralElement(ElementType
207         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
208     if (candidateData.getValidationData() != null) {
209       validationData
210           .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
211     }
212     candidateElement.addSubElement(candidateContentElement);
213     candidateElement.addSubElement(validationData);
214     SessionContext context = createSessionContext();
215     ElementContext elementContext = new ElementContext(vspId, version.getId());
216     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
217         "Update Orchestration Template Candidate");
218     logger
219         .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
220   }
221
222   @Override
223   public void updateValidationData(String vspId, Version version, ValidationStructureList
224       validationData) {
225     logger.info("Updating validation data of  orchestration template candidate for VSP id -> "
226         + vspId);
227
228     ZusammenElement validationDataElement = buildStructuralElement(ElementType
229         .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
230     validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA
231         .getBytes()) : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));
232
233     ZusammenElement candidateElement =
234         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
235     candidateElement.addSubElement(validationDataElement);
236
237     SessionContext context = createSessionContext();
238     ElementContext elementContext = new ElementContext(vspId, version.getId());
239     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
240         "Update Orchestration Template Candidate validation data");
241     logger.info("Finished updating validation data of  orchestration template candidate for VSP "
242         + "id -> " + vspId);
243   }
244
245   @Override
246   public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
247     logger.info("Updating orchestration template for VSP id -> " + vspId);
248
249     ZusammenElement candidateElement =
250         buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
251     candidateElement
252         .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
253
254     SessionContext context = createSessionContext();
255     ElementContext elementContext = new ElementContext(vspId, version.getId());
256     zusammenAdaptor.saveElement(context, elementContext, candidateElement,
257         "Update Orchestration Template Candidate structure");
258     logger
259         .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
260   }
261
262
263   @Override
264   public Optional<String> getStructure(String vspId, Version version) {
265     logger.info("Getting orchestration template candidate structure for VendorSoftwareProduct id "
266         + "-> " + vspId);
267
268     SessionContext context = createSessionContext();
269     ElementContext elementContext = new ElementContext(vspId, version.getId());
270
271     Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
272         ElementType.OrchestrationTemplateCandidate.name());
273
274     if (element.isPresent() && !VspZusammenUtil.isEmpty(element.get().getData())) {
275       return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
276     }
277
278     logger.info(
279         "Finished getting orchestration template candidate structure for VendorSoftwareProduct "
280             + "id -> " + vspId);
281
282     return Optional.empty();
283   }
284
285   public enum InfoPropertyName {
286     FILE_SUFFIX("fileSuffix"),
287     FILE_NAME("fileName");
288
289     private String val;
290
291     InfoPropertyName(String val){
292       this.val = val;
293     }
294
295     public String getVal() {
296       return val;
297     }
298   }
299 }