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 * ===================================================================
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;
38 import javax.xml.transform.TransformerException;
40 import org.json.simple.parser.ParseException;
41 import org.onap.clamp.clds.client.DcaeInventoryServices;
42 import org.onap.clamp.clds.config.ClampProperties;
43 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
44 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
45 import org.onap.clamp.clds.dao.CldsDao;
46 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
47 import org.onap.clamp.clds.model.CldsModel;
48 import org.onap.clamp.clds.model.CldsTemplate;
49 import org.onap.clamp.clds.model.properties.ModelProperties;
50 import org.onap.clamp.clds.service.CldsService;
51 import org.onap.clamp.clds.service.CldsTemplateService;
52 import org.onap.clamp.clds.transform.XslTransformer;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.beans.factory.annotation.Value;
55 import org.springframework.context.ApplicationContext;
56 import org.springframework.transaction.annotation.Transactional;
57 import org.yaml.snakeyaml.Yaml;
60 * This class will be instantiated by spring config, and used by Sdc Controller.
61 * There is no state kept by the bean. It's used to deploy the csar/notification
62 * received from SDC in DB.
64 public class CsarInstallerImpl implements CsarInstaller {
66 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
67 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
68 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-ClosedLoopTemplate-";
69 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
70 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
71 // This will be used later as the policy scope
72 public static final String MODEL_NAME_PREFIX = "CLAMP";
74 * The file name that will be loaded by Spring.
76 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
77 protected String blueprintMappingFile;
79 protected ApplicationContext appContext;
81 private CldsDao cldsDao;
83 CldsTemplateService cldsTemplateService;
85 CldsService cldsService;
87 DcaeInventoryServices dcaeInventoryService;
89 private XslTransformer cldsBpmnTransformer;
91 private ClampProperties refProp;
94 public void loadConfiguration() throws IOException {
95 BlueprintParserMappingConfiguration
96 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
97 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
101 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
102 boolean alreadyInstalled = true;
103 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
104 alreadyInstalled = alreadyInstalled
105 && (CldsModel.retrieve(cldsDao, buildModelName(csar, blueprint.getKey()), true).getId() != null)
109 return alreadyInstalled;
112 public static String buildModelName(CsarHandler csar, String resourceInstanceName) {
113 return MODEL_NAME_PREFIX + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
114 + csar.getSdcNotification().getServiceVersion().replace('.', '_') + "_" + resourceInstanceName;
119 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException {
121 logger.info("Installing the CSAR " + csar.getFilePath());
122 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
123 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
124 String serviceTypeId = queryDcaeToGetServiceTypeId(blueprint.getValue());
125 createFakeCldsModel(csar, blueprint.getValue(), createFakeCldsTemplate(csar, blueprint.getValue(),
126 this.searchForRightMapping(blueprint.getValue())), serviceTypeId);
128 logger.info("Successfully installed the CSAR " + csar.getFilePath());
129 } catch (IOException e) {
130 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
131 } catch (ParseException | InterruptedException e) {
132 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
136 private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact)
137 throws SdcArtifactInstallerException {
138 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
139 Yaml yaml = new Yaml();
140 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
141 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
142 bpmnMapping.entrySet().forEach(e -> {
143 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
144 listConfig.add(e.getValue());
147 if (listConfig.size() > 1) {
148 throw new SdcArtifactInstallerException(
149 "The code does not currently support multiple MicroServices in the blueprint");
150 } else if (listConfig.isEmpty()) {
151 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
153 logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
154 + listConfig.get(0).getBpmnXmlFilePath());
155 return listConfig.get(0);
158 private String searchForPolicyName(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException {
159 String policyName = null;
160 Yaml yaml = new Yaml();
161 List<String> policyNameList = new ArrayList<>();
162 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
163 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
164 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy")).forEach(ef -> {
165 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
166 .get("properties")).get("policy_filter");
167 if (policyName != null) {
168 policyNameList.add(filteredPolicyName);
170 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
171 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
172 if (inputPolicyName != null) {
173 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
177 if (policyNameList.size() > 1) {
178 throw new SdcArtifactInstallerException(
179 "The code does not currently support multiple Policy MicroServices in the blueprint");
180 } else if (policyNameList.isEmpty()) {
181 throw new SdcArtifactInstallerException(
182 "There is no recognized Policy MicroService found in the blueprint");
184 logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
185 + policyNameList.get(0));
186 return policyNameList.get(0);
189 private String queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
190 throws IOException, ParseException, InterruptedException {
191 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
192 blueprintArtifact.getBlueprintInvariantServiceUuid(),
193 blueprintArtifact.getResourceAttached().getResourceInvariantUUID()).getTypeId();
196 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
197 BlueprintParserFilesConfiguration configFiles) throws IOException {
198 CldsTemplate template = new CldsTemplate();
199 template.setBpmnId("Sdc-Generated");
200 template.setBpmnText(
201 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
202 template.setPropText(
203 "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
204 template.setImageText(
205 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
206 template.setName(TEMPLATE_NAME_PREFIX
207 + buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName()));
208 template.save(cldsDao, null);
209 logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
210 + " with name " + template.getName());
214 private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
215 CldsTemplate cldsTemplate, String serviceTypeId) throws SdcArtifactInstallerException {
217 CldsModel cldsModel = new CldsModel();
218 cldsModel.setName(buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName()));
219 cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
220 cldsModel.setTemplateName(cldsTemplate.getName());
221 cldsModel.setTemplateId(cldsTemplate.getId());
222 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
223 cldsModel.setTypeId(serviceTypeId);
224 ModelProperties modelProp = new ModelProperties(cldsModel.getName(), "test", "PUT", false,
225 cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
226 String policyName = searchForPolicyName(blueprintArtifact);
227 String inputParams = "";
228 if (policyName.contains("*")) {
229 // It's a filter must add a specific prefix
230 cldsModel.setControlNamePrefix(policyName);
232 cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
233 inputParams = "{\"name\":\"deployParameters\",\"value\":{\n" + "\"policy_id\": \""
234 + modelProp.getPolicyNameForDcaeDeploy(refProp) + "\"" + "}}";
236 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
237 + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
238 + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
239 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
240 + inputParams + "]}");
241 cldsModel = cldsModel.save(cldsDao, null);
242 logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
243 + " with name " + cldsModel.getName());
245 } catch (TransformerException e) {
246 throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);