fcc6f0ab3110ca1579431242e06441126e1858ac
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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.core.impl;
18
19 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
20 import org.openecomp.core.converter.ServiceTemplateReaderService;
21 import org.openecomp.core.converter.datatypes.CsarFileTypes;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
29 import static org.openecomp.core.converter.datatypes.Constants.mainStName;
30
31 public class ToscaConverterImpl extends AbstractToscaConverter {
32
33   @Override
34   public ToscaServiceModel convert(FileContentHandler fileContentHandler) {
35       Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
36       ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
37       Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
38       FileContentHandler artifacts = new FileContentHandler();
39       GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
40       csarFiles.putAll(fileContentHandler.getFiles());
41       for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
42           CsarFileTypes fileType = getFileType(fileEntry.getKey());
43           switch (fileType) {
44               case mainServiceTemplate:
45                   handleServiceTemplate(mainStName, fileEntry.getKey(), csarFiles, serviceTemplates);
46                   break;
47
48               case globalServiceTemplate:
49                   handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
50                   break;
51
52               case externalFile:
53                   artifacts.addFile(
54                           getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
55                   break;
56
57               case definitionsFile:
58                   handleDefinitionTemplate(fileEntry.getKey(), csarFiles, gsst);
59                   break;
60
61               default:
62                   break;
63           }
64       }
65       handleMetadataFile(csarFiles);
66       updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, mainStName);
67       return toscaServiceModel;
68     }
69
70     @Override
71     public void convertTopologyTemplate(ServiceTemplate serviceTemplate, ServiceTemplateReaderService readerService) {
72         new VnfTopologyTemplateConverter().convertTopologyTemplate(serviceTemplate, readerService);
73     }
74 }