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>)
232 ef.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(
267 blueprintArtifact.getDcaeBlueprint()) ;
268 List<MicroService> microServicesChain = chainGenerator.getChainOfMicroServices(microServicesFromBlueprint);
269 if (microServicesChain.isEmpty()) {
270 microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
272 String imageText = svgFacade.getSvgImage(microServicesChain);
274 CldsTemplate template = new CldsTemplate();
275 template.setBpmnId("Sdc-Generated");
276 template.setBpmnText(IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream(),
277 StandardCharsets.UTF_8));
278 template.setPropText(
279 "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
281 .setImageText(imageText);
282 template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar, blueprintArtifact));
283 template.save(cldsDao, null);
284 logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
285 + " with name " + template.getName());
289 private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
290 CldsTemplate cldsTemplate, DcaeInventoryResponse dcaeInventoryResponse) throws SdcArtifactInstallerException {
292 if (dcaeInventoryResponse == null) {
293 throw new SdcArtifactInstallerException(
294 "DCAE inventory response is NULL, query to DCAE fail to be answered properly, "
295 + "this is required to deploy CSAR properly !!!");
298 CldsModel cldsModel = new CldsModel();
299 cldsModel.setName(buildModelName(csar, blueprintArtifact));
300 cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
301 cldsModel.setTemplateName(cldsTemplate.getName());
302 cldsModel.setTemplateId(cldsTemplate.getId());
303 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
304 cldsModel.setTypeId(dcaeInventoryResponse.getTypeId());
305 cldsModel.setTypeName(dcaeInventoryResponse.getTypeName());
306 cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
307 // We must save it otherwise object won't be created in db
308 // and proptext will always be null
309 cldsModel.setPropText("{\"global\":[]}");
310 // Must save first to have the generated id available to generate
312 cldsModel = cldsModel.save(cldsDao, null);
313 cldsModel = setModelPropText(cldsModel, blueprintArtifact, cldsTemplate);
314 logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
315 + " with name " + cldsModel.getName());
317 } catch (TransformerException e) {
318 throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);
322 private CldsModel setModelPropText(CldsModel cldsModel, BlueprintArtifact blueprintArtifact,
323 CldsTemplate cldsTemplate) throws TransformerException {
324 // Do a test to validate the BPMN
325 new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), "PUT", false,
326 cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
327 String inputParams = "{\"name\":\"deployParameters\",\"value\":"
328 + getAllBlueprintParametersInJson(blueprintArtifact) + "}";
329 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
330 + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
331 + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
332 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
333 + inputParams + "]}");
334 return cldsModel.save(cldsDao, null);
337 private void addPropertyToNode(JsonObject node, String key, Object value) {
338 if (value instanceof String) {
339 node.addProperty(key, (String) value);
340 } else if (value instanceof Number) {
341 node.addProperty(key, (Number) value);
342 } else if (value instanceof Boolean) {
343 node.addProperty(key, (Boolean) value);
344 } else if (value instanceof Character) {
345 node.addProperty(key, (Character) value);
347 node.addProperty(key, JsonUtils.GSON.toJson(value));