Fix blueprint installation
[clamp.git] / src / main / java / org / onap / clamp / loop / CsarInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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.loop;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.io.IOException;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Map.Entry;
32 import org.json.simple.parser.ParseException;
33 import org.onap.clamp.clds.client.DcaeInventoryServices;
34 import org.onap.clamp.clds.client.PolicyEngineServices;
35 import org.onap.clamp.clds.exception.sdc.controller.BlueprintParserException;
36 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
37 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
38 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
39 import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService;
40 import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser;
41 import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator;
42 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
43 import org.onap.clamp.loop.cds.CdsDataInstaller;
44 import org.onap.clamp.loop.service.CsarServiceInstaller;
45 import org.onap.clamp.loop.service.Service;
46 import org.onap.clamp.loop.template.LoopElementModel;
47 import org.onap.clamp.loop.template.LoopTemplate;
48 import org.onap.clamp.loop.template.LoopTemplatesRepository;
49 import org.onap.clamp.loop.template.PolicyModel;
50 import org.onap.clamp.loop.template.PolicyModelId;
51 import org.onap.clamp.loop.template.PolicyModelsRepository;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.beans.factory.annotation.Qualifier;
54 import org.springframework.stereotype.Component;
55
56 /**
57  * This class will be instantiated by spring config, and used by Sdc Controller.
58  * There is no state kept by the bean. It's used to deploy the csar/notification
59  * received from SDC in DB.
60  */
61 @Component
62 @Qualifier("csarInstaller")
63 public class CsarInstaller {
64
65     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstaller.class);
66
67     @Autowired
68     private PolicyModelsRepository policyModelsRepository;
69
70     @Autowired
71     private LoopTemplatesRepository loopTemplatesRepository;
72
73     @Autowired
74     private ChainGenerator chainGenerator;
75
76     @Autowired
77     private DcaeInventoryServices dcaeInventoryService;
78
79     @Autowired
80     private CsarServiceInstaller csarServiceInstaller;
81
82     @Autowired
83     private CdsDataInstaller cdsDataInstaller;
84
85     @Autowired
86     private PolicyEngineServices policyEngineServices;
87
88     /**
89      * Verify whether Csar is deployed.
90      *
91      * @param csar The Csar Handler
92      * @return The flag indicating whether Csar is deployed
93      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
94      */
95     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
96         boolean alreadyInstalled = csarServiceInstaller.isServiceAlreadyDeployed(csar);
97
98         for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
99             alreadyInstalled = alreadyInstalled
100                     && loopTemplatesRepository.existsById(LoopTemplate.generateLoopTemplateName(
101                     csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
102                     blueprint.getValue().getResourceAttached().getResourceInstanceName(),
103                     blueprint.getValue().getBlueprintArtifactName()));
104         }
105         return alreadyInstalled;
106     }
107
108     /**
109      * Install the service and loop templates from the csar.
110      *
111      * @param csar The Csar Handler
112      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
113      * @throws InterruptedException          The InterruptedException
114      * @throws BlueprintParserException      In case of issues with the blueprint
115      *                                       parsing
116      */
117     public void installTheCsar(CsarHandler csar)
118             throws SdcArtifactInstallerException, InterruptedException, BlueprintParserException {
119         logger.info("Installing the CSAR " + csar.getFilePath());
120         Service associatedService = csarServiceInstaller.installTheService(csar);
121         cdsDataInstaller.installCdsServiceProperties(csar, associatedService);
122
123         installTheLoopTemplates(csar, associatedService);
124         logger.info("Successfully installed the CSAR " + csar.getFilePath());
125     }
126
127     /**
128      * Install the loop templates from the csar.
129      *
130      * @param csar    The Csar Handler
131      * @param service The service object that is related to the loop
132      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
133      * @throws InterruptedException          The InterruptedException
134      * @throws BlueprintParserException      In case of issues with the blueprint
135      *                                       parsing
136      */
137     public void installTheLoopTemplates(CsarHandler csar, Service service)
138             throws SdcArtifactInstallerException, InterruptedException, BlueprintParserException {
139         try {
140             logger.info("Installing the Loops");
141             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
142                 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
143                 loopTemplatesRepository.save(createLoopTemplateFromBlueprint(csar, blueprint.getValue(), service));
144             }
145             logger.info("Successfully installed the Loops ");
146         } catch (IOException e) {
147             throw new SdcArtifactInstallerException("Exception caught during the Loop installation in database", e);
148         } catch (ParseException e) {
149             throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
150         }
151     }
152
153     private LoopTemplate createLoopTemplateFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact,
154                                                          Service service)
155             throws IOException, ParseException, InterruptedException, BlueprintParserException,
156             SdcArtifactInstallerException {
157         LoopTemplate newLoopTemplate = new LoopTemplate();
158         newLoopTemplate.setBlueprint(blueprintArtifact.getDcaeBlueprint());
159         newLoopTemplate.setName(LoopTemplate.generateLoopTemplateName(csar.getSdcNotification().getServiceName(),
160                 csar.getSdcNotification().getServiceVersion(),
161                 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
162                 blueprintArtifact.getBlueprintArtifactName()));
163         List<BlueprintMicroService> microServicesChain = chainGenerator
164                 .getChainOfMicroServices(BlueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
165         if (microServicesChain.isEmpty()) {
166             microServicesChain = BlueprintParser.fallbackToOneMicroService();
167         }
168         newLoopTemplate.setModelService(service);
169         newLoopTemplate.addLoopElementModels(createMicroServiceModels(blueprintArtifact, microServicesChain));
170         newLoopTemplate.setMaximumInstancesAllowed(0);
171         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
172         newLoopTemplate.setDcaeBlueprintId(dcaeResponse.getTypeId());
173         return newLoopTemplate;
174     }
175
176     private HashSet<LoopElementModel> createMicroServiceModels(BlueprintArtifact blueprintArtifact,
177                                                                List<BlueprintMicroService> microServicesChain)
178             throws SdcArtifactInstallerException {
179         HashSet<LoopElementModel> newSet = new HashSet<>();
180         for (BlueprintMicroService microService : microServicesChain) {
181             LoopElementModel loopElementModel =
182                     new LoopElementModel(microService.getModelType(), LoopElementModel.MICRO_SERVICE_TYPE,
183                             null);
184             newSet.add(loopElementModel);
185             PolicyModel newPolicyModel = policyEngineServices.createPolicyModelFromPolicyEngine(microService);
186             if (newPolicyModel != null) {
187                 loopElementModel.addPolicyModel(newPolicyModel);
188             } else {
189                 throw new SdcArtifactInstallerException(
190                         "Unable to find the policy specified in the blueprint " +
191                                 blueprintArtifact.getBlueprintArtifactName() + ") on the Policy Engine:" +
192                                 microService.getModelType() + "/" + microService.getModelVersion());
193             }
194         }
195         return newSet;
196     }
197
198     /**
199      * Get the service blueprint Id in the Dcae inventory using the SDC UUID.
200      *
201      * @return The DcaeInventoryResponse object containing the dcae values
202      */
203     private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
204             throws IOException, ParseException, InterruptedException {
205         return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
206                 blueprintArtifact.getBlueprintInvariantServiceUuid(),
207                 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
208     }
209
210 }