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