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.Collection;
37 import java.util.Optional;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
42 public class OrchestrationTemplateDaoZusammenImpl implements OrchestrationTemplateDao {
44 private static final Logger LOGGER = LoggerFactory.getLogger(
45 OrchestrationTemplateDaoZusammenImpl.class);
46 private ZusammenAdaptor zusammenAdaptor;
48 public OrchestrationTemplateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
49 this.zusammenAdaptor = zusammenAdaptor;
53 public void registerVersioning(String versionableEntityType) {
54 // registerVersioning not implemented for OrchestrationTemplateDaoZusammenImpl
58 public OrchestrationTemplateEntity getInfo(String vspId, Version version) {
59 SessionContext context = createSessionContext();
60 ElementContext elementContext = new ElementContext(vspId, version.getId());
62 Optional<ElementInfo> vspModel = zusammenAdaptor
63 .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
64 if (!vspModel.isPresent()) {
68 Optional<ElementInfo> elementInfo = zusammenAdaptor
69 .getElementInfoByName(context, elementContext, vspModel.get().getId(),
70 ElementType.OrchestrationTemplate.name());
71 if (!elementInfo.isPresent()) {
75 Optional<Element> element =
76 zusammenAdaptor.getElementByName(context, elementContext, elementInfo.get().getId(),
77 ElementType.OrchestrationTemplateValidationData.name());
79 OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
80 if (!element.isPresent()) {
81 return orchestrationTemplate;
84 .setFileSuffix(element.get().getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
86 .setFileName(element.get().getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
87 if (!VspZusammenUtil.hasEmptyData(element.get().getData())) {
89 .setValidationData(new String(FileUtils.toByteArray(element.get().getData())));
91 return orchestrationTemplate;
95 public OrchestrationTemplateEntity get(String vspId, Version version) {
96 SessionContext context = createSessionContext();
97 ElementContext elementContext = new ElementContext(vspId, version.getId());
99 OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
101 Optional<ElementInfo> vspModel = zusammenAdaptor
102 .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
103 if (!vspModel.isPresent()) {
104 return orchestrationTemplate;
107 Optional<Element> orchestrationTemplateElement = zusammenAdaptor
108 .getElementByName(context, elementContext, vspModel.get().getId(),
109 ElementType.OrchestrationTemplate.name());
110 if (orchestrationTemplateElement.isPresent() &&
111 VspZusammenUtil.hasEmptyData(orchestrationTemplateElement.get().getData())) {
112 return orchestrationTemplate;
115 orchestrationTemplate.setContentData(
116 ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.get().getData())));
118 Collection<Element> subElements = orchestrationTemplateElement.get().getSubElements();
119 if (subElements.isEmpty()) {
120 return orchestrationTemplate;
123 for (Element element : subElements) {
124 Optional<Element> subElement = zusammenAdaptor.getElement(context,
125 elementContext, element.getElementId().toString());
127 if (subElement.get().getInfo().getName().equals(ElementType
128 .OrchestrationTemplateValidationData.name())) {
129 orchestrationTemplate.setFileSuffix(subElement.get().getInfo()
130 .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
131 orchestrationTemplate.setFileName(subElement.get().getInfo()
132 .getProperty(InfoPropertyName.FILE_NAME.getVal()));
133 if (!VspZusammenUtil.hasEmptyData(subElement.get().getData())) {
134 orchestrationTemplate.setValidationData(
135 new String(FileUtils.toByteArray(subElement.get().getData())));
137 } else if (subElement.get().getInfo().getName().equals(ElementType
138 .OrchestrationTemplateStructure.name())) {
139 orchestrationTemplate.setFilesDataStructure(new String(FileUtils.toByteArray(subElement
144 return orchestrationTemplate;
148 public void update(String vspId, Version version,
149 OrchestrationTemplateEntity orchestrationTemplate) {
150 SessionContext context = createSessionContext();
151 ElementContext elementContext = new ElementContext(vspId, version.getId());
153 ZusammenElement validationData =
154 buildStructuralElement(ElementType.OrchestrationTemplateValidationData, Action.UPDATE);
156 .setData(new ByteArrayInputStream(orchestrationTemplate.getValidationData().getBytes()));
157 validationData.getInfo()
158 .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), orchestrationTemplate.getFileSuffix());
159 validationData.getInfo()
160 .addProperty(InfoPropertyName.FILE_NAME.getVal(), orchestrationTemplate.getFileName());
161 ZusammenElement orchestrationTemplateStructure = buildStructuralElement(ElementType
162 .OrchestrationTemplateStructure, Action.UPDATE);
163 orchestrationTemplateStructure
164 .setData(new ByteArrayInputStream(orchestrationTemplate.getFilesDataStructure()
166 ZusammenElement orchestrationTemplateElement =
167 buildStructuralElement(ElementType.OrchestrationTemplate, Action.UPDATE);
168 orchestrationTemplateElement
169 .setData(new ByteArrayInputStream(orchestrationTemplate.getContentData().array()));
170 orchestrationTemplateElement.addSubElement(validationData);
171 orchestrationTemplateElement.addSubElement(orchestrationTemplateStructure);
172 ZusammenElement vspModel = buildStructuralElement(ElementType.VspModel, Action.IGNORE);
173 vspModel.addSubElement(orchestrationTemplateElement);
175 zusammenAdaptor.saveElement(context, elementContext, vspModel, "Update Orchestration Template");
179 public Optional<String> getOrchestrationTemplateStructure(String vspId, Version version) {
180 LOGGER.info("Getting orchestration template structure for VendorSoftwareProduct id" +
181 " " + "-> " + vspId);
183 SessionContext context = createSessionContext();
184 ElementContext elementContext = new ElementContext(vspId, version.getId());
186 Optional<ElementInfo> vspModel = zusammenAdaptor.getElementInfoByName(context, elementContext,
187 null, ElementType.VspModel.name());
188 if (!vspModel.isPresent()) {
189 return Optional.empty();
191 Optional<Element> orchestrationTemplateElement = zusammenAdaptor.getElementByName(context,
192 elementContext, vspModel.get().getId(), ElementType.OrchestrationTemplate.name());
193 if (!orchestrationTemplateElement.isPresent()) {
194 return Optional.empty();
197 Optional<Element> orchestrationTemplateStructureElement = zusammenAdaptor
198 .getElementByName(context, elementContext,
199 orchestrationTemplateElement.get().getElementId(),
200 ElementType.OrchestrationTemplateStructure.name());
201 if (orchestrationTemplateStructureElement.isPresent() &&
202 !VspZusammenUtil.hasEmptyData(orchestrationTemplateStructureElement.get().getData())) {
203 return Optional.of(new String(
204 FileUtils.toByteArray(orchestrationTemplateStructureElement.get().getData())));
206 LOGGER.info("Finished getting orchestration template structure for VendorSoftwareProduct " +
209 return Optional.empty();
212 private enum InfoPropertyName {
213 FILE_SUFFIX("fileSuffix"),
214 FILE_NAME("fileName");
218 InfoPropertyName(String val){
222 public String getVal() {