1 package org.openecomp.sdc.model.impl.zusammen;
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.Id;
7 import com.amdocs.zusammen.datatypes.SessionContext;
8 import com.amdocs.zusammen.datatypes.item.Action;
9 import com.amdocs.zusammen.datatypes.item.ElementContext;
10 import com.amdocs.zusammen.datatypes.item.Info;
11 import com.amdocs.zusammen.datatypes.item.ItemVersion;
12 import org.apache.commons.io.IOUtils;
13 import org.openecomp.core.model.dao.ServiceModelDao;
14 import org.openecomp.core.model.errors.RetrieveServiceTemplateFromDbErrorBuilder;
15 import org.openecomp.core.model.types.ServiceElement;
16 import org.openecomp.core.utilities.file.FileContentHandler;
17 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
18 import org.openecomp.core.zusammen.api.ZusammenUtil;
19 import org.openecomp.sdc.common.errors.CoreException;
20 import org.openecomp.sdc.logging.api.Logger;
21 import org.openecomp.sdc.logging.api.LoggerFactory;
22 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
23 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
24 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
25 import org.openecomp.sdc.versioning.dao.types.Version;
26 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
27 import org.openecomp.core.converter.datatypes.Constants;
29 import java.io.ByteArrayInputStream;
30 import java.util.Collection;
32 import java.util.Objects;
33 import java.util.Optional;
34 import java.util.stream.Collectors;
36 public class ServiceModelDaoZusammenImpl
37 implements ServiceModelDao<ToscaServiceModel, ServiceElement> {
38 private static final Logger logger = LoggerFactory.getLogger(ServiceModelDaoZusammenImpl.class);
40 protected ZusammenAdaptor zusammenAdaptor;
41 protected String name;
43 public ServiceModelDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
44 this.zusammenAdaptor = zusammenAdaptor;
45 this.name = StructureElement.ServiceModel.name();
49 public void registerVersioning(String versionableEntityType) {
54 public ToscaServiceModel getServiceModel(String vspId, Version version) {
55 SessionContext context = ZusammenUtil.createSessionContext();
56 Id itemId = new Id(vspId);
57 ElementContext elementContext = new ElementContext(itemId, getFirstVersionId(context, itemId),
58 version.getStatus() == VersionStatus.Locked ? null : version.toString());
60 Optional<ElementInfo> serviceModelElement = zusammenAdaptor
61 .getElementInfoByName(context, elementContext, null, name);
63 if (serviceModelElement.isPresent()) {
64 String entryDefinitionServiceTemplate =
65 serviceModelElement.get().getInfo().getProperty("base");
66 Id serviceModelElementId = serviceModelElement.get().getId();
67 Map<String, ServiceTemplate> serviceTemplates =
68 getTemplates(context, elementContext, serviceModelElementId);
69 if (serviceTemplates == null) {
72 FileContentHandler artifacts = getArtifacts(context, elementContext, serviceModelElementId);
75 return new ToscaServiceModel(
76 artifacts, serviceTemplates, entryDefinitionServiceTemplate);
82 protected Map<String, ServiceTemplate> getTemplates(SessionContext context,
83 ElementContext elementContext,
84 Id serviceModelElementId) {
85 Optional<ElementInfo> templatesElementInfo = zusammenAdaptor.getElementInfoByName(
86 context, elementContext, serviceModelElementId, StructureElement.Templates.name());
88 if (templatesElementInfo.isPresent()) {
89 Collection<Element> elements = zusammenAdaptor.listElementData(context, elementContext,
90 templatesElementInfo.get().getId());
92 return elements.stream().collect(Collectors.toMap(
93 element -> element.getInfo().getName(),
94 this::elementToServiceTemplate));
99 protected FileContentHandler getArtifacts(SessionContext context, ElementContext elementContext,
100 Id serviceModelElementId) {
101 Optional<ElementInfo> artifactsElement = zusammenAdaptor.getElementInfoByName(
102 context, elementContext, serviceModelElementId, StructureElement.Artifacts.name());
104 if (artifactsElement.isPresent()) {
106 Collection<Element> elements = zusammenAdaptor.listElementData(context, elementContext,
107 artifactsElement.get().getId());
108 FileContentHandler fileContentHandler = new FileContentHandler();
109 elements.forEach(element -> fileContentHandler.addFile(element.getInfo().getName(),
111 return fileContentHandler;
118 public void storeServiceModel(String vspId, Version version, ToscaServiceModel serviceModel) {
119 logger.info("Storing service model for vsp id -> " + vspId);
121 ZusammenElement templatesElement =
122 buildStructuralElement(StructureElement.Templates.name(), null);
123 serviceModel.getServiceTemplates().entrySet().forEach(entry -> templatesElement.addSubElement(
124 buildServiceTemplateElement(entry.getKey(), entry.getValue(),
125 serviceModel.getEntryDefinitionServiceTemplate(), Action.CREATE)));
127 ZusammenElement artifactsElement =
128 buildStructuralElement(StructureElement.Artifacts.name(), Action.UPDATE);
129 if (Objects.nonNull(serviceModel.getArtifactFiles())) {
130 serviceModel.getArtifactFiles().getFiles().entrySet().forEach(entry -> artifactsElement
131 .addSubElement(buildArtifactElement(entry.getKey(), entry.getValue(), Action.CREATE)));
134 ZusammenElement serviceModelElement = buildStructuralElement(name, Action.UPDATE);
135 serviceModelElement.getInfo()
136 .addProperty("base", serviceModel.getEntryDefinitionServiceTemplate());
138 serviceModelElement.addSubElement(templatesElement);
139 serviceModelElement.addSubElement(artifactsElement);
141 SessionContext context = ZusammenUtil.createSessionContext();
142 Id itemId = new Id(vspId);
143 ElementContext elementContext = new ElementContext(itemId, getFirstVersionId(context, itemId));
145 .saveElement(context, elementContext, serviceModelElement, "Store service model");
147 logger.info("Finished storing service model for vsp id -> " + vspId);
151 public ServiceElement getServiceModelInfo(String vspId, Version version, String name) {
156 public void deleteAll(String vspId, Version version) {
157 logger.info("started deleting service model for vsp id -> " + vspId);
158 SessionContext context = ZusammenUtil.createSessionContext();
159 Id itemId = new Id(vspId);
160 ElementContext elementContext = new ElementContext(itemId, getFirstVersionId(context, itemId));
162 ZusammenElement zusammenElement = ZusammenUtil.buildStructuralElement(name, Action.DELETE);
163 zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "delete:" + name + ".");
164 logger.info("Finished deleting service model for vsp id -> " + vspId);
167 protected ZusammenElement buildArtifactElement(String name, byte[] artifact, Action action) {
168 ZusammenElement artifactElement = new ZusammenElement();
169 artifactElement.setAction(action);
170 Info info = new Info();
172 info.addProperty("type", ElementType.Artifact.name());
173 artifactElement.setInfo(info);
174 artifactElement.setData(new ByteArrayInputStream(artifact));
176 return artifactElement;
179 private ServiceTemplate elementToServiceTemplate(Element element){
182 String yamlContent = IOUtils.toString(element.getData());
183 return new ToscaExtensionYamlUtil().
184 yamlToObject(yamlContent, ServiceTemplate.class);
185 }catch (Exception e){
186 throw new CoreException(
187 new RetrieveServiceTemplateFromDbErrorBuilder(
188 element.getInfo().getName(), e.getMessage()).build());
192 private Element buildServiceTemplateElement(String name, ServiceTemplate serviceTemplate,
193 String entryDefinitionServiceTemplate,
195 ZusammenElement zusammenElement = new ZusammenElement();
196 zusammenElement.setAction(action);
197 Info info = new Info();
199 info.setDescription(serviceTemplate.getDescription());
200 info.addProperty("type", ElementType.Servicetemplate.name());
201 info.addProperty("base", entryDefinitionServiceTemplate);
202 String yaml = new ToscaExtensionYamlUtil().objectToYaml(serviceTemplate);
203 zusammenElement.setData(new ByteArrayInputStream(yaml.getBytes()));
204 zusammenElement.setInfo(info);
205 return zusammenElement;
208 protected Id getFirstVersionId(SessionContext context, Id vspId) {
209 Optional<ItemVersion> itemVersionOptional = zusammenAdaptor.getFirstVersion(context, vspId);
210 ItemVersion itemVersion = itemVersionOptional.orElseThrow(() ->
211 new RuntimeException(String.format("Vsp %s does not contain any version.", vspId))); //todo
212 return itemVersion.getId();
215 protected ZusammenElement buildStructuralElement(String structureElement, Action action) {
216 return ZusammenUtil.buildStructuralElement(structureElement, action);