push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-model-lib / openecomp-sdc-model-core / src / main / java / org / openecomp / sdc / model / impl / AbstractServiceModelDao.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.model.impl;
22
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;
34
35 import java.io.InputStream;
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.stream.Collectors;
40
41 public class AbstractServiceModelDao implements VersionableDao {
42
43   protected ServiceTemplateDaoInter templateDao;
44   protected ServiceArtifactDaoInter artifactDao;
45
46   @Override
47   public void registerVersioning(String versionableEntityType) {
48     templateDao.registerVersioning(versionableEntityType);
49     artifactDao.registerVersioning(versionableEntityType);
50   }
51
52
53   /**
54    * Gets service model.
55    *
56    * @param vspId   the vsp id
57    * @param version the version
58    * @return the service model
59    */
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");
64     }
65
66
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);
72   }
73
74
75   public void storeExternalArtifact(ServiceArtifact serviceArtifact) {
76     artifactDao.create(serviceArtifact);
77     //TODO: update last modification time
78   }
79
80
81   /**
82    * Store service model.
83    *
84    * @param vspId             the vsp id
85    * @param version           the version
86    * @param toscaServiceModel the tosca service model
87    */
88   public void storeServiceModel(String vspId, Version version,
89                                 ToscaServiceModel toscaServiceModel) {
90     ServiceArtifact entityArt;
91
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);
99
100       artifactDao.create(entityArt);
101     }
102
103     ServiceTemplate entityTmp;
104     String yaml;
105     for (Map.Entry<String, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate>
106             entryTemplate : toscaServiceModel
107         .getServiceTemplates().entrySet()) {
108       entityTmp = new ServiceTemplate();
109
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());
116
117       templateDao.create(entityTmp);
118     }
119
120     //TODO: update last modification time
121   }
122
123
124   /**
125    * Gets service model info.
126    *
127    * @param vspId   the vsp id
128    * @param version the version
129    * @param name    the name
130    * @return the service model info
131    */
132   public ServiceElement getServiceModelInfo(String vspId, Version version, String name) {
133     ServiceElement element = templateDao.getTemplateInfo(vspId, version, name);
134     if (element != null) {
135       return element;
136     }
137
138     element = artifactDao.getArtifactInfo(vspId, version, name);
139     if (element != null) {
140       return element;
141     }
142     return null;
143   }
144
145
146   /**
147    * Gets service model content names.
148    *
149    * @return the service model content names
150    */
151   public List<String> getServiceModelContentNames() {
152
153
154     return null;
155   }
156
157
158   private String getServiceBase(String vspId, Version version) {
159     return templateDao.getBase(vspId, version);
160   }
161
162   private Map<String, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate> getTemplates(
163       String vspId, Version version) {
164
165     Collection<ServiceTemplate> templates = templateDao.list(vspId, version);
166     if (templates == null) {
167       return null;
168     }
169     return templates.stream().collect(Collectors.toMap(template -> template.getName(),
170         template -> getServiceTemplate(template.getContent())));
171   }
172
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);
177   }
178
179   private FileContentHandler getArtifacts(String vspId, Version version) {
180     Collection<ServiceArtifact> templates = artifactDao.list(vspId, version);
181     if (templates == null) {
182       return null;
183     }
184
185     FileContentHandler fileContentHandler = new FileContentHandler();
186     templates.stream().forEach(serviceArtifact -> fileContentHandler
187         .addFile(serviceArtifact.getName(), serviceArtifact.getContent()));
188
189     return fileContentHandler;
190   }
191 }