2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.model.impl;
23 import org.openecomp.core.model.dao.ServiceArtifactDaoInter;
24 import org.openecomp.core.model.dao.ServiceTemplateDaoInter;
25 import org.openecomp.core.model.types.ServiceArtifact;
26 import org.openecomp.core.model.types.ServiceElement;
27 import org.openecomp.core.model.types.ServiceTemplate;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.utilities.file.FileUtils;
30 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
31 import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil;
32 import org.openecomp.sdc.versioning.dao.VersionableDao;
33 import org.openecomp.sdc.versioning.dao.types.Version;
35 import java.io.InputStream;
36 import java.util.Collection;
37 import java.util.List;
39 import java.util.stream.Collectors;
41 public class AbstractServiceModelDao implements VersionableDao {
43 protected ServiceTemplateDaoInter templateDao;
44 protected ServiceArtifactDaoInter artifactDao;
47 public void registerVersioning(String versionableEntityType) {
48 templateDao.registerVersioning(versionableEntityType);
49 artifactDao.registerVersioning(versionableEntityType);
56 * @param vspId the vsp id
57 * @param version the version
58 * @return the service model
60 public ToscaServiceModel getServiceModel(String vspId, Version version) {
61 if (vspId == null || version == null) {
62 //throw new CoreException()
63 throw new RuntimeException("missing service model key");
67 FileContentHandler artifactFiles = getArtifacts(vspId, version);
68 Map<String, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate> serviceTemplates =
69 getTemplates(vspId, version);
70 String entryDefinitionServiceTemplate = getServiceBase(vspId, version);
71 return new ToscaServiceModel(artifactFiles, serviceTemplates, entryDefinitionServiceTemplate);
75 public void storeExternalArtifact(ServiceArtifact serviceArtifact) {
76 artifactDao.create(serviceArtifact);
77 //TODO: update last modification time
82 * Store service model.
84 * @param vspId the vsp id
85 * @param version the version
86 * @param toscaServiceModel the tosca service model
88 public void storeServiceModel(String vspId, Version version,
89 ToscaServiceModel toscaServiceModel) {
90 ServiceArtifact entityArt;
92 for (String fileName : toscaServiceModel.getArtifactFiles().getFileList()) {
93 entityArt = new ServiceArtifact();
94 entityArt.setContentData(
95 FileUtils.toByteArray(toscaServiceModel.getArtifactFiles().getFileContent(fileName)));
96 entityArt.setVspId(vspId);
97 entityArt.setVersion(version);
98 entityArt.setName(fileName);
100 artifactDao.create(entityArt);
103 ServiceTemplate entityTmp;
105 for (Map.Entry<String, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate>
106 entryTemplate : toscaServiceModel
107 .getServiceTemplates().entrySet()) {
108 entityTmp = new ServiceTemplate();
110 yaml = new ToscaExtensionYamlUtil().objectToYaml(entryTemplate.getValue());
111 entityTmp.setContentData(yaml.getBytes());
112 entityTmp.setVspId(vspId);
113 entityTmp.setVersion(version);
114 entityTmp.setName(entryTemplate.getKey());
115 entityTmp.setBaseName(toscaServiceModel.getEntryDefinitionServiceTemplate());
117 templateDao.create(entityTmp);
120 //TODO: update last modification time
125 * Gets service model info.
127 * @param vspId the vsp id
128 * @param version the version
129 * @param name the name
130 * @return the service model info
132 public ServiceElement getServiceModelInfo(String vspId, Version version, String name) {
133 ServiceElement element = templateDao.getTemplateInfo(vspId, version, name);
134 if (element != null) {
138 element = artifactDao.getArtifactInfo(vspId, version, name);
139 if (element != null) {
147 * Gets service model content names.
149 * @return the service model content names
151 public List<String> getServiceModelContentNames() {
158 private String getServiceBase(String vspId, Version version) {
159 return templateDao.getBase(vspId, version);
162 private Map<String, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate> getTemplates(
163 String vspId, Version version) {
165 Collection<ServiceTemplate> templates = templateDao.list(vspId, version);
166 if (templates == null) {
169 return templates.stream().collect(Collectors.toMap(template -> template.getName(),
170 template -> getServiceTemplate(template.getContent())));
173 private org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate getServiceTemplate(
174 InputStream content) {
175 return new ToscaExtensionYamlUtil()
176 .yamlToObject(content, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate.class);
179 private FileContentHandler getArtifacts(String vspId, Version version) {
180 Collection<ServiceArtifact> templates = artifactDao.list(vspId, version);
181 if (templates == null) {
185 FileContentHandler fileContentHandler = new FileContentHandler();
186 templates.stream().forEach(serviceArtifact -> fileContentHandler
187 .addFile(serviceArtifact.getName(), serviceArtifact.getContent()));
189 return fileContentHandler;