fb267143755fdd7ca1b2802943c097b1740ea45d
[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 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
17
18 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
19 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
20
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
24 import com.amdocs.zusammen.datatypes.SessionContext;
25 import com.amdocs.zusammen.datatypes.item.Action;
26 import com.amdocs.zusammen.datatypes.item.ElementContext;
27 import com.amdocs.zusammen.utils.fileutils.FileUtils;
28 import java.io.ByteArrayInputStream;
29 import java.nio.ByteBuffer;
30 import java.util.Optional;
31 import org.openecomp.core.utilities.json.JsonUtil;
32 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
33 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
34 import org.openecomp.sdc.datatypes.model.ElementType;
35 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42
43 public class OrchestrationTemplateCandidateDaoZusammenImpl implements OrchestrationTemplateCandidateDao {
44
45     private static final Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);
46     private static final String EMPTY_DATA = "{}";
47     private final ZusammenAdaptor zusammenAdaptor;
48
49     public OrchestrationTemplateCandidateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
50         this.zusammenAdaptor = zusammenAdaptor;
51     }
52
53     @Override
54     public void registerVersioning(String versionableEntityType) {
55         // registerVersioning not implemented for OrchestrationTemplateCandidateDaoZusammenImpl
56     }
57
58     @Override
59     public Optional<OrchestrationTemplateCandidateData> get(String vspId, Version version) {
60         logger.info("Getting orchestration template for vsp id {}", vspId);
61         SessionContext context = createSessionContext();
62         ElementContext elementContext = new ElementContext(vspId, version.getId());
63         Optional<Element> candidateElement = zusammenAdaptor
64             .getElementByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
65         if (!candidateElement.isPresent() || VspZusammenUtil.hasEmptyData(candidateElement.get().getData()) || candidateElement.get().getSubElements()
66             .isEmpty()) {
67             logger.info("Orchestration template for vsp id {} does not exist / has empty data", vspId);
68             return Optional.empty();
69         }
70         OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
71         candidate.setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.get().getData())));
72         candidateElement.get().getSubElements().stream()
73             .map(element -> zusammenAdaptor.getElement(context, elementContext, element.getElementId().toString()))
74             .forEach(element -> element.ifPresent(candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, true)));
75         logger.info("Finished getting orchestration template for vsp id {}", vspId);
76         return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
77     }
78
79     @Override
80     public Optional<OrchestrationTemplateCandidateData> getInfo(String vspId, Version version) {
81         logger.info("Getting orchestration template info for vsp id {}", vspId);
82         SessionContext context = createSessionContext();
83         ElementContext elementContext = new ElementContext(vspId, version.getId());
84         Optional<ElementInfo> candidateElement = zusammenAdaptor
85             .getElementInfoByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
86         if (!candidateElement.isPresent() || candidateElement.get().getSubElements().isEmpty()) {
87             logger.info("Orchestration template info for vsp id {} does not exist", vspId);
88             return Optional.empty();
89         }
90         OrchestrationTemplateCandidateData candidate = new OrchestrationTemplateCandidateData();
91         candidateElement.get().getSubElements().stream()
92             .map(elementInfo -> zusammenAdaptor.getElement(context, elementContext, elementInfo.getId().toString()))
93             .forEach(element -> element.ifPresent(candidateInfoElement -> populateCandidate(candidate, candidateInfoElement, false)));
94         logger.info("Finished getting orchestration template info for vsp id {}", vspId);
95         return candidate.getFileSuffix() == null ? Optional.empty() : Optional.of(candidate);
96     }
97
98     private void populateCandidate(final OrchestrationTemplateCandidateData candidate, final Element candidateInfoElement, final boolean fullData) {
99         final String elementName = candidateInfoElement.getInfo().getName();
100         if (ElementType.OrchestrationTemplateCandidateContent.name().equals(elementName)) {
101             if (fullData) {
102                 candidate.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
103             }
104             candidate.setFileSuffix(candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
105             candidate.setFileName(candidateInfoElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
106         } else if (ElementType.OrchestrationTemplateCandidateValidationData.name().equals(elementName)) {
107             candidate.setValidationData(new String(FileUtils.toByteArray(candidateInfoElement.getData())));
108         } else if (ElementType.ORIGINAL_ONBOARDED_PACKAGE.name().equals(elementName)) {
109             candidate.setOriginalFileName(candidateInfoElement.getInfo().getProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal()));
110             candidate.setOriginalFileSuffix(candidateInfoElement.getInfo().getProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal()));
111             if (fullData) {
112                 candidate.setOriginalFileContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateInfoElement.getData())));
113             }
114         }
115     }
116
117     @Override
118     public void delete(String vspId, Version version) {
119         ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());
120         ZusammenElement candidateContentElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
121         candidateContentElement.setData(emptyData);
122         ZusammenElement validationData = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
123         validationData.setData(emptyData);
124         ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
125         candidateElement.setData(emptyData);
126         candidateElement.addSubElement(candidateContentElement);
127         candidateElement.addSubElement(validationData);
128         SessionContext context = createSessionContext();
129         ElementContext elementContext = new ElementContext(vspId, version.getId());
130         zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Delete Orchestration Template Candidate Elements's content");
131     }
132
133     @Override
134     public void update(final String vspId, final Version version, final OrchestrationTemplateCandidateData candidateData) {
135         logger.info("Uploading candidate data entity for vsp id {}", vspId);
136         final ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
137         candidateElement.setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));
138         final ZusammenElement candidateContentElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
139         candidateContentElement.setData(new ByteArrayInputStream(candidateData.getContentData().array()));
140         candidateContentElement.getInfo().addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
141         candidateContentElement.getInfo().addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());
142         if (OnboardingTypesEnum.CSAR.toString().equalsIgnoreCase(candidateData.getFileSuffix())) {
143             final ZusammenElement originalPackageElement = buildStructuralElement(ElementType.ORIGINAL_ONBOARDED_PACKAGE, Action.UPDATE);
144             originalPackageElement.getInfo().addProperty(InfoPropertyName.ORIGINAL_FILE_NAME.getVal(), candidateData.getOriginalFileName());
145             originalPackageElement.getInfo().addProperty(InfoPropertyName.ORIGINAL_FILE_SUFFIX.getVal(), candidateData.getOriginalFileSuffix());
146             originalPackageElement.setData(new ByteArrayInputStream(candidateData.getOriginalFileContentData().array()));
147             candidateElement.addSubElement(originalPackageElement);
148         }
149         final ZusammenElement validationData = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
150         if (candidateData.getValidationData() != null) {
151             validationData.setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
152         }
153         candidateElement.addSubElement(validationData);
154         candidateElement.addSubElement(candidateContentElement);
155         SessionContext context = createSessionContext();
156         ElementContext elementContext = new ElementContext(vspId, version.getId());
157         zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate");
158         logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
159     }
160
161     @Override
162     public void updateValidationData(String vspId, Version version, ValidationStructureList validationData) {
163         logger.info("Updating validation data of orchestration template candidate for VSP id {} ", vspId);
164         ZusammenElement validationDataElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE);
165         validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA.getBytes())
166             : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));
167         ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
168         candidateElement.addSubElement(validationDataElement);
169         SessionContext context = createSessionContext();
170         ElementContext elementContext = new ElementContext(vspId, version.getId());
171         zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate validation data");
172         logger.info("Finished updating validation data of orchestration template candidate for VSP id {}", vspId);
173     }
174
175     @Override
176     public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
177         logger.info("Updating orchestration template for VSP id {}", vspId);
178         ZusammenElement candidateElement = buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
179         candidateElement.setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));
180         SessionContext context = createSessionContext();
181         ElementContext elementContext = new ElementContext(vspId, version.getId());
182         zusammenAdaptor.saveElement(context, elementContext, candidateElement, "Update Orchestration Template Candidate structure");
183         logger.info("Finished uploading candidate data entity for vsp id {}", vspId);
184     }
185
186     @Override
187     public Optional<String> getStructure(String vspId, Version version) {
188         logger.info("Getting orchestration template candidate structure for vsp id {}", vspId);
189         SessionContext context = createSessionContext();
190         ElementContext elementContext = new ElementContext(vspId, version.getId());
191         Optional<Element> element = zusammenAdaptor
192             .getElementByName(context, elementContext, null, ElementType.OrchestrationTemplateCandidate.name());
193         if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
194             return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
195         }
196         logger.info("Finished getting orchestration template candidate structure for vsp id {}", vspId);
197         return Optional.empty();
198     }
199
200     public enum InfoPropertyName {
201         FILE_SUFFIX("fileSuffix"), FILE_NAME("fileName"), ORIGINAL_FILE_NAME("originalFilename"), ORIGINAL_FILE_SUFFIX("originalFileSuffix");
202         private final String val;
203
204         InfoPropertyName(String val) {
205             this.val = val;
206         }
207
208         private String getVal() {
209             return val;
210         }
211     }
212 }