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 com.google.gson.JsonObject;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map.Entry;
36 import org.json.simple.parser.ParseException;
37 import org.onap.clamp.clds.client.DcaeInventoryServices;
38 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
39 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
40 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
41 import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser;
42 import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator;
43 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
44 import org.onap.clamp.clds.sdc.controller.installer.MicroService;
45 import org.onap.clamp.clds.util.drawing.SvgFacade;
46 import org.onap.clamp.loop.deploy.DcaeDeployParameters;
47 import org.onap.clamp.loop.service.CsarServiceInstaller;
48 import org.onap.clamp.loop.service.Service;
49 import org.onap.clamp.policy.Policy;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.beans.factory.annotation.Qualifier;
54 import org.springframework.stereotype.Component;
55 import org.springframework.transaction.annotation.Propagation;
56 import org.springframework.transaction.annotation.Transactional;
59 * This class will be instantiated by spring config, and used by Sdc Controller.
60 * There is no state kept by the bean. It's used to deploy the csar/notification
61 * received from SDC in DB.
64 @Qualifier("csarInstaller")
65 public class CsarInstaller {
67 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstaller.class);
68 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
69 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
70 // This will be used later as the policy scope
71 public static final String MODEL_NAME_PREFIX = "Loop_";
74 LoopsRepository loopRepository;
77 BlueprintParser blueprintParser;
80 ChainGenerator chainGenerator;
83 DcaeInventoryServices dcaeInventoryService;
86 private SvgFacade svgFacade;
89 CsarServiceInstaller csarServiceInstaller;
92 * Verify whether Csar is deployed.
94 * @param csar The Csar Handler
95 * @return The flag indicating whether Csar is deployed
96 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
98 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
99 boolean alreadyInstalled = csarServiceInstaller.isServiceAlreadyDeployed(csar);
101 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
102 alreadyInstalled = alreadyInstalled
103 && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
104 csar.getSdcNotification().getServiceVersion(),
105 blueprint.getValue().getResourceAttached().getResourceInstanceName(),
106 blueprint.getValue().getBlueprintArtifactName()));
108 return alreadyInstalled;
112 * Install the service and loops from the csar.
114 * @param csar The Csar Handler
115 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
116 * @throws InterruptedException The InterruptedException
118 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException {
119 logger.info("Installing the CSAR " + csar.getFilePath());
120 installTheLoop(csar, csarServiceInstaller.installTheService(csar));
121 logger.info("Successfully installed the CSAR " + csar.getFilePath());
125 * Install the Loop from the csar.
127 * @param csar The Csar Handler
128 * @param service The service object that is related to the loop
129 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
130 * @throws InterruptedException The InterruptedException
132 @Transactional(propagation = Propagation.REQUIRES_NEW)
133 public void installTheLoop(CsarHandler csar, Service service)
134 throws SdcArtifactInstallerException, InterruptedException {
136 logger.info("Installing the Loops");
137 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
138 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
139 loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue(), service));
141 logger.info("Successfully installed the Loops ");
142 } catch (IOException e) {
143 throw new SdcArtifactInstallerException("Exception caught during the Loop installation in database", e);
144 } catch (ParseException e) {
145 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
149 private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact, Service service)
150 throws IOException, ParseException, InterruptedException {
151 Loop newLoop = new Loop();
152 newLoop.setBlueprint(blueprintArtifact.getDcaeBlueprint());
153 newLoop.setName(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
154 csar.getSdcNotification().getServiceVersion(),
155 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
156 blueprintArtifact.getBlueprintArtifactName()));
157 newLoop.setLastComputedState(LoopState.DESIGN);
159 List<MicroService> microServicesChain = chainGenerator
160 .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
161 if (microServicesChain.isEmpty()) {
162 microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
164 newLoop.setModelService(service);
165 newLoop.setMicroServicePolicies(
166 createMicroServicePolicies(microServicesChain, csar, blueprintArtifact, newLoop));
167 newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
169 newLoop.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain));
170 newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact, newLoop));
172 DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
173 newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
177 private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
179 return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
180 csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
181 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
182 blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
185 private HashSet<MicroServicePolicy> createMicroServicePolicies(List<MicroService> microServicesChain,
186 CsarHandler csar, BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
187 HashSet<MicroServicePolicy> newSet = new HashSet<>();
189 for (MicroService microService : microServicesChain) {
190 MicroServicePolicy microServicePolicy = new MicroServicePolicy(
191 Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(),
192 csar.getSdcNotification().getServiceVersion(),
193 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
194 blueprintArtifact.getBlueprintArtifactName()),
195 microService.getModelType(), csar.getPolicyModelYaml().orElse(""), false,
196 new HashSet<>(Arrays.asList(newLoop)));
198 newSet.add(microServicePolicy);
199 microService.setMappedNameJpa(microServicePolicy.getName());
204 private JsonObject createGlobalPropertiesJson(BlueprintArtifact blueprintArtifact, Loop newLoop) {
205 return DcaeDeployParameters.getDcaeDeploymentParametersInJson(blueprintArtifact, newLoop);
209 * ll get the latest version of the artifact (version can be specified to DCAE
212 * @return The DcaeInventoryResponse object containing the dcae values
214 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
215 throws IOException, ParseException, InterruptedException {
216 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
217 blueprintArtifact.getBlueprintInvariantServiceUuid(),
218 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());