2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.sdc.controller.installer;
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;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
36 import javax.annotation.PostConstruct;
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;
54 public class CsarInstallerImpl implements CsarInstaller {
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";
61 * The file name that will be loaded by Spring.
63 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
64 protected String blueprintMappingFile;
66 protected ApplicationContext appContext;
68 private CldsDao cldsDao;
70 private ClampProperties refProp;
72 CldsTemplateService cldsTemplateService;
74 CldsService cldsService;
77 public void loadConfiguration() throws IOException {
78 BlueprintParserMappingConfiguration
79 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
80 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
84 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
89 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
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);
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());
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");
115 return listConfig.get(0);
118 private String createTemplateName(CsarHandler csar) {
119 return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + TEMPLATE_NAME_SUFFIX;
122 private String createModelName(CsarHandler csar) {
123 return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + MODEL_NAME_SUFFIX;
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);
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());
145 return cldsService.putModel(createModelName(csar), cldsModel);