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;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Map.Entry;
38 import javax.annotation.PostConstruct;
39 import javax.xml.transform.TransformerException;
41 import org.json.simple.parser.ParseException;
42 import org.onap.clamp.clds.client.DcaeInventoryServices;
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.dcae.DcaeInventoryResponse;
50 import org.onap.clamp.clds.model.properties.ModelProperties;
51 import org.onap.clamp.clds.service.CldsService;
52 import org.onap.clamp.clds.service.CldsTemplateService;
53 import org.onap.clamp.clds.transform.XslTransformer;
54 import org.onap.clamp.clds.util.JacksonUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.beans.factory.annotation.Value;
57 import org.springframework.context.ApplicationContext;
58 import org.springframework.transaction.annotation.Transactional;
59 import org.yaml.snakeyaml.Yaml;
62 * This class will be instantiated by spring config, and used by Sdc Controller.
63 * There is no state kept by the bean. It's used to deploy the csar/notification
64 * received from SDC in DB.
66 public class CsarInstallerImpl implements CsarInstaller {
68 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
69 private Map<String, BlueprintParserFilesConfiguration> bpmnMapping = new HashMap<>();
70 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-Template-";
71 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
72 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
73 // This will be used later as the policy scope
74 public static final String MODEL_NAME_PREFIX = "CLAMP";
76 * The file name that will be loaded by Spring.
78 @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
79 protected String blueprintMappingFile;
81 protected ApplicationContext appContext;
83 private CldsDao cldsDao;
85 CldsTemplateService cldsTemplateService;
87 CldsService cldsService;
89 DcaeInventoryServices dcaeInventoryService;
91 private XslTransformer cldsBpmnTransformer;
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 throws SdcArtifactInstallerException {
114 String policyScopePrefix = searchForPolicyScopePrefix(csar.getMapOfBlueprints().get(resourceInstanceName));
115 if (policyScopePrefix.contains("*")) {
116 // This is policy_filter type
117 policyScopePrefix = policyScopePrefix.replaceAll("\\*", "");
119 // This is normally the get_input case
120 policyScopePrefix = MODEL_NAME_PREFIX;
122 return policyScopePrefix + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
123 + csar.getSdcNotification().getServiceVersion().replace('.', '_') + "_"
124 + resourceInstanceName.replaceAll(" ", "");
129 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException {
131 logger.info("Installing the CSAR " + csar.getFilePath());
132 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
133 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
134 createFakeCldsModel(csar, blueprint.getValue(),
135 createFakeCldsTemplate(csar, blueprint.getValue(),
136 this.searchForRightMapping(blueprint.getValue())),
137 queryDcaeToGetServiceTypeId(blueprint.getValue()));
139 logger.info("Successfully installed the CSAR " + csar.getFilePath());
140 } catch (IOException e) {
141 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
142 } catch (ParseException e) {
143 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
147 private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact)
148 throws SdcArtifactInstallerException {
149 List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
150 Yaml yaml = new Yaml();
151 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
152 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
153 bpmnMapping.entrySet().forEach(e -> {
154 if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
155 listConfig.add(e.getValue());
158 if (listConfig.size() > 1) {
159 throw new SdcArtifactInstallerException(
160 "The code does not currently support multiple MicroServices in the blueprint");
161 } else if (listConfig.isEmpty()) {
162 throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
164 logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
165 + listConfig.get(0).getBpmnXmlFilePath());
166 return listConfig.get(0);
169 private static String getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact) {
170 ObjectNode node = JacksonUtils.getObjectMapperInstance().createObjectNode();
171 Yaml yaml = new Yaml();
172 Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
173 .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
174 inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
175 Object defaultNode = ((Map<String, Object>) elem.getValue()).get("default");
176 if (defaultNode != null && defaultNode instanceof String) {
177 node.put(elem.getKey(), (String) defaultNode);
178 } else if (defaultNode != null) {
179 node.putPOJO(elem.getKey(), defaultNode);
181 node.put(elem.getKey(), "");
184 node.put("policy_id", "AUTO_GENERATED_POLICY_ID_AT_SUBMIT");
185 return node.toString();
188 private static String searchForPolicyScopePrefix(BlueprintArtifact blueprintArtifact)
189 throws SdcArtifactInstallerException {
190 String policyName = null;
191 Yaml yaml = new Yaml();
192 List<String> policyNameList = new ArrayList<>();
193 Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
194 .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
195 templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy")).forEach(ef -> {
196 String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
197 .get("properties")).get("policy_filter");
198 if (policyName != null) {
199 policyNameList.add(filteredPolicyName);
201 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
202 .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
203 if (inputPolicyName != null) {
204 policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
208 if (policyNameList.size() > 1) {
209 throw new SdcArtifactInstallerException(
210 "The code does not currently support multiple Policy MicroServices in the blueprint");
211 } else if (policyNameList.isEmpty()) {
212 throw new SdcArtifactInstallerException(
213 "There is no recognized Policy MicroService found in the blueprint");
215 logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
216 + policyNameList.get(0));
217 return policyNameList.get(0);
221 * This call must be done when deploying the SDC notification as this call
222 * get the latest version of the artifact (version can be specified to DCAE
225 * @param blueprintArtifact
226 * @return The DcaeInventoryResponse object containing the dcae values
227 * @throws IOException
228 * @throws ParseException
229 * @throws InterruptedException
231 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
232 throws IOException, ParseException, InterruptedException {
233 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
234 blueprintArtifact.getBlueprintInvariantServiceUuid(),
235 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
238 private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
239 BlueprintParserFilesConfiguration configFiles) throws IOException, SdcArtifactInstallerException {
240 CldsTemplate template = new CldsTemplate();
241 template.setBpmnId("Sdc-Generated");
242 template.setBpmnText(
243 IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
244 template.setPropText(
245 "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
246 template.setImageText(
247 IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
248 template.setName(TEMPLATE_NAME_PREFIX
249 + buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName()));
250 template.save(cldsDao, null);
251 logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
252 + " with name " + template.getName());
256 private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
257 CldsTemplate cldsTemplate, DcaeInventoryResponse dcaeInventoryResponse)
258 throws SdcArtifactInstallerException {
260 CldsModel cldsModel = new CldsModel();
261 cldsModel.setName(buildModelName(csar, blueprintArtifact.getResourceAttached().getResourceInstanceName()));
262 cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
263 cldsModel.setTemplateName(cldsTemplate.getName());
264 cldsModel.setTemplateId(cldsTemplate.getId());
265 cldsModel.setBpmnText(cldsTemplate.getBpmnText());
266 cldsModel.setTypeId(dcaeInventoryResponse.getTypeId());
267 cldsModel.setTypeName(dcaeInventoryResponse.getTypeName());
268 cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
269 // We must save it otherwise object won't be created in db
270 // and proptext will always be null
271 cldsModel.setPropText("{\"global\":[]}");
272 // Must save first to have the generated id available to generate
274 cldsModel = cldsModel.save(cldsDao, null);
275 cldsModel = setModelPropText(cldsModel, blueprintArtifact, cldsTemplate);
276 logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
277 + " with name " + cldsModel.getName());
279 } catch (TransformerException e) {
280 throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);
284 private CldsModel setModelPropText(CldsModel cldsModel, BlueprintArtifact blueprintArtifact,
285 CldsTemplate cldsTemplate) throws TransformerException {
286 // Do a test to validate the BPMN
287 new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), "PUT", false,
288 cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
289 String inputParams = "{\"name\":\"deployParameters\",\"value\":"
290 + getAllBlueprintParametersInJson(blueprintArtifact) + "}";
291 cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
292 + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
293 + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
294 + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
295 + inputParams + "]}");
296 return cldsModel.save(cldsDao, null);