2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.service;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
30 import java.util.Map.Entry;
32 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
33 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
34 import org.onap.clamp.clds.util.JsonUtils;
35 import org.onap.sdc.tosca.parser.api.IEntityDetails;
36 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
37 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
38 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
39 import org.onap.sdc.tosca.parser.enums.SdcTypes;
40 import org.onap.sdc.toscaparser.api.NodeTemplate;
41 import org.onap.sdc.toscaparser.api.Property;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.stereotype.Component;
45 import org.springframework.transaction.annotation.Propagation;
46 import org.springframework.transaction.annotation.Transactional;
49 @Qualifier("csarInstaller")
50 public class CsarServiceInstaller {
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class);
54 ServiceRepository serviceRepository;
57 * Install the Service from the csar.
59 * @param csar The Csar Handler
60 * @return The service object
62 @Transactional(propagation = Propagation.REQUIRES_NEW)
63 public Service installTheService(CsarHandler csar) {
64 logger.info("Start to install the Service from csar");
65 JsonObject serviceDetails = JsonUtils.GSON.fromJson(
66 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
68 // Add properties details for each type, VfModule, VF, VFC, ....
69 JsonObject resourcesProp = createServicePropertiesByType(csar);
70 resourcesProp.add("VFModule", createVfModuleProperties(csar));
72 Service modelService = new Service(serviceDetails, resourcesProp,
73 csar.getSdcNotification().getServiceVersion());
75 serviceRepository.save(modelService);
76 logger.info("Successfully installed the Service");
80 private static JsonObject createServicePropertiesByType(CsarHandler csar) {
81 JsonObject resourcesProp = new JsonObject();
82 // Iterate on all types defined in the tosca lib
83 for (SdcTypes type : SdcTypes.values()) {
84 JsonObject resourcesPropByType = new JsonObject();
85 // For each type, get the metadata of each nodetemplate
86 for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
87 resourcesPropByType.add(nodeTemplate.getName(),
88 JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
90 resourcesProp.add(type.getValue(), resourcesPropByType);
95 private static JsonObject createVfModuleProperties(CsarHandler csar) {
96 JsonObject vfModuleProps = new JsonObject();
97 // Loop on all Groups defined in the service (VFModule entries type:
98 // org.openecomp.groups.VfModule)
99 for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
100 EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
101 TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
102 // Get all metadata info
103 JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
104 vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
105 // now append the properties section so that we can also have isBase,
106 // volume_group, etc ... fields under the VFmodule name
107 for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
108 allVfProps.add(additionalProp.getValue().getName(),
109 JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
112 return vfModuleProps;
116 * Verify whether Service in Csar is deployed.
118 * @param csar The Csar Handler
119 * @return The flag indicating whether Service is deployed
120 * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
122 public boolean isServiceAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
123 boolean alreadyInstalled = true;
124 JsonObject serviceDetails = JsonUtils.GSON.fromJson(
125 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
126 alreadyInstalled = serviceRepository.existsById(serviceDetails.get("UUID").getAsString());
128 return alreadyInstalled;