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