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.JsonUtils;
46 import org.onap.clamp.clds.util.drawing.SvgFacade;
47 import org.onap.clamp.loop.deploy.DcaeDeployParameters;
48 import org.onap.clamp.loop.service.Service;
49 import org.onap.clamp.loop.service.ServiceRepository;
50 import org.onap.clamp.policy.Policy;
51 import org.onap.clamp.policy.microservice.MicroServicePolicy;
52 import org.onap.clamp.policy.operational.OperationalPolicy;
53 import org.onap.sdc.tosca.parser.api.IEntityDetails;
54 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
55 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
56 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
57 import org.onap.sdc.tosca.parser.enums.SdcTypes;
58 import org.onap.sdc.toscaparser.api.NodeTemplate;
59 import org.onap.sdc.toscaparser.api.Property;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.beans.factory.annotation.Qualifier;
62 import org.springframework.stereotype.Component;
63 import org.springframework.transaction.annotation.Propagation;
64 import org.springframework.transaction.annotation.Transactional;
65 import org.yaml.snakeyaml.Yaml;
68 * This class will be instantiated by spring config, and used by Sdc Controller.
69 * There is no state kept by the bean. It's used to deploy the csar/notification
70 * received from SDC in DB.
73 @Qualifier("csarInstaller")
74 public class CsarInstaller {
76 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstaller.class);
77 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
78 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
79 // This will be used later as the policy scope
80 public static final String MODEL_NAME_PREFIX = "Loop_";
83 LoopsRepository loopRepository;
86 ServiceRepository serviceRepository;
89 BlueprintParser blueprintParser;
92 ChainGenerator chainGenerator;
95 DcaeInventoryServices dcaeInventoryService;
98 private SvgFacade svgFacade;
101 * Verify whether Csar is deployed.
103 * @param csar The Csar Handler
104 * @return The flag indicating whether Csar is deployed
105 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
107 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
108 boolean alreadyInstalled = true;
109 JsonObject serviceDetails = JsonUtils.GSON.fromJson(
110 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
111 alreadyInstalled = alreadyInstalled
112 && serviceRepository.existsById(serviceDetails.get("UUID").getAsString());
114 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
115 alreadyInstalled = alreadyInstalled
116 && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
117 csar.getSdcNotification().getServiceVersion(),
118 blueprint.getValue().getResourceAttached().getResourceInstanceName(),
119 blueprint.getValue().getBlueprintArtifactName()));
122 return alreadyInstalled;
126 * Install the service and loops from the csar.
128 * @param csar The Csar Handler
129 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
130 * @throws InterruptedException The InterruptedException
132 public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException {
133 logger.info("Installing the CSAR " + csar.getFilePath());
134 installTheLoop(csar, installTheService(csar));
135 logger.info("Successfully installed the CSAR " + csar.getFilePath());
139 * Install the Loop from the csar.
141 * @param csar The Csar Handler
142 * @param service The service object that is related to the loop
143 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
144 * @throws InterruptedException The InterruptedException
146 @Transactional(propagation = Propagation.REQUIRED)
147 public void installTheLoop(CsarHandler csar, Service service)
148 throws SdcArtifactInstallerException, InterruptedException {
150 logger.info("Installing the Loops");
151 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
152 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
153 loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue(), service));
155 logger.info("Successfully installed the Loops ");
156 } catch (IOException e) {
157 throw new SdcArtifactInstallerException("Exception caught during the Loop installation in database", e);
158 } catch (ParseException e) {
159 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
164 * Install the Service from the csar.
166 * @param csar The Csar Handler
167 * @return The service object
170 public Service installTheService(CsarHandler csar) {
171 logger.info("Start to install the Service from csar");
172 JsonObject serviceDetails = JsonUtils.GSON.fromJson(
173 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
175 // Add properties details for each type, VfModule, VF, VFC, ....
176 JsonObject resourcesProp = createServicePropertiesByType(csar);
177 resourcesProp.add("VFModule", createVfModuleProperties(csar));
179 Service modelService = new Service(serviceDetails, resourcesProp,
180 csar.getSdcNotification().getServiceVersion());
182 serviceRepository.save(modelService);
183 logger.info("Successfully installed the Service");
187 private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact, Service service)
188 throws IOException, ParseException, InterruptedException {
189 Loop newLoop = new Loop();
190 newLoop.setBlueprint(blueprintArtifact.getDcaeBlueprint());
191 newLoop.setName(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
192 csar.getSdcNotification().getServiceVersion(),
193 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
194 blueprintArtifact.getBlueprintArtifactName()));
195 newLoop.setLastComputedState(LoopState.DESIGN);
197 List<MicroService> microServicesChain = chainGenerator
198 .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
199 if (microServicesChain.isEmpty()) {
200 microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
202 newLoop.setModelService(service);
203 newLoop.setMicroServicePolicies(
204 createMicroServicePolicies(microServicesChain, csar, blueprintArtifact, newLoop));
205 newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
207 newLoop.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain));
208 newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact, newLoop));
210 DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
211 newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
215 private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
217 return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
218 csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
219 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
220 blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
223 private HashSet<MicroServicePolicy> createMicroServicePolicies(List<MicroService> microServicesChain,
224 CsarHandler csar, BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
225 HashSet<MicroServicePolicy> newSet = new HashSet<>();
227 for (MicroService microService : microServicesChain) {
228 MicroServicePolicy microServicePolicy = new MicroServicePolicy(
229 Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(),
230 csar.getSdcNotification().getServiceVersion(),
231 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
232 blueprintArtifact.getBlueprintArtifactName()),
233 microService.getModelType(), csar.getPolicyModelYaml().orElse(""), false,
234 new HashSet<>(Arrays.asList(newLoop)));
236 newSet.add(microServicePolicy);
237 microService.setMappedNameJpa(microServicePolicy.getName());
242 private JsonObject createGlobalPropertiesJson(BlueprintArtifact blueprintArtifact, Loop newLoop) {
243 return DcaeDeployParameters.getDcaeDeploymentParametersInJson(blueprintArtifact, newLoop);
246 private static JsonObject createVfModuleProperties(CsarHandler csar) {
247 JsonObject vfModuleProps = new JsonObject();
248 // Loop on all Groups defined in the service (VFModule entries type:
249 // org.openecomp.groups.VfModule)
250 for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
251 EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
252 TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
253 // Get all metadata info
254 JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
255 vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
256 // now append the properties section so that we can also have isBase,
257 // volume_group, etc ... fields under the VFmodule name
258 for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
259 allVfProps.add(additionalProp.getValue().getName(),
260 JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
263 return vfModuleProps;
266 private static JsonObject createServicePropertiesByType(CsarHandler csar) {
267 JsonObject resourcesProp = new JsonObject();
268 // Iterate on all types defined in the tosca lib
269 for (SdcTypes type : SdcTypes.values()) {
270 JsonObject resourcesPropByType = new JsonObject();
271 // For each type, get the metadata of each nodetemplate
272 for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
273 resourcesPropByType.add(nodeTemplate.getName(),
274 JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
276 resourcesProp.add(type.getValue(), resourcesPropByType);
278 return resourcesProp;
282 * ll get the latest version of the artifact (version can be specified to DCAE
285 * @return The DcaeInventoryResponse object containing the dcae values
287 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
288 throws IOException, ParseException, InterruptedException {
289 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
290 blueprintArtifact.getBlueprintInvariantServiceUuid(),
291 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());