2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
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;
37 import java.io.ByteArrayInputStream;
38 import java.nio.ByteBuffer;
39 import java.util.Collection;
40 import java.util.Optional;
42 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
43 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
45 public class OrchestrationTemplateCandidateDaoZusammenImpl
46 implements OrchestrationTemplateCandidateDao {
48 private static final Logger logger =
49 LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
51 private ZusammenAdaptor zusammenAdaptor;
53 private static final String EMPTY_DATA = "{}";
55 public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
56 this.zusammenAdaptor = zusammenAdaptor;
60 public void registerVersioning(String versionableEntityType) {
61 // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
65 public OrchestrationTemplateCandidateData get(String vspId, Version version) {
66 logger.info("Getting orchestration template for VendorSoftwareProduct id -> " + vspId);
68 SessionContext context = createSessionContext();
69 ElementContext elementContext = new ElementContext(vspId, version.getId());
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())) {
78 OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
79 candidateData.setFilesDataStructure(
80 new String(FileUtils.toByteArray(candidateElement.get().getData())));
82 Collection<Element> subElements = candidateElement.get().getSubElements();
83 if (subElements.isEmpty()) {
87 for (Element element : subElements) {
88 Optional<Element> subElement = zusammenAdaptor.getElement(context,
89 elementContext, element.getElementId().toString());
91 if (subElement.get().getInfo().getName()
92 .equals(ElementType.OrchestrationTemplateCandidateContent
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
108 .info("Finished getting orchestration template for VendorSoftwareProduct id -> " + vspId);
109 return candidateData;
112 .format("Orchestration template for VendorSoftwareProduct id %s does not exist", vspId));
117 public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
118 logger.info("Getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
120 SessionContext context = createSessionContext();
121 ElementContext elementContext = new ElementContext(vspId, version.getId());
123 OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
125 Optional<ElementInfo> candidateElement =
126 zusammenAdaptor.getElementInfoByName(context, elementContext, null,
127 ElementType.OrchestrationTemplateCandidate.name());
129 if (candidateElement.isPresent()) {
130 Collection<ElementInfo> subElements = candidateElement.get().getSubElements();
131 if (subElements.isEmpty()) {
132 return candidateData;
135 for (ElementInfo elementInfo : subElements) {
136 Optional<Element> subElement = zusammenAdaptor.getElement(context,
137 elementContext, elementInfo.getId().toString());
139 if (subElement.get().getInfo().getName().equals(ElementType
140 .OrchestrationTemplateCandidateContent.name())) {
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()
154 "Finished getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
155 return candidateData;
158 .format("Orchestration template info for VendorSoftwareProduct id %s does not exist",
164 public void delete(String vspId, Version version) {
165 ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
167 ZusammenElement candidateContentElement =
168 buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
169 candidateContentElement.setData(emptyData);
171 ZusammenElement validationData = buildStructuralElement(ElementType
172 .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
173 validationData.setData(emptyData);
175 ZusammenElement candidateElement =
176 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
177 candidateElement.setData(emptyData);
178 candidateElement.addSubElement(candidateContentElement);
179 candidateElement.addSubElement(validationData);
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");
188 public void update(String vspId, Version version,
189 OrchestrationTemplateCandidateData candidateData) {
190 logger.info("Uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
192 ZusammenElement candidateElement =
193 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
195 .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
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());
206 ZusammenElement validationData = buildStructuralElement(ElementType
207 .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
208 if (candidateData.getValidationData() != null) {
210 .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
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");
219 .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
223 public void updateValidationData(String vspId, Version version, ValidationStructureList
225 logger.info("Updating validation data of orchestration template candidate for VSP id -> "
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()));
233 ZusammenElement candidateElement =
234 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
235 candidateElement.addSubElement(validationDataElement);
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 "
246 public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
247 logger.info("Updating orchestration template for VSP id -> " + vspId);
249 ZusammenElement candidateElement =
250 buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
252 .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
254 SessionContext context = createSessionContext();
255 ElementContext elementContext = new ElementContext(vspId, version.getId());
256 zusammenAdaptor.saveElement(context, elementContext, candidateElement,
257 "Update Orchestration Template Candidate structure");
259 .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
264 public Optional<String> getStructure(String vspId, Version version) {
265 logger.info("Getting orchestration template candidate structure for VendorSoftwareProduct id "
268 SessionContext context = createSessionContext();
269 ElementContext elementContext = new ElementContext(vspId, version.getId());
271 Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
272 ElementType.OrchestrationTemplateCandidate.name());
274 if (element.isPresent() && !VspZusammenUtil.isEmpty(element.get().getData())) {
275 return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
279 "Finished getting orchestration template candidate structure for VendorSoftwareProduct "
282 return Optional.empty();
285 public enum InfoPropertyName {
286 FILE_SUFFIX("fileSuffix"),
287 FILE_NAME("fileName");
291 InfoPropertyName(String val){
295 public String getVal() {