Fix crash at SDC deploy
[clamp.git] / src / main / java / org / onap / clamp / loop / cds / CdsDataInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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.cds;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonObject;
30 import org.onap.clamp.clds.client.CdsServices;
31 import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
32 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
33 import org.onap.clamp.loop.service.Service;
34 import org.onap.clamp.loop.service.ServicesRepository;
35 import org.onap.sdc.tosca.parser.enums.SdcTypes;
36 import org.onap.sdc.toscaparser.api.NodeTemplate;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39 import org.springframework.transaction.annotation.Propagation;
40 import org.springframework.transaction.annotation.Transactional;
41
42 /**
43  * This class installs the cds data in the service model properties.
44  * This can be refreshed later on by clicking on the button refresh, when recomputing the json schema.
45  */
46 @Component
47 public class CdsDataInstaller {
48
49     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsDataInstaller.class);
50
51     @Autowired
52     CdsServices cdsServices;
53
54     @Autowired
55     ServicesRepository serviceRepository;
56
57     public static final String CONTROLLER_PROPERTIES = "controllerProperties";
58     public static final String SDNC_MODEL_NAME = "sdnc_model_name";
59     public static final String SDNC_MODEL_VERSION = "sdnc_model_version";
60
61     /**
62      * This method installs the service model properties for CDS in the service object given in input.
63      *
64      * @param csar    The csar from sdc
65      * @param service the service object already provisioned with csar data
66      */
67     @Transactional(propagation = Propagation.REQUIRES_NEW)
68     public Service installCdsServiceProperties(CsarHandler csar, Service service) {
69         // Iterate on all types defined in the tosca lib
70         for (SdcTypes type : SdcTypes.values()) {
71             JsonObject resourcesPropByType = service.getResourceByType(type.getValue());
72             // For each type, get the metadata of each nodetemplate
73             for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
74                 // get cds artifact information and save in resources Prop
75                 if (SdcTypes.PNF == type || SdcTypes.VF == type) {
76                     JsonObject controllerProperties = createCdsArtifactProperties(
77                             String.valueOf(nodeTemplate.getPropertyValue(SDNC_MODEL_NAME)),
78                             String.valueOf(nodeTemplate.getPropertyValue(SDNC_MODEL_VERSION)));
79                     if (controllerProperties != null) {
80                         resourcesPropByType.getAsJsonObject(nodeTemplate.getName())
81                                 .add(CONTROLLER_PROPERTIES, controllerProperties);
82                         logger.info("Successfully installed the CDS data in Service");
83                     }
84                     else {
85                         logger.warn("Skipping CDS data installation in Service, as sdnc_model_name and "
86                                 + "sdnc_model_version are not provided in the CSAR");
87                     }
88                 }
89             }
90         }
91         serviceRepository.save(service);
92
93         return service;
94     }
95
96     /**
97      * This method updates the service model properties for CDS in the service object given in input.
98      *
99      * @param service the service object already provisioned with csar data
100      */
101     @Transactional(propagation = Propagation.REQUIRES_NEW)
102     public Service updateCdsServiceProperties(Service service) {
103         // Iterate on all types defined in the tosca lib
104         for (SdcTypes type : SdcTypes.values()) {
105             JsonObject resourcesPropByType = service.getResourceByType(type.getValue());
106             for (String resourceName : resourcesPropByType.keySet()) {
107                 // get cds artifact information and save in resources Prop
108                 if ((SdcTypes.PNF == type || SdcTypes.VF == type) && resourcesPropByType.getAsJsonObject(resourceName)
109                         .getAsJsonObject(CONTROLLER_PROPERTIES) != null) {
110                     JsonObject controllerProperties =
111                             createCdsArtifactProperties(resourcesPropByType.getAsJsonObject(resourceName)
112                                             .getAsJsonObject(CONTROLLER_PROPERTIES).get(SDNC_MODEL_NAME)
113                                             .getAsString(),
114                                     resourcesPropByType.getAsJsonObject(resourceName)
115                                             .getAsJsonObject(CONTROLLER_PROPERTIES).get(SDNC_MODEL_VERSION)
116                                             .getAsString());
117                     if (controllerProperties != null) {
118                         resourcesPropByType.getAsJsonObject(resourceName)
119                                 .add(CONTROLLER_PROPERTIES, controllerProperties);
120                     }
121                 }
122             }
123         }
124         serviceRepository.save(service);
125         logger.info("Successfully updated the CDS data in Service");
126         return service;
127     }
128
129     /**
130      * Retrieve CDS artifacts information from node template and save in resource object.
131      *
132      * @param sdncModelName    sdnc model name
133      * @param sdncModelVersion sdnc model version
134      * @return Returns CDS artifacts information
135      */
136     private JsonObject createCdsArtifactProperties(String sdncModelName, String sdncModelVersion) {
137         if (sdncModelName != null && !"null".equals(sdncModelName)
138                 && sdncModelVersion != null && !"null".equals(sdncModelVersion)) {
139             JsonObject controllerProperties = new JsonObject();
140             controllerProperties.addProperty(SDNC_MODEL_NAME, sdncModelName);
141             controllerProperties.addProperty(SDNC_MODEL_VERSION, sdncModelVersion);
142
143             CdsBpWorkFlowListResponse response =
144                     queryCdsToGetWorkFlowList(sdncModelName, sdncModelVersion);
145             if (response == null) {
146                 return controllerProperties;
147             }
148
149             JsonObject workFlowProps = new JsonObject();
150             for (String workFlow : response.getWorkflows()) {
151                 logger.info("Found CDS workflow " + workFlow + " for model name " + sdncModelName + " and version "
152                         + sdncModelVersion);
153                 JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(),
154                         response.getVersion(), workFlow);
155                 workFlowProps.add(workFlow, inputs);
156             }
157
158             controllerProperties.add("workflows", workFlowProps);
159             return controllerProperties;
160         }
161         return null;
162     }
163
164
165     private CdsBpWorkFlowListResponse queryCdsToGetWorkFlowList(String artifactName, String artifactVersion) {
166         return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion);
167     }
168
169     private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion,
170                                                             String workFlow) {
171         return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow);
172     }
173 }