2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.loop;
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;
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.
62 @Qualifier("csarInstaller")
63 public class CsarInstaller {
65 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstaller.class);
68 private PolicyModelsRepository policyModelsRepository;
71 private LoopTemplatesRepository loopTemplatesRepository;
74 private ChainGenerator chainGenerator;
77 private DcaeInventoryServices dcaeInventoryService;
80 private CsarServiceInstaller csarServiceInstaller;
83 private CdsDataInstaller cdsDataInstaller;
86 private PolicyEngineServices policyEngineServices;
89 * Verify whether Csar is deployed.
91 * @param csar The Csar Handler
92 * @return The flag indicating whether Csar is deployed
93 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
95 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
96 boolean alreadyInstalled = csarServiceInstaller.isServiceAlreadyDeployed(csar);
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()));
105 return alreadyInstalled;
109 * Install the service and loop templates from the csar.
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
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);
123 installTheLoopTemplates(csar, associatedService);
124 logger.info("Successfully installed the CSAR " + csar.getFilePath());
128 * Install the loop templates from the csar.
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
137 public void installTheLoopTemplates(CsarHandler csar, Service service)
138 throws SdcArtifactInstallerException, InterruptedException, BlueprintParserException {
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));
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);
153 private LoopTemplate createLoopTemplateFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact,
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();
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;
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,
184 newSet.add(loopElementModel);
185 PolicyModel newPolicyModel = policyEngineServices.createPolicyModelFromPolicyEngine(microService);
186 if (newPolicyModel != null) {
187 loopElementModel.addPolicyModel(newPolicyModel);
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());
199 * Get the service blueprint Id in the Dcae inventory using the SDC UUID.
201 * @return The DcaeInventoryResponse object containing the dcae values
203 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
204 throws IOException, ParseException, InterruptedException {
205 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
206 blueprintArtifact.getBlueprintInvariantServiceUuid(),
207 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());