Fix refresh action
[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  * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END============================================
21  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.loop.service;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonObject;
30 import java.util.Map.Entry;
31 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
32 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
33 import org.onap.clamp.clds.util.JsonUtils;
34 import org.onap.sdc.tosca.parser.api.IEntityDetails;
35 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
36 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
37 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
38 import org.onap.sdc.tosca.parser.enums.SdcTypes;
39 import org.onap.sdc.toscaparser.api.NodeTemplate;
40 import org.onap.sdc.toscaparser.api.Property;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Qualifier;
43 import org.springframework.stereotype.Component;
44 import org.springframework.transaction.annotation.Propagation;
45 import org.springframework.transaction.annotation.Transactional;
46
47 @Component
48 @Qualifier("csarInstaller")
49 public class CsarServiceInstaller {
50     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class);
51
52     @Autowired
53     ServicesRepository serviceRepository;
54
55     /**
56      * Install the Service from the csar.
57      *
58      * @param csar The Csar Handler
59      * @return The service object
60      */
61     @Transactional(propagation = Propagation.REQUIRES_NEW)
62     public Service installTheService(CsarHandler csar) {
63         logger.info("Start to install the Service from csar");
64         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
65                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
66
67         // Add properties details for each type, VfModule, VF, VFC, ....
68         JsonObject resourcesProp = createServicePropertiesByType(csar);
69         resourcesProp.add("VFModule", createVfModuleProperties(csar));
70
71         Service modelService = new Service(serviceDetails, resourcesProp,
72                 csar.getSdcNotification().getServiceVersion());
73
74         serviceRepository.save(modelService);
75         logger.info("Successfully installed the Service");
76         return modelService;
77     }
78
79     private JsonObject createServicePropertiesByType(CsarHandler csar) {
80         JsonObject resourcesProp = new JsonObject();
81         // Iterate on all types defined in the tosca lib
82         for (SdcTypes type : SdcTypes.values()) {
83             JsonObject resourcesPropByType = new JsonObject();
84             // For each type, get the metadata of each nodetemplate
85             for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
86                 resourcesPropByType.add(nodeTemplate.getName(),
87                         JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
88             }
89             resourcesProp.add(type.getValue(), resourcesPropByType);
90         }
91         return resourcesProp;
92     }
93
94     private static JsonObject createVfModuleProperties(CsarHandler csar) {
95         JsonObject vfModuleProps = new JsonObject();
96         // Loop on all Groups defined in the service (VFModule entries type:
97         // org.openecomp.groups.VfModule)
98         for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
99                 EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
100                 TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
101             // Get all metadata info
102             JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
103             vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
104             // now append the properties section so that we can also have isBase,
105             // volume_group, etc ... fields under the VFmodule name
106             for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
107                 allVfProps.add(additionalProp.getValue().getName(),
108                         JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
109             }
110         }
111         return vfModuleProps;
112     }
113
114     /**
115      * Verify whether Service in Csar is deployed.
116      *
117      * @param csar The Csar Handler
118      * @return The flag indicating whether Service is deployed
119      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
120      */
121     public boolean isServiceAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
122         boolean alreadyInstalled = true;
123         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
124                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
125         alreadyInstalled = serviceRepository.existsById(serviceDetails.get("UUID").getAsString());
126
127         return alreadyInstalled;
128     }
129 }