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;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import javax.annotation.PostConstruct;
36 import org.json.simple.parser.ParseException;
37 import org.onap.clamp.clds.client.DcaeInventoryServices;
38 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
39 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
40 import org.onap.clamp.clds.dao.CldsDao;
41 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
42 import org.onap.clamp.clds.model.CldsModel;
43 import org.onap.clamp.clds.model.CldsTemplate;
44 import org.onap.clamp.clds.service.CldsService;
45 import org.onap.clamp.clds.service.CldsTemplateService;
46 import org.springframework.transaction.annotation.Transactional;
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.yaml.snakeyaml.Yaml;
53 * This class will be instantiated by spring config, and used by Sdc Controller.
54 * There is no state kept by the bean. It's used to deploy the csar/notification
55 * received from SDC in DB.
57 public class CsarInstallerImpl implements CsarInstaller {
59 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
60 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
61 public static final String MODEL_NAME_PREFIX = "ClosedLoop-";
62 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
64 * The file name that will be loaded by Spring.
66 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
67 protected String blueprintMappingFile;
69 protected ApplicationContext appContext;
71 private CldsDao cldsDao;
73 CldsTemplateService cldsTemplateService;
75 CldsService cldsService;
77 DcaeInventoryServices dcaeInventoryService;
80 public void loadConfiguration() throws IOException {
81 BlueprintParserMappingConfiguration
82 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
83 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
87 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
88 return (CldsModel.retrieve(cldsDao, csar.getSdcCsarHelper().getServiceMetadata().getValue("name"), true)
89 .getId() != null) ? true : false;
94 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
96 String serviceTypeId = queryDcaeToGetServiceTypeId(csar);
97 createFakeCldsModel(csar, createFakeCldsTemplate(csar, this.searchForRightMapping(csar)), serviceTypeId);
98 } catch (IOException e) {
99 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
100 } catch (ParseException e) {
101 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
105 private BlueprintParserFilesConfiguration searchForRightMapping(CsarHandler csar)
106 throws SdcArtifactInstallerException {
107 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
108 Yaml yaml = new Yaml();
109 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
110 .load(csar.getDcaeBlueprint())).get("node_templates"));
111 bpmnMapping.entrySet().forEach(e -> {
112 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
113 listConfig.add(e.getValue());
116 if (listConfig.size() > 1) {
117 throw new SdcArtifactInstallerException(
118 "The code does not currently support multiple MicroServices in the blueprint");
119 } else if (listConfig.isEmpty()) {
120 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
122 return listConfig.get(0);
125 private String searchForPolicyName(CsarHandler csar) throws SdcArtifactInstallerException {
126 String policyName = null;
127 Yaml yaml = new Yaml();
128 List<String> policyNameList = new ArrayList<>();
129 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
130 .load(csar.getDcaeBlueprint())).get("node_templates"));
131 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy_")).forEach(ef -> {
132 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
133 .get("properties")).get("policy_filter");
134 if (policyName != null) {
135 policyNameList.add(filteredPolicyName);
137 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
138 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
139 if (inputPolicyName != null) {
140 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
144 if (policyNameList.size() > 1) {
145 throw new SdcArtifactInstallerException(
146 "The code does not currently support multiple Policy MicroServices in the blueprint");
147 } else if (policyNameList.isEmpty()) {
148 throw new SdcArtifactInstallerException(
149 "There is no recognized Policy MicroService found in the blueprint");
151 return policyNameList.get(0);
154 private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException {
155 return dcaeInventoryService.getDcaeInformation(csar.getBlueprintArtifactName(),
156 csar.getBlueprintInvariantServiceUuid(), csar.getBlueprintInvariantResourceUuid()).getTypeId();
159 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles)
161 CldsTemplate template = new CldsTemplate();
162 template.setBpmnId("Sdc-Generated");
163 template.setBpmnText(
164 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
165 template.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\"" + csar.getDcaeBlueprint() + "\"]}]}");
166 template.setImageText(
167 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
168 template.setName(TEMPLATE_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
169 template.save(cldsDao, null);
173 private CldsModel createFakeCldsModel(CsarHandler csar, CldsTemplate cldsTemplate, String serviceTypeId)
174 throws SdcArtifactInstallerException {
175 CldsModel cldsModel = new CldsModel();
176 String policyName = searchForPolicyName(csar);
177 if (policyName.contains("*")) {
178 // It's a filter must add a specific prefix
179 cldsModel.setControlNamePrefix(policyName);
181 cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
183 cldsModel.setName(csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
184 cldsModel.setBlueprintText(csar.getDcaeBlueprint());
185 cldsModel.setTemplateName(cldsTemplate.getName());
186 cldsModel.setTemplateId(cldsTemplate.getId());
187 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
188 + csar.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
189 + csar.getBlueprintInvariantResourceUuid()
190 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},{\"name\":\"deployParameters\",\"value\":{\n"
191 + " \"policy_id\": \"" + "test" + "\"" + " }}]}");
192 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
193 cldsModel.setTypeId(serviceTypeId);
194 cldsModel.save(cldsDao, null);