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 * Modifications copyright (c) 2019 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.sdc.controller.installer;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonObject;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Optional;
39 import javax.annotation.PostConstruct;
40 import javax.xml.transform.TransformerException;
41 import org.apache.commons.io.IOUtils;
42 import org.json.simple.parser.ParseException;
43 import org.onap.clamp.clds.client.DcaeInventoryServices;
44 import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration;
45 import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration;
46 import org.onap.clamp.clds.dao.CldsDao;
47 import org.onap.clamp.clds.exception.policy.PolicyModelException;
48 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
49 import org.onap.clamp.clds.model.CldsModel;
50 import org.onap.clamp.clds.model.CldsTemplate;
51 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
52 import org.onap.clamp.clds.model.properties.ModelProperties;
53 import org.onap.clamp.clds.service.CldsService;
54 import org.onap.clamp.clds.service.CldsTemplateService;
55 import org.onap.clamp.clds.transform.XslTransformer;
56 import org.onap.clamp.clds.util.JsonUtils;
57 import org.onap.clamp.clds.util.drawing.SvgFacade;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.beans.factory.annotation.Qualifier;
60 import org.springframework.beans.factory.annotation.Value;
61 import org.springframework.context.ApplicationContext;
62 import org.springframework.stereotype.Component;
63 import org.springframework.transaction.annotation.Transactional;
64 import org.yaml.snakeyaml.Yaml;
67 * This class will be instantiated by spring config, and used by Sdc Controller.
68 * There is no state kept by the bean. It's used to deploy the csar/notification
69 * received from SDC in DB.
72 @Qualifier("oldModelInstaller")
73 public class CsarInstallerImpl implements CsarInstaller {
75 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
76 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
77 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-Template-";
78 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
79 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
80 // This will be used later as the policy scope
81 public static final String MODEL_NAME_PREFIX = "CLAMP";
83 * The file name that will be loaded by Spring.
85 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
86 protected String blueprintMappingFile;
87 protected ApplicationContext appContext;
88 private CldsDao cldsDao;
89 CldsTemplateService cldsTemplateService;
90 CldsService cldsService;
91 DcaeInventoryServices dcaeInventoryService;
92 private XslTransformer cldsBpmnTransformer;
95 public CsarInstallerImpl(ApplicationContext appContext,
96 CldsDao cldsDao, CldsTemplateService cldsTemplateService, CldsService cldsService,
97 DcaeInventoryServices dcaeInventoryService, XslTransformer cldsBpmnTransformer) {
98 this.appContext = appContext;
99 this.cldsDao = cldsDao;
100 this.cldsTemplateService = cldsTemplateService;
101 this.cldsService = cldsService;
102 this.dcaeInventoryService = dcaeInventoryService;
103 this.cldsBpmnTransformer = cldsBpmnTransformer;
107 private BlueprintParser blueprintParser;
110 private ChainGenerator chainGenerator;
113 private SvgFacade svgFacade;
116 public void loadConfiguration() throws IOException {
117 BlueprintParserMappingConfiguration
118 .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
119 .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
123 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
124 boolean alreadyInstalled = true;
125 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
126 alreadyInstalled = alreadyInstalled
127 && CldsModel.retrieve(cldsDao, buildModelName(csar, blueprint.getValue()), true).getId() != null;
129 return alreadyInstalled;
132 public static String buildModelName(CsarHandler csar, BlueprintArtifact artifact)
133 throws SdcArtifactInstallerException {
134 String policyScopePrefix = searchForPolicyScopePrefix(artifact);
135 if (policyScopePrefix.contains("*")) {
136 // This is policy_filter type
137 policyScopePrefix = policyScopePrefix.replaceAll("\\*", "");
139 // This is normally the get_input case
140 policyScopePrefix = MODEL_NAME_PREFIX;
142 return (policyScopePrefix + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
143 + csar.getSdcNotification().getServiceVersion() + "_"
144 + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
145 + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
150 public void installTheCsar(CsarHandler csar)
151 throws SdcArtifactInstallerException, InterruptedException, PolicyModelException {
153 logger.info("Installing the CSAR " + csar.getFilePath());
154 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
155 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
156 createFakeCldsModel(csar, blueprint.getValue(),
157 createFakeCldsTemplate(csar, blueprint.getValue(),
158 this.searchForRightMapping(blueprint.getValue())),
159 queryDcaeToGetServiceTypeId(blueprint.getValue()));
161 createPolicyModel(csar);
162 logger.info("Successfully installed the CSAR " + csar.getFilePath());
163 } catch (IOException e) {
164 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
165 } catch (ParseException e) {
166 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
170 BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact)
171 throws SdcArtifactInstallerException {
172 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
173 Yaml yaml = new Yaml();
174 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
175 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
176 bpmnMapping.entrySet().forEach(e -> {
177 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
178 listConfig.add(e.getValue());
181 if (listConfig.size() > 1) {
182 throw new SdcArtifactInstallerException(
183 "The code does not currently support multiple MicroServices in the blueprint");
184 } else if (listConfig.isEmpty()) {
185 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
187 logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
188 + listConfig.get(0).getBpmnXmlFilePath());
189 return listConfig.get(0);
192 String getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact) {
193 JsonObject node = new JsonObject();
194 Yaml yaml = new Yaml();
195 Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
196 .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
197 inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
198 Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
199 if (defaultValue != null) {
200 addPropertyToNode(node, elem.getKey(), defaultValue);
202 node.addProperty(elem.getKey(), "");
205 node.addProperty("policy_id", "AUTO_GENERATED_POLICY_ID_AT_SUBMIT");
206 return node.toString();
209 private void createPolicyModel(CsarHandler csar) throws PolicyModelException {
211 Optional<String> policyModelYaml = csar.getPolicyModelYaml();
212 // save policy model into the database
213 } catch (IOException e) {
214 throw new PolicyModelException("TransformerException when decoding the YamlText", e);
218 private static String searchForPolicyScopePrefix(BlueprintArtifact blueprintArtifact)
219 throws SdcArtifactInstallerException {
220 String policyName = null;
221 Yaml yaml = new Yaml();
222 List<String> policyNameList = new ArrayList<>();
223 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
224 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
225 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy")).forEach(ef -> {
226 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
227 .get("properties")).get("policy_filter");
228 if (policyName != null) {
229 policyNameList.add(filteredPolicyName);
231 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
232 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
233 if (inputPolicyName != null) {
234 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
238 if (policyNameList.size() > 1) {
239 throw new SdcArtifactInstallerException(
240 "The code does not currently support multiple Policy MicroServices in the blueprint");
241 } else if (policyNameList.isEmpty()) {
242 throw new SdcArtifactInstallerException(
243 "There is no recognized Policy MicroService found in the blueprint");
245 logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
246 + policyNameList.get(0));
247 return policyNameList.get(0);
251 * This call must be done when deploying the SDC notification as this call get
252 * the latest version of the artifact (version can be specified to DCAE call)
254 * @return The DcaeInventoryResponse object containing the dcae values
256 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
257 throws IOException, ParseException, InterruptedException {
258 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
259 blueprintArtifact.getBlueprintInvariantServiceUuid(),
260 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
263 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
264 BlueprintParserFilesConfiguration configFiles) throws IOException, SdcArtifactInstallerException {
266 Set<MicroService> microServicesFromBlueprint = blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()) ;
267 List<MicroService> microServicesChain = chainGenerator.getChainOfMicroServices(microServicesFromBlueprint);
268 if(microServicesChain.isEmpty()) {
269 microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
271 String imageText = svgFacade.getSvgImage(microServicesChain);
273 CldsTemplate template = new CldsTemplate();
274 template.setBpmnId("Sdc-Generated");
275 template.setBpmnText(IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream(),
276 StandardCharsets.UTF_8));
277 template.setPropText(
278 "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
280 .setImageText(imageText);
281 template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar, blueprintArtifact));
282 template.save(cldsDao, null);
283 logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
284 + " with name " + template.getName());
288 private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
289 CldsTemplate cldsTemplate, DcaeInventoryResponse dcaeInventoryResponse) throws SdcArtifactInstallerException {
291 if (dcaeInventoryResponse == null) {
292 throw new SdcArtifactInstallerException(
293 "DCAE inventory response is NULL, query to DCAE fail to be answered properly, this is required to deploy CSAR properly !!!");
296 CldsModel cldsModel = new CldsModel();
297 cldsModel.setName(buildModelName(csar, blueprintArtifact));
298 cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
299 cldsModel.setTemplateName(cldsTemplate.getName());
300 cldsModel.setTemplateId(cldsTemplate.getId());
301 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
302 cldsModel.setTypeId(dcaeInventoryResponse.getTypeId());
303 cldsModel.setTypeName(dcaeInventoryResponse.getTypeName());
304 cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
305 // We must save it otherwise object won't be created in db
306 // and proptext will always be null
307 cldsModel.setPropText("{\"global\":[]}");
308 // Must save first to have the generated id available to generate
310 cldsModel = cldsModel.save(cldsDao, null);
311 cldsModel = setModelPropText(cldsModel, blueprintArtifact, cldsTemplate);
312 logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
313 + " with name " + cldsModel.getName());
315 } catch (TransformerException e) {
316 throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);
320 private CldsModel setModelPropText(CldsModel cldsModel, BlueprintArtifact blueprintArtifact,
321 CldsTemplate cldsTemplate) throws TransformerException {
322 // Do a test to validate the BPMN
323 new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), "PUT", false,
324 cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
325 String inputParams = "{\"name\":\"deployParameters\",\"value\":"
326 + getAllBlueprintParametersInJson(blueprintArtifact) + "}";
327 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
328 + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
329 + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
330 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
331 + inputParams + "]}");
332 return cldsModel.save(cldsDao, null);
335 private void addPropertyToNode(JsonObject node, String key, Object value) {
336 if (value instanceof String) {
337 node.addProperty(key, (String) value);
338 } else if (value instanceof Number) {
339 node.addProperty(key, (Number) value);
340 } else if (value instanceof Boolean) {
341 node.addProperty(key, (Boolean) value);
342 } else if (value instanceof Character) {
343 node.addProperty(key, (Character) value);
345 node.addProperty(key, JsonUtils.GSON.toJson(value));