3b3cdfead3812c8facf3c015a0e2e1c29f348c41
[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.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;
33
34 import java.io.ByteArrayInputStream;
35 import java.nio.ByteBuffer;
36 import java.util.Optional;
37
38 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
40
41 public class OrchestrationTemplateDaoZusammenImpl implements OrchestrationTemplateDao {
42
43   private static final Logger LOGGER = LoggerFactory.getLogger
44       (OrchestrationTemplateDaoZusammenImpl.class);
45   private ZusammenAdaptor zusammenAdaptor;
46
47   public OrchestrationTemplateDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
48     this.zusammenAdaptor = zusammenAdaptor;
49   }
50
51   @Override
52   public void registerVersioning(String versionableEntityType) {
53     // registerVersioning not implemented for OrchestrationTemplateDaoZusammenImpl
54   }
55
56   @Override
57   public OrchestrationTemplateEntity getInfo(String vspId, Version version) {
58     SessionContext context = createSessionContext();
59     ElementContext elementContext = new ElementContext(vspId, version.getId());
60
61     Optional<ElementInfo> vspModel = zusammenAdaptor
62         .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
63     if (!vspModel.isPresent()) {
64       return null;
65     }
66
67     Optional<ElementInfo> elementInfo = zusammenAdaptor
68         .getElementInfoByName(context, elementContext, vspModel.get().getId(),
69             ElementType.OrchestrationTemplate.name());
70     if (!elementInfo.isPresent()) {
71       return null;
72     }
73
74     Optional<Element> element =
75         zusammenAdaptor.getElementByName(context, elementContext, elementInfo.get().getId(),
76             ElementType.OrchestrationTemplateValidationData.name());
77
78     OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
79     if (!element.isPresent()) {
80       return orchestrationTemplate;
81     }
82     orchestrationTemplate
83         .setFileSuffix(element.get().getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
84     orchestrationTemplate
85         .setFileName(element.get().getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal()));
86     if (!VspZusammenUtil.hasEmptyData(element.get().getData())) {
87       orchestrationTemplate
88           .setValidationData(new String(FileUtils.toByteArray(element.get().getData())));
89     }
90     return orchestrationTemplate;
91   }
92
93   @Override
94   public OrchestrationTemplateEntity get(String vspId, Version version) {
95     SessionContext context = createSessionContext();
96     ElementContext elementContext = new ElementContext(vspId, version.getId());
97
98     OrchestrationTemplateEntity orchestrationTemplate = new OrchestrationTemplateEntity();
99
100     Optional<ElementInfo> vspModel = zusammenAdaptor
101         .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
102     if (!vspModel.isPresent()) {
103       return orchestrationTemplate;
104     }
105
106     Optional<Element> orchestrationTemplateElement = zusammenAdaptor
107         .getElementByName(context, elementContext, vspModel.get().getId(),
108             ElementType.OrchestrationTemplate.name());
109     if (!orchestrationTemplateElement.isPresent()) {
110       return orchestrationTemplate;
111     }
112
113     if (!VspZusammenUtil.hasEmptyData(orchestrationTemplateElement.get().getData())) {
114       orchestrationTemplate.setContentData(
115           ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.get().getData())));
116     }
117
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())));
130       }
131     }
132     return orchestrationTemplate;
133   }
134
135   @Override
136   public void update(String vspId, Version version,
137                      OrchestrationTemplateEntity orchestrationTemplate) {
138     SessionContext context = createSessionContext();
139     ElementContext elementContext = new ElementContext(vspId, version.getId());
140
141     ZusammenElement validationData =
142         buildStructuralElement(ElementType.OrchestrationTemplateValidationData, Action.UPDATE);
143     validationData
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()
153             .getBytes()));
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);
162
163     zusammenAdaptor.saveElement(context, elementContext, vspModel, "Update Orchestration Template");
164   }
165
166   @Override
167   public Optional<String> getOrchestrationTemplateStructure(String vspId, Version version) {
168     LOGGER.info("Getting orchestration template structure for VendorSoftwareProduct id" +
169         " " + "-> " + vspId);
170
171     SessionContext context = createSessionContext();
172     ElementContext elementContext = new ElementContext(vspId, version.getId());
173
174     Optional<ElementInfo> vspModel = zusammenAdaptor.getElementInfoByName(context, elementContext,
175         null, ElementType.VspModel.name());
176     if (!vspModel.isPresent()) {
177       return Optional.empty();
178     }
179     Optional<Element> orchestrationTemplateElement = zusammenAdaptor.getElementByName(context,
180         elementContext, vspModel.get().getId(), ElementType.OrchestrationTemplate.name());
181     if (!orchestrationTemplateElement.isPresent()) {
182       return Optional.empty();
183     }
184
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())));
193     }
194     LOGGER.info("Finished getting orchestration template structure for VendorSoftwareProduct " +
195         "id -> " + vspId);
196
197     return Optional.empty();
198   }
199
200   private enum InfoPropertyName {
201     FILE_SUFFIX("fileSuffix"),
202     FILE_NAME("fileName");
203
204     private String val;
205
206     InfoPropertyName(String val){
207       this.val=val;
208     }
209
210     public String getVal() {
211       return val;
212     }
213   }
214 }