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;
35 import java.util.Map.Entry;
37 import javax.annotation.PostConstruct;
39 import org.json.simple.parser.ParseException;
40 import org.onap.clamp.clds.client.DcaeInventoryServices;
41 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
42 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
43 import org.onap.clamp.clds.dao.CldsDao;
44 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
45 import org.onap.clamp.clds.model.CldsModel;
46 import org.onap.clamp.clds.model.CldsTemplate;
47 import org.onap.clamp.clds.service.CldsService;
48 import org.onap.clamp.clds.service.CldsTemplateService;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Value;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.transaction.annotation.Transactional;
53 import org.yaml.snakeyaml.Yaml;
56 * This class will be instantiated by spring config, and used by Sdc Controller.
57 * There is no state kept by the bean. It's used to deploy the csar/notification
58 * received from SDC in DB.
60 public class CsarInstallerImpl implements CsarInstaller {
62 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
63 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
64 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
65 public static final String MODEL_NAME_PREFIX = "ClosedLoop-";
66 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
68 * The file name that will be loaded by Spring.
70 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
71 protected String blueprintMappingFile;
73 protected ApplicationContext appContext;
75 private CldsDao cldsDao;
77 CldsTemplateService cldsTemplateService;
79 CldsService cldsService;
81 DcaeInventoryServices dcaeInventoryService;
84 public void loadConfiguration() throws IOException {
85 BlueprintParserMappingConfiguration
86 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
87 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
91 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
92 return (CldsModel.retrieve(cldsDao, buildModelName(csar), true).getId() != null) ? true : false;
95 public static String buildModelName(CsarHandler csar) {
96 return csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + " v"
97 + csar.getSdcNotification().getServiceVersion();
102 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
104 logger.info("Installing the CSAR " + csar.getFilePath());
105 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
106 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
107 String serviceTypeId = queryDcaeToGetServiceTypeId(blueprint.getValue());
108 createFakeCldsModel(csar, blueprint.getValue(), createFakeCldsTemplate(csar, blueprint.getValue(),
109 this.searchForRightMapping(blueprint.getValue())), serviceTypeId);
111 logger.info("Successfully installed the CSAR " + csar.getFilePath());
112 } catch (IOException e) {
113 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
114 } catch (ParseException | InterruptedException e) {
115 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
119 private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact)
120 throws SdcArtifactInstallerException {
121 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
122 Yaml yaml = new Yaml();
123 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
124 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
125 bpmnMapping.entrySet().forEach(e -> {
126 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
127 listConfig.add(e.getValue());
130 if (listConfig.size() > 1) {
131 throw new SdcArtifactInstallerException(
132 "The code does not currently support multiple MicroServices in the blueprint");
133 } else if (listConfig.isEmpty()) {
134 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
136 logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
137 + listConfig.get(0).getBpmnXmlFilePath());
138 return listConfig.get(0);
141 private String searchForPolicyName(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException {
142 String policyName = null;
143 Yaml yaml = new Yaml();
144 List<String> policyNameList = new ArrayList<>();
145 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
146 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
147 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy_")).forEach(ef -> {
148 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
149 .get("properties")).get("policy_filter");
150 if (policyName != null) {
151 policyNameList.add(filteredPolicyName);
153 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
154 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
155 if (inputPolicyName != null) {
156 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
160 if (policyNameList.size() > 1) {
161 throw new SdcArtifactInstallerException(
162 "The code does not currently support multiple Policy MicroServices in the blueprint");
163 } else if (policyNameList.isEmpty()) {
164 throw new SdcArtifactInstallerException(
165 "There is no recognized Policy MicroService found in the blueprint");
167 logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
168 + policyNameList.get(0));
169 return policyNameList.get(0);
172 private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
173 throws IOException, ParseException, InterruptedException {
174 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
175 blueprintArtifact.getBlueprintInvariantServiceUuid(),
176 blueprintArtifact.getBlueprintInvariantResourceUuid()).getTypeId();
179 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
180 BlueprintParserFilesConfiguration configFiles) throws IOException {
181 CldsTemplate template = new CldsTemplate();
182 template.setBpmnId("Sdc-Generated");
183 template.setBpmnText(
184 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
185 template.setPropText(
186 "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
187 template.setImageText(
188 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
189 template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar));
190 template.save(cldsDao, null);
191 logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
192 + " with name " + template.getName());
196 private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
197 CldsTemplate cldsTemplate, String serviceTypeId) throws SdcArtifactInstallerException {
198 CldsModel cldsModel = new CldsModel();
199 String policyName = searchForPolicyName(blueprintArtifact);
200 if (policyName.contains("*")) {
201 // It's a filter must add a specific prefix
202 cldsModel.setControlNamePrefix(policyName);
204 cldsModel.setControlNamePrefix(MODEL_NAME_PREFIX);
206 cldsModel.setName(buildModelName(csar));
207 cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
208 cldsModel.setTemplateName(cldsTemplate.getName());
209 cldsModel.setTemplateId(cldsTemplate.getId());
210 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
211 + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
212 + blueprintArtifact.getBlueprintInvariantResourceUuid()
213 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},{\"name\":\"deployParameters\",\"value\":{\n"
214 + " \"policy_id\": \"" + "test" + "\"" + " }}]}");
215 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
216 cldsModel.setTypeId(serviceTypeId);
217 cldsModel.save(cldsDao, null);
218 logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
219 + " with name " + cldsModel.getName());