67c7ce5cff8388727420435c309e2ed4fae105a4
[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         LoopTemplate newLoopTemplate = new LoopTemplate();
157         newLoopTemplate.setBlueprint(blueprintArtifact.getDcaeBlueprint());
158         newLoopTemplate.setName(LoopTemplate.generateLoopTemplateName(csar.getSdcNotification().getServiceName(),
159                 csar.getSdcNotification().getServiceVersion(),
160                 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
161                 blueprintArtifact.getBlueprintArtifactName()));
162         List<BlueprintMicroService> microServicesChain = chainGenerator
163                 .getChainOfMicroServices(BlueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
164         if (microServicesChain.isEmpty()) {
165             microServicesChain = BlueprintParser.fallbackToOneMicroService();
166         }
167         newLoopTemplate.setModelService(service);
168         newLoopTemplate.addLoopElementModels(createMicroServiceModels(microServicesChain));
169         newLoopTemplate.setMaximumInstancesAllowed(0);
170         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
171         newLoopTemplate.setDcaeBlueprintId(dcaeResponse.getTypeId());
172         return newLoopTemplate;
173     }
174
175     private HashSet<LoopElementModel> createMicroServiceModels(List<BlueprintMicroService> microServicesChain)
176             throws InterruptedException {
177         HashSet<LoopElementModel> newSet = new HashSet<>();
178         for (BlueprintMicroService microService : microServicesChain) {
179             LoopElementModel loopElementModel =
180                     new LoopElementModel(microService.getModelType(), LoopElementModel.MICRO_SERVICE_TYPE,
181                             null);
182             newSet.add(loopElementModel);
183             loopElementModel.addPolicyModel(getPolicyModel(microService));
184         }
185         return newSet;
186     }
187
188     private PolicyModel getPolicyModel(BlueprintMicroService microService) throws InterruptedException {
189         return policyModelsRepository
190                 .findById(new PolicyModelId(microService.getModelType(), microService.getModelVersion()))
191                 .orElse(policyEngineServices.createPolicyModelFromPolicyEngine(microService));
192     }
193
194     /**
195      * Get the service blueprint Id in the Dcae inventory using the SDC UUID.
196      *
197      * @return The DcaeInventoryResponse object containing the dcae values
198      */
199     private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
200             throws IOException, ParseException, InterruptedException {
201         return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
202                 blueprintArtifact.getBlueprintInvariantServiceUuid(),
203                 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
204     }
205
206 }