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.zusammen.api.ZusammenAdaptor;
27 import org.openecomp.sdc.datatypes.model.ElementType;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
32 import org.openecomp.sdc.versioning.dao.types.Version;
34 import java.io.ByteArrayInputStream;
35 import java.nio.ByteBuffer;
36 import java.util.Optional;
38 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
41 public class OrchestrationTemplateDaoZusammenImpl implements OrchestrationTemplateDao {
43 private static final Logger LOGGER = LoggerFactory.getLogger
44 (OrchestrationTemplateDaoZusammenImpl.class);
45 private ZusammenAdaptor zusammenAdaptor;
47 public OrchestrationTemplateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
48 this.zusammenAdaptor = zusammenAdaptor;
52 public void registerVersioning(String versionableEntityType) {
53 // registerVersioning not implemented for OrchestrationTemplateDaoZusammenImpl
57 public OrchestrationTemplateEntity getInfo(String vspId, Version version) {
58 SessionContext context = createSessionContext();
59 ElementContext elementContext = new ElementContext(vspId, version.getId());
61 Optional<ElementInfo> vspModel = zusammenAdaptor
62 .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
63 if (!vspModel.isPresent()) {
67 Optional<ElementInfo> elementInfo = zusammenAdaptor
68 .getElementInfoByName(context, elementContext, vspModel.get().getId(),
69 ElementType.OrchestrationTemplate.name());
70 if (!elementInfo.isPresent()) {
74 Optional<Element> element =
75 zusammenAdaptor.getElementByName(context, elementContext, elementInfo.get().getId(),
76 ElementType.OrchestrationTemplateValidationData.name());
78 OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
79 if (!element.isPresent()) {
80 return orchestrationTemplate;
83 .setFileSuffix(element.get().getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
85 .setFileName(element.get().getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
86 if (!VspZusammenUtil.hasEmptyData(element.get().getData())) {
88 .setValidationData(new String(FileUtils.toByteArray(element.get().getData())));
90 return orchestrationTemplate;
94 public OrchestrationTemplateEntity get(String vspId, Version version) {
95 SessionContext context = createSessionContext();
96 ElementContext elementContext = new ElementContext(vspId, version.getId());
98 OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
100 Optional<ElementInfo> vspModel = zusammenAdaptor
101 .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
102 if (!vspModel.isPresent()) {
103 return orchestrationTemplate;
106 Optional<Element> orchestrationTemplateElement = zusammenAdaptor
107 .getElementByName(context, elementContext, vspModel.get().getId(),
108 ElementType.OrchestrationTemplate.name());
109 if (!orchestrationTemplateElement.isPresent()) {
110 return orchestrationTemplate;
113 if (!VspZusammenUtil.hasEmptyData(orchestrationTemplateElement.get().getData())) {
114 orchestrationTemplate.setContentData(
115 ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.get().getData())));
118 Optional<Element> validationDataElement =
119 zusammenAdaptor.getElementByName(context, elementContext,
120 orchestrationTemplateElement.get().getElementId(),
121 ElementType.OrchestrationTemplateValidationData.name());
122 if (validationDataElement.isPresent()) {
123 orchestrationTemplate.setFileSuffix(validationDataElement.get().getInfo()
124 .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
125 orchestrationTemplate.setFileName(validationDataElement.get().getInfo()
126 .getProperty(InfoPropertyName.FILE_NAME.getVal()));
127 if (!VspZusammenUtil.hasEmptyData(validationDataElement.get().getData())) {
128 orchestrationTemplate.setValidationData(
129 new String(FileUtils.toByteArray(validationDataElement.get().getData())));
132 return orchestrationTemplate;
136 public void update(String vspId, Version version,
137 OrchestrationTemplateEntity orchestrationTemplate) {
138 SessionContext context = createSessionContext();
139 ElementContext elementContext = new ElementContext(vspId, version.getId());
141 ZusammenElement validationData =
142 buildStructuralElement(ElementType.OrchestrationTemplateValidationData, Action.UPDATE);
144 .setData(new ByteArrayInputStream(orchestrationTemplate.getValidationData().getBytes()));
145 validationData.getInfo()
146 .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), orchestrationTemplate.getFileSuffix());
147 validationData.getInfo()
148 .addProperty(InfoPropertyName.FILE_NAME.getVal(), orchestrationTemplate.getFileName());
149 ZusammenElement orchestrationTemplateStructure = buildStructuralElement(ElementType
150 .OrchestrationTemplateStructure, Action.UPDATE);
151 orchestrationTemplateStructure
152 .setData(new ByteArrayInputStream(orchestrationTemplate.getFilesDataStructure()
154 ZusammenElement orchestrationTemplateElement =
155 buildStructuralElement(ElementType.OrchestrationTemplate, Action.UPDATE);
156 orchestrationTemplateElement
157 .setData(new ByteArrayInputStream(orchestrationTemplate.getContentData().array()));
158 orchestrationTemplateElement.addSubElement(validationData);
159 orchestrationTemplateElement.addSubElement(orchestrationTemplateStructure);
160 ZusammenElement vspModel = buildStructuralElement(ElementType.VspModel, Action.IGNORE);
161 vspModel.addSubElement(orchestrationTemplateElement);
163 zusammenAdaptor.saveElement(context, elementContext, vspModel, "Update Orchestration Template");
167 public Optional<String> getOrchestrationTemplateStructure(String vspId, Version version) {
168 LOGGER.info("Getting orchestration template structure for VendorSoftwareProduct id" +
169 " " + "-> " + vspId);
171 SessionContext context = createSessionContext();
172 ElementContext elementContext = new ElementContext(vspId, version.getId());
174 Optional<ElementInfo> vspModel = zusammenAdaptor.getElementInfoByName(context, elementContext,
175 null, ElementType.VspModel.name());
176 if (!vspModel.isPresent()) {
177 return Optional.empty();
179 Optional<Element> orchestrationTemplateElement = zusammenAdaptor.getElementByName(context,
180 elementContext, vspModel.get().getId(), ElementType.OrchestrationTemplate.name());
181 if (!orchestrationTemplateElement.isPresent()) {
182 return Optional.empty();
185 Optional<Element> orchestrationTemplateStructureElement = zusammenAdaptor
186 .getElementByName(context, elementContext,
187 orchestrationTemplateElement.get().getElementId(),
188 ElementType.OrchestrationTemplateStructure.name());
189 if (orchestrationTemplateStructureElement.isPresent() &&
190 !VspZusammenUtil.isEmpty(orchestrationTemplateStructureElement.get().getData())) {
191 return Optional.of(new String(
192 FileUtils.toByteArray(orchestrationTemplateStructureElement.get().getData())));
194 LOGGER.info("Finished getting orchestration template structure for VendorSoftwareProduct " +
197 return Optional.empty();
200 private enum InfoPropertyName {
201 FILE_SUFFIX("fileSuffix"),
202 FILE_NAME("fileName");
206 InfoPropertyName(String val){
210 public String getVal() {