Fix the tosca converter
[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
31 import java.util.Map.Entry;
32
33 import org.onap.clamp.clds.client.CdsServices;
34 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
35 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
36 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
37 import org.onap.clamp.clds.util.JsonUtils;
38 import org.onap.sdc.tosca.parser.api.IEntityDetails;
39 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
40 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
41 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
42 import org.onap.sdc.tosca.parser.enums.SdcTypes;
43 import org.onap.sdc.toscaparser.api.NodeTemplate;
44 import org.onap.sdc.toscaparser.api.Property;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Qualifier;
47 import org.springframework.stereotype.Component;
48 import org.springframework.transaction.annotation.Propagation;
49 import org.springframework.transaction.annotation.Transactional;
50
51 @Component
52 @Qualifier("csarInstaller")
53 public class CsarServiceInstaller {
54     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class);
55
56     @Autowired
57     ServicesRepository serviceRepository;
58
59     @Autowired
60     CdsServices cdsServices;
61
62     /**
63      * Install the Service from the csar.
64      *
65      * @param csar The Csar Handler
66      * @return The service object
67      */
68     @Transactional(propagation = Propagation.REQUIRES_NEW)
69     public Service installTheService(CsarHandler csar) {
70         logger.info("Start to install the Service from csar");
71         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
72                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
73
74         // Add properties details for each type, VfModule, VF, VFC, ....
75         JsonObject resourcesProp = createServicePropertiesByType(csar);
76         resourcesProp.add("VFModule", createVfModuleProperties(csar));
77
78         Service modelService = new Service(serviceDetails, resourcesProp,
79                 csar.getSdcNotification().getServiceVersion());
80
81         serviceRepository.save(modelService);
82         logger.info("Successfully installed the Service");
83         return modelService;
84     }
85
86     private JsonObject createServicePropertiesByType(CsarHandler csar) {
87         JsonObject resourcesProp = new JsonObject();
88         // Iterate on all types defined in the tosca lib
89         for (SdcTypes type : SdcTypes.values()) {
90             JsonObject resourcesPropByType = new JsonObject();
91             // For each type, get the metadata of each nodetemplate
92             for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
93                 resourcesPropByType.add(nodeTemplate.getName(),
94                         JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
95                 // get cds artifact information and save in resources Prop
96                 if (SdcTypes.PNF == type || SdcTypes.VF == type) {
97                     JsonObject controllerProperties = createCdsArtifactProperties(nodeTemplate);
98                     if (controllerProperties != null) {
99                         resourcesPropByType.getAsJsonObject(nodeTemplate.getName()).add("controllerProperties", controllerProperties);
100                     }
101                 }
102             }
103             resourcesProp.add(type.getValue(), resourcesPropByType);
104         }
105         return resourcesProp;
106     }
107
108     private static JsonObject createVfModuleProperties(CsarHandler csar) {
109         JsonObject vfModuleProps = new JsonObject();
110         // Loop on all Groups defined in the service (VFModule entries type:
111         // org.openecomp.groups.VfModule)
112         for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
113                 EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
114                 TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
115             // Get all metadata info
116             JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
117             vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
118             // now append the properties section so that we can also have isBase,
119             // volume_group, etc ... fields under the VFmodule name
120             for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
121                 allVfProps.add(additionalProp.getValue().getName(),
122                         JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
123             }
124         }
125         return vfModuleProps;
126     }
127
128     /**
129      * Verify whether Service in Csar is deployed.
130      *
131      * @param csar The Csar Handler
132      * @return The flag indicating whether Service is deployed
133      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
134      */
135     public boolean isServiceAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
136         boolean alreadyInstalled = true;
137         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
138                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
139         alreadyInstalled = serviceRepository.existsById(serviceDetails.get("UUID").getAsString());
140
141         return alreadyInstalled;
142     }
143
144     /**
145      * Retrive CDS artifacts information from node template and save in resource object.
146      *
147      * @param nodeTemplate node template
148      * @return Returns CDS artifacts information
149      */
150     private JsonObject createCdsArtifactProperties(NodeTemplate nodeTemplate) {
151         Object artifactName = nodeTemplate.getPropertyValue("sdnc_model_name");
152         Object artifactVersion = nodeTemplate.getPropertyValue("sdnc_model_version");
153         if (artifactName != null && artifactVersion != null) {
154             CdsBpWorkFlowListResponse response = queryCdsToGetWorkFlowList(artifactName.toString(), artifactVersion.toString());
155             if (response == null) {
156                 return null;
157             }
158
159             JsonObject workFlowProps = new JsonObject();
160             for (String workFlow : response.getWorkflows()) {
161                 JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(),
162                                                                          response.getVersion(), workFlow);
163                 workFlowProps.add(workFlow, inputs);
164             }
165
166             JsonObject controllerProperties = new JsonObject();
167             controllerProperties.addProperty("sdnc_model_name", artifactName.toString());
168             controllerProperties.addProperty("sdnc_model_version", artifactVersion.toString());
169             controllerProperties.add("workflows", workFlowProps);
170             return controllerProperties;
171         }
172         return null;
173     }
174
175     private CdsBpWorkFlowListResponse queryCdsToGetWorkFlowList(String artifactName, String artifactVersion) {
176         return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion);
177     }
178
179     private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion, String workFlow) {
180         return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow);
181     }
182 }