Sonar fixes
[clamp.git] / src / main / java / org / onap / clamp / loop / service / CsarServiceInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 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.service;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29
30 import java.util.Map.Entry;
31
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;
47
48 @Component
49 @Qualifier("csarInstaller")
50 public class CsarServiceInstaller {
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class);
52
53     @Autowired
54     ServiceRepository serviceRepository;
55
56     /**
57      * Install the Service from the csar.
58      * 
59      * @param csar The Csar Handler
60      * @return The service object
61      */
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);
67
68         // Add properties details for each type, VfModule, VF, VFC, ....
69         JsonObject resourcesProp = createServicePropertiesByType(csar);
70         resourcesProp.add("VFModule", createVfModuleProperties(csar));
71
72         Service modelService = new Service(serviceDetails, resourcesProp,
73                 csar.getSdcNotification().getServiceVersion());
74
75         serviceRepository.save(modelService);
76         logger.info("Successfully installed the Service");
77         return modelService;
78     }
79
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()));
89             }
90             resourcesProp.add(type.getValue(), resourcesPropByType);
91         }
92         return resourcesProp;
93     }
94
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()));
110             }
111         }
112         return vfModuleProps;
113     }
114
115     /**
116      * Verify whether Service in Csar is deployed.
117      * 
118      * @param csar The Csar Handler
119      * @return The flag indicating whether Service is deployed
120      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
121      */
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());
127
128         return alreadyInstalled;
129     }
130 }