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.json.simple.parser.ParseException;
39 import org.onap.clamp.clds.client.DcaeInventoryServices;
40 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
41 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
42 import org.onap.clamp.clds.dao.CldsDao;
43 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
44 import org.onap.clamp.clds.model.CldsModel;
45 import org.onap.clamp.clds.model.CldsTemplate;
46 import org.onap.clamp.clds.service.CldsService;
47 import org.onap.clamp.clds.service.CldsTemplateService;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.context.ApplicationContext;
51 import org.springframework.stereotype.Component;
52 import org.yaml.snakeyaml.Yaml;
55 public class CsarInstallerImpl implements CsarInstaller {
57 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
58 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
59 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
60 public static final String MODEL_NAME_PREFIX = "DCAE-Designer-ClosedLoopInstance-";
62 * The file name that will be loaded by Spring.
64 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
65 protected String blueprintMappingFile;
67 protected ApplicationContext appContext;
69 private CldsDao cldsDao;
71 CldsTemplateService cldsTemplateService;
73 CldsService cldsService;
75 DcaeInventoryServices dcaeInventoryService;
78 public void loadConfiguration() throws IOException {
79 BlueprintParserMappingConfiguration
80 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
81 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
85 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
90 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
92 String serviceTypeId = queryDcaeToGetServiceTypeId(csar);
93 String policyName = searchForPolicyName(csar);
94 if (policyName.contains("*")) {
96 createFakeCldsModel(csar, createFakeCldsTemplate(csar, this.searchForRightMapping(csar)), serviceTypeId);
97 } catch (IOException e) {
98 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
99 } catch (ParseException e) {
100 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
104 private BlueprintParserFilesConfiguration searchForRightMapping(CsarHandler csar)
105 throws SdcArtifactInstallerException {
106 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
107 Yaml yaml = new Yaml();
108 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
109 .load(csar.getDcaeBlueprint())).get("node_templates"));
110 bpmnMapping.entrySet().forEach(e -> {
111 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
112 listConfig.add(e.getValue());
115 if (listConfig.size() > 1) {
116 throw new SdcArtifactInstallerException(
117 "The code does not currently support multiple MicroServices in the blueprint");
118 } else if (listConfig.isEmpty()) {
119 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
121 return listConfig.get(0);
124 private String searchForPolicyName(CsarHandler csar) throws SdcArtifactInstallerException {
125 String policyName = null;
126 Yaml yaml = new Yaml();
127 List<String> policyNameList = new ArrayList<>();
128 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
129 .load(csar.getDcaeBlueprint())).get("node_templates"));
130 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy_")).forEach(ef -> {
131 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
132 .get("properties")).get("policy_filter");
133 if (policyName != null) {
134 policyNameList.add(filteredPolicyName);
136 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
137 .getValue()).get("properties")).get("policy_id")).get("get_input");
138 if (inputPolicyName != null) {
139 policyNameList.add("get_input");
143 if (policyNameList.size() > 1) {
144 throw new SdcArtifactInstallerException(
145 "The code does not currently support multiple Policy MicroServices in the blueprint");
146 } else if (policyNameList.isEmpty()) {
147 throw new SdcArtifactInstallerException(
148 "There is no recognized Policy MicroService found in the blueprint");
150 return policyNameList.get(0);
153 private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException {
154 return dcaeInventoryService.getDcaeInformation(csar.getBlueprintArtifactName(),
155 csar.getBlueprintInvariantServiceUuid(), csar.getBlueprintInvariantResourceUuid());
158 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles)
159 throws IOException, SdcArtifactInstallerException {
160 CldsTemplate template = new CldsTemplate();
161 template.setBpmnId("Sdc-Generated");
162 template.setBpmnText(
163 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
164 template.setPropText(csar.getDcaeBlueprint());
165 template.setImageText(
166 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
167 template.setName(TEMPLATE_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
168 template.save(cldsDao, null);
172 private CldsModel createFakeCldsModel(CsarHandler csar, CldsTemplate cldsTemplate, String serviceTypeId) {
173 CldsModel cldsModel = new CldsModel();
174 cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
175 cldsModel.setName(csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
176 cldsModel.setBlueprintText(csar.getDcaeBlueprint());
177 cldsModel.setTemplateName(cldsTemplate.getName());
178 cldsModel.setTemplateId(cldsTemplate.getId());
179 cldsModel.setDocText(cldsTemplate.getPropText());
180 cldsModel.setPropText("{}");
181 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
182 cldsModel.setTypeId(serviceTypeId);
183 cldsModel.save(cldsDao, null);