Merge "Remove SDC query"
[clamp.git] / src / main / java / org / onap / clamp / clds / sdc / controller / installer / CsarInstallerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *
22  */
23
24 package org.onap.clamp.clds.sdc.controller.installer;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 import javax.annotation.PostConstruct;
38 import javax.xml.transform.TransformerException;
39
40 import org.apache.commons.io.IOUtils;
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.JsonUtils;
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;
60
61 /**
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.
65  */
66 public class CsarInstallerImpl implements CsarInstaller {
67
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";
75     /**
76      * The file name that will be loaded by Spring.
77      */
78     @Value("${clamp.config.sdc.blueprint.parser.mapping:'classpath:/clds/blueprint-parser-mapping.json'}")
79     protected String blueprintMappingFile;
80     @Autowired
81     protected ApplicationContext appContext;
82     @Autowired
83     private CldsDao cldsDao;
84     @Autowired
85     CldsTemplateService cldsTemplateService;
86     @Autowired
87     CldsService cldsService;
88     @Autowired
89     DcaeInventoryServices dcaeInventoryService;
90     @Autowired
91     private XslTransformer cldsBpmnTransformer;
92
93     @PostConstruct
94     public void loadConfiguration() throws IOException {
95         BlueprintParserMappingConfiguration
96             .createFromJson(appContext.getResource(blueprintMappingFile).getInputStream()).stream()
97             .forEach(e -> bpmnMapping.put(e.getBlueprintKey(), e.getFiles()));
98     }
99
100     @Override
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.getValue()), true).getId() != null;
106         }
107         return alreadyInstalled;
108     }
109
110     public static String buildModelName(CsarHandler csar, BlueprintArtifact artifact)
111         throws SdcArtifactInstallerException {
112         String policyScopePrefix = searchForPolicyScopePrefix(artifact);
113         if (policyScopePrefix.contains("*")) {
114             // This is policy_filter type
115             policyScopePrefix = policyScopePrefix.replaceAll("\\*", "");
116         } else {
117             // This is normally the get_input case
118             policyScopePrefix = MODEL_NAME_PREFIX;
119         }
120         return (policyScopePrefix + "_" + csar.getSdcCsarHelper().getServiceMetadata().getValue("name") + "_v"
121             + csar.getSdcNotification().getServiceVersion() + "_"
122             + artifact.getResourceAttached().getResourceInstanceName().replaceAll(" ", "") + "_"
123             + artifact.getBlueprintArtifactName().replace(".yaml", "")).replace('.', '_');
124     }
125
126     @Override
127     @Transactional
128     public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException {
129         try {
130             logger.info("Installing the CSAR " + csar.getFilePath());
131             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
132                 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
133                 createFakeCldsModel(csar, blueprint.getValue(),
134                     createFakeCldsTemplate(csar, blueprint.getValue(),
135                         this.searchForRightMapping(blueprint.getValue())),
136                     queryDcaeToGetServiceTypeId(blueprint.getValue()));
137             }
138             logger.info("Successfully installed the CSAR " + csar.getFilePath());
139         } catch (IOException e) {
140             throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
141         } catch (ParseException e) {
142             throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
143         }
144     }
145
146     private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact)
147         throws SdcArtifactInstallerException {
148         List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>();
149         Yaml yaml = new Yaml();
150         Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
151             .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
152         bpmnMapping.entrySet().forEach(e -> {
153             if (templateNodes.keySet().stream().anyMatch(t -> t.contains(e.getKey()))) {
154                 listConfig.add(e.getValue());
155             }
156         });
157         if (listConfig.size() > 1) {
158             throw new SdcArtifactInstallerException(
159                 "The code does not currently support multiple MicroServices in the blueprint");
160         } else if (listConfig.isEmpty()) {
161             throw new SdcArtifactInstallerException("There is no recognized MicroService found in the blueprint");
162         }
163         logger.info("Mapping found for blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
164             + listConfig.get(0).getBpmnXmlFilePath());
165         return listConfig.get(0);
166     }
167
168     String getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact) {
169         JsonObject node = new JsonObject();
170         Yaml yaml = new Yaml();
171         Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
172             .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
173         inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
174             Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
175             if (defaultValue != null) {
176                 addPropertyToNode(node, elem.getKey(), defaultValue);
177             } else {
178                 node.addProperty(elem.getKey(), "");
179             }
180         });
181         node.addProperty("policy_id", "AUTO_GENERATED_POLICY_ID_AT_SUBMIT");
182         return node.toString();
183     }
184
185     private static String searchForPolicyScopePrefix(BlueprintArtifact blueprintArtifact)
186         throws SdcArtifactInstallerException {
187         String policyName = null;
188         Yaml yaml = new Yaml();
189         List<String> policyNameList = new ArrayList<>();
190         Map<String, Object> templateNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
191             .load(blueprintArtifact.getDcaeBlueprint())).get("node_templates"));
192         templateNodes.entrySet().stream().filter(e -> e.getKey().contains("policy")).forEach(ef -> {
193             String filteredPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ef.getValue())
194                 .get("properties")).get("policy_filter");
195             if (policyName != null) {
196                 policyNameList.add(filteredPolicyName);
197             } else {
198                 String inputPolicyName = (String) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ef
199                     .getValue()).get("properties")).get("policy_id")).get(GET_INPUT_BLUEPRINT_PARAM);
200                 if (inputPolicyName != null) {
201                     policyNameList.add(GET_INPUT_BLUEPRINT_PARAM);
202                 }
203             }
204         });
205         if (policyNameList.size() > 1) {
206             throw new SdcArtifactInstallerException(
207                 "The code does not currently support multiple Policy MicroServices in the blueprint");
208         } else if (policyNameList.isEmpty()) {
209             throw new SdcArtifactInstallerException(
210                 "There is no recognized Policy MicroService found in the blueprint");
211         }
212         logger.info("policyName found in blueprint " + blueprintArtifact.getBlueprintArtifactName() + " is "
213             + policyNameList.get(0));
214         return policyNameList.get(0);
215     }
216
217     /**
218      * This call must be done when deploying the SDC notification as this call get
219      * the latest version of the artifact (version can be specified to DCAE call)
220      *
221      * @return The DcaeInventoryResponse object containing the dcae values
222      */
223     private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
224         throws IOException, ParseException, InterruptedException {
225         return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
226             blueprintArtifact.getBlueprintInvariantServiceUuid(),
227             blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
228     }
229
230     private CldsTemplate createFakeCldsTemplate(CsarHandler csar, BlueprintArtifact blueprintArtifact,
231         BlueprintParserFilesConfiguration configFiles) throws IOException, SdcArtifactInstallerException {
232         CldsTemplate template = new CldsTemplate();
233         template.setBpmnId("Sdc-Generated");
234         template
235             .setBpmnText(IOUtils.toString(appContext.getResource(configFiles.getBpmnXmlFilePath()).getInputStream()));
236         template.setPropText(
237             "{\"global\":[{\"name\":\"service\",\"value\":[\"" + blueprintArtifact.getDcaeBlueprint() + "\"]}]}");
238         template
239             .setImageText(IOUtils.toString(appContext.getResource(configFiles.getSvgXmlFilePath()).getInputStream()));
240         template.setName(TEMPLATE_NAME_PREFIX + buildModelName(csar, blueprintArtifact));
241         template.save(cldsDao, null);
242         logger.info("Fake Clds Template created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
243             + " with name " + template.getName());
244         return template;
245     }
246
247     private CldsModel createFakeCldsModel(CsarHandler csar, BlueprintArtifact blueprintArtifact,
248         CldsTemplate cldsTemplate, DcaeInventoryResponse dcaeInventoryResponse) throws SdcArtifactInstallerException {
249
250         if (dcaeInventoryResponse == null) {
251             throw new SdcArtifactInstallerException(
252                 "DCAE inventory response is NULL, query to DCAE fail to be answered properly, this is required to deploy CSAR properly !!!");
253         }
254         try {
255             CldsModel cldsModel = new CldsModel();
256             cldsModel.setName(buildModelName(csar, blueprintArtifact));
257             cldsModel.setBlueprintText(blueprintArtifact.getDcaeBlueprint());
258             cldsModel.setTemplateName(cldsTemplate.getName());
259             cldsModel.setTemplateId(cldsTemplate.getId());
260             cldsModel.setBpmnText(cldsTemplate.getBpmnText());
261             cldsModel.setTypeId(dcaeInventoryResponse.getTypeId());
262             cldsModel.setTypeName(dcaeInventoryResponse.getTypeName());
263             cldsModel.setControlNamePrefix(CONTROL_NAME_PREFIX);
264             // We must save it otherwise object won't be created in db
265             // and proptext will always be null
266             cldsModel.setPropText("{\"global\":[]}");
267             // Must save first to have the generated id available to generate
268             // the policyId
269             cldsModel = cldsModel.save(cldsDao, null);
270             cldsModel = setModelPropText(cldsModel, blueprintArtifact, cldsTemplate);
271             logger.info("Fake Clds Model created for blueprint " + blueprintArtifact.getBlueprintArtifactName()
272                 + " with name " + cldsModel.getName());
273             return cldsModel;
274         } catch (TransformerException e) {
275             throw new SdcArtifactInstallerException("TransformerException when decoding the BpmnText", e);
276         }
277     }
278
279     private CldsModel setModelPropText(CldsModel cldsModel, BlueprintArtifact blueprintArtifact,
280         CldsTemplate cldsTemplate) throws TransformerException {
281         // Do a test to validate the BPMN
282         new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), "PUT", false,
283             cldsBpmnTransformer.doXslTransformToString(cldsTemplate.getBpmnText()), "{}");
284         String inputParams = "{\"name\":\"deployParameters\",\"value\":"
285             + getAllBlueprintParametersInJson(blueprintArtifact) + "}";
286         cldsModel.setPropText("{\"global\":[{\"name\":\"service\",\"value\":[\""
287             + blueprintArtifact.getBlueprintInvariantServiceUuid() + "\"]},{\"name\":\"vf\",\"value\":[\""
288             + blueprintArtifact.getResourceAttached().getResourceInvariantUUID()
289             + "\"]},{\"name\":\"actionSet\",\"value\":[\"vnfRecipe\"]},{\"name\":\"location\",\"value\":[\"DC1\"]},"
290             + inputParams + "]}");
291         return cldsModel.save(cldsDao, null);
292     }
293
294     private void addPropertyToNode(JsonObject node, String key, Object value) {
295         if (value instanceof String) {
296             node.addProperty(key, (String) value);
297         } else if (value instanceof Number) {
298             node.addProperty(key, (Number) value);
299         } else if (value instanceof Boolean) {
300             node.addProperty(key, (Boolean) value);
301         } else if (value instanceof Character) {
302             node.addProperty(key, (Character) value);
303         } else {
304             node.addProperty(key, JsonUtils.GSON.toJson(value));
305         }
306     }
307 }