Merge "SDC proxy do not depend on CLAMP"
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / CsarInstallerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.sdc.controller.installer;
25
26 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import javax.annotation.PostConstruct;
37
38 import org.onap.clamp.clds.config.ClampProperties;
39 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
40 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
41 import org.onap.clamp.clds.dao.CldsDao;
42 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
43 import org.onap.clamp.clds.model.CldsModel;
44 import org.onap.clamp.clds.model.CldsTemplate;
45 import org.onap.clamp.clds.service.CldsService;
46 import org.onap.clamp.clds.service.CldsTemplateService;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.beans.factory.annotation.Value;
49 import org.springframework.context.ApplicationContext;
50 import org.springframework.stereotype.Component;
51 import org.yaml.snakeyaml.Yaml;
52
53 @Component
54 public class CsarInstallerImpl implements CsarInstaller {
55
56     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
57     private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
58     public static final String TEMPLATE_NAME_SUFFIX = "-template-dcae-designer";
59     public static final String MODEL_NAME_SUFFIX = "-model-dcae-designer";
60     /**
61      * The file name that will be loaded by Spring.
62      */
63     @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
64     protected String blueprintMappingFile;
65     @Autowired
66     protected ApplicationContext appContext;
67     @Autowired
68     private CldsDao cldsDao;
69     @Autowired
70     private ClampProperties refProp;
71     @Autowired
72     CldsTemplateService cldsTemplateService;
73     @Autowired
74     CldsService cldsService;
75
76     @PostConstruct
77     public void loadConfiguration() throws IOException {
78         BlueprintParserMappingConfiguration
79                 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
80                 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
81     }
82
83     @Override
84     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
85         return false;
86     }
87
88     @Override
89     public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
90         try {
91             BlueprintParserFilesConfiguration configFiles = this.searchForRightMapping(csar);
92             createFakeCldsModel(csar, configFiles, createFakeCldsTemplate(csar, configFiles));
93         } catch (IOException e) {
94             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
95         }
96     }
97
98     private BlueprintParserFilesConfiguration searchForRightMapping(CsarHandler csar)
99             throws SdcArtifactInstallerException {
100         List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
101         Yaml yaml = new Yaml();
102         Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
103                 .load(csar.getDcaeBlueprint())).get("node_templates"));
104         bpmnMapping.entrySet().forEach(e -> {
105             if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
106                 listConfig.add(e.getValue());
107             }
108         });
109         if (listConfig.size() > 1) {
110             throw new SdcArtifactInstallerException(
111                     "The code does not currently support multiple MicroServices in the blueprint");
112         } else if (listConfig.isEmpty()) {
113             throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
114         }
115         return listConfig.get(0);
116     }
117
118     private String createTemplateName(CsarHandler csar) {
119         return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + TEMPLATE_NAME_SUFFIX;
120     }
121
122     private String createModelName(CsarHandler csar) {
123         return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + MODEL_NAME_SUFFIX;
124     }
125
126     private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles)
127             throws IOException, SdcArtifactInstallerException {
128         CldsTemplate template = new CldsTemplate();
129         template.setBpmnId("Sdc-Generated");
130         template.setBpmnText(
131                 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
132         // ((ObjectNode)refProp.getJsonTemplate(CldsService.GLOBAL_PROPERTIES_KEY));
133         template.setPropText(csar.getDcaeBlueprint());
134         template.setImageText(
135                 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
136         return cldsTemplateService.putTemplate(createTemplateName(csar), template);
137     }
138
139     private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintParserFilesConfiguration configFiles,
140             CldsTemplate cldsTemplate) {
141         CldsModel cldsModel = new CldsModel();
142         cldsModel.setBlueprintText(csar.getDcaeBlueprint());
143         cldsModel.setTemplateName(cldsTemplate.getName());
144         // cldsModel.set
145         return cldsService.putModel(createModelName(csar), cldsModel);
146     }
147 }