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.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.context.ApplicationContext;
49 import org.yaml.snakeyaml.Yaml;
52 * This class will be instantiated by spring config, and used by Sdc Controller.
53 * There is no state kept by the bean. It's used to deploy the csar/notification
54 * received from SDC in DB.
56 public class CsarInstallerImpl implements CsarInstaller {
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 = "ClosedLoop-";
61 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
63 * The file name that will be loaded by Spring.
65 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
66 protected String blueprintMappingFile;
68 protected ApplicationContext appContext;
70 private CldsDao cldsDao;
72 CldsTemplateService cldsTemplateService;
74 CldsService cldsService;
76 DcaeInventoryServices dcaeInventoryService;
79 public void loadConfiguration() throws IOException {
80 BlueprintParserMappingConfiguration
81 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
82 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
86 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
87 return (CldsModel.retrieve(cldsDao, csar.getSdcCsarHelper().getServiceMetadata().getValue("name"), true)
88 .getId() != null) ? true : false;
92 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
94 String serviceTypeId = queryDcaeToGetServiceTypeId(csar);
95 createFakeCldsModel(csar, createFakeCldsTemplate(csar, this.searchForRightMapping(csar)), serviceTypeId);
96 } catch (IOException e) {
97 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
98 } catch (ParseException e) {
99 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
103 private BlueprintParserFilesConfiguration searchForRightMapping(CsarHandler csar)
104 throws SdcArtifactInstallerException {
105 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
106 Yaml yaml = new Yaml();
107 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
108 .load(csar.getDcaeBlueprint())).get("node_templates"));
109 bpmnMapping.entrySet().forEach(e -> {
110 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
111 listConfig.add(e.getValue());
114 if (listConfig.size() > 1) {
115 throw new SdcArtifactInstallerException(
116 "The code does not currently support multiple MicroServices in the blueprint");
117 } else if (listConfig.isEmpty()) {
118 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
120 return listConfig.get(0);
123 private String searchForPolicyName(CsarHandler csar) throws SdcArtifactInstallerException {
124 String policyName = null;
125 Yaml yaml = new Yaml();
126 List<String> policyNameList = new ArrayList<>();
127 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
128 .load(csar.getDcaeBlueprint())).get("node_templates"));
129 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy_")).forEach(ef -> {
130 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
131 .get("properties")).get("policy_filter");
132 if (policyName != null) {
133 policyNameList.add(filteredPolicyName);
135 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
136 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
137 if (inputPolicyName != null) {
138 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
142 if (policyNameList.size() > 1) {
143 throw new SdcArtifactInstallerException(
144 "The code does not currently support multiple Policy MicroServices in the blueprint");
145 } else if (policyNameList.isEmpty()) {
146 throw new SdcArtifactInstallerException(
147 "There is no recognized Policy MicroService found in the blueprint");
149 return policyNameList.get(0);
152 private String queryDcaeToGetServiceTypeId(CsarHandler csar) throws IOException, ParseException {
153 return dcaeInventoryService.getDcaeInformation(csar.getBlueprintArtifactName(),
154 csar.getBlueprintInvariantServiceUuid(), csar.getBlueprintInvariantResourceUuid()).getTypeId();
157 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintParserFilesConfiguration configFiles)
159 CldsTemplate template = new CldsTemplate();
160 template.setBpmnId("Sdc-Generated");
161 template.setBpmnText(
162 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
163 template.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\"" + csar.getDcaeBlueprint() + "\"]}]}");
164 template.setImageText(
165 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
166 template.setName(TEMPLATE_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
167 template.save(cldsDao, null);
171 private CldsModel createFakeCldsModel(CsarHandler csar, CldsTemplate cldsTemplate, String serviceTypeId)
172 throws SdcArtifactInstallerException {
173 CldsModel cldsModel = new CldsModel();
174 String policyName = searchForPolicyName(csar);
175 if (policyName.contains("*")) {
176 // It's a filter must add a specific prefix
177 cldsModel.setControlNamePrefix(policyName);
179 cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
181 cldsModel.setName(csar.getSdcCsarHelper().getServiceMetadata().getValue("name"));
182 cldsModel.setBlueprintText(csar.getDcaeBlueprint());
183 cldsModel.setTemplateName(cldsTemplate.getName());
184 cldsModel.setTemplateId(cldsTemplate.getId());
185 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
186 + csar.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
187 + csar.getBlueprintInvariantResourceUuid()
188 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},{\"name\":\"deployParameters\",\"value\":{\n"
189 + " \"policy_id\": \"" + "test" + "\"" + " }}]}");
190 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
191 cldsModel.setTypeId(serviceTypeId);
192 cldsModel.save(cldsDao, null);