9c1cd3bc2030d80d6bf515bf72069a7f1e0a558d
[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(nodeTemplate.getPropertyValue(
77                             SDNC_MODEL_NAME).toString(),
78                             nodeTemplate.getPropertyValue(SDNC_MODEL_VERSION).toString());
79                     if (controllerProperties != null) {
80                         resourcesPropByType.getAsJsonObject(nodeTemplate.getName())
81                                 .add(CONTROLLER_PROPERTIES, controllerProperties);
82                     }
83                 }
84             }
85         }
86         serviceRepository.save(service);
87         logger.info("Successfully installed the CDS data in Service");
88         return service;
89     }
90
91     /**
92      * This method updates the service model properties for CDS in the service object given in input.
93      *
94      * @param service the service object already provisioned with csar data
95      */
96     @Transactional(propagation = Propagation.REQUIRES_NEW)
97     public Service updateCdsServiceProperties(Service service) {
98         // Iterate on all types defined in the tosca lib
99         for (SdcTypes type : SdcTypes.values()) {
100             JsonObject resourcesPropByType = service.getResourceByType(type.getValue());
101             for (String resourceName : resourcesPropByType.keySet()) {
102                 // get cds artifact information and save in resources Prop
103                 if ((SdcTypes.PNF == type || SdcTypes.VF == type) && resourcesPropByType.getAsJsonObject(resourceName)
104                         .getAsJsonObject(CONTROLLER_PROPERTIES) != null) {
105                     JsonObject controllerProperties =
106                             createCdsArtifactProperties(resourcesPropByType.getAsJsonObject(resourceName)
107                                             .getAsJsonObject(CONTROLLER_PROPERTIES).get(SDNC_MODEL_NAME)
108                                             .getAsString(),
109                                     resourcesPropByType.getAsJsonObject(resourceName)
110                                             .getAsJsonObject(CONTROLLER_PROPERTIES).get(SDNC_MODEL_VERSION)
111                                             .getAsString());
112                     if (controllerProperties != null) {
113                         resourcesPropByType.getAsJsonObject(resourceName)
114                                 .add(CONTROLLER_PROPERTIES, controllerProperties);
115                     }
116                 }
117             }
118         }
119         serviceRepository.save(service);
120         logger.info("Successfully updated the CDS data in Service");
121         return service;
122     }
123
124     /**
125      * Retrieve CDS artifacts information from node template and save in resource object.
126      *
127      * @param sdncModelName    sdnc model name
128      * @param sdncModelVersion sdnc model version
129      * @return Returns CDS artifacts information
130      */
131     private JsonObject createCdsArtifactProperties(String sdncModelName, String sdncModelVersion) {
132         if (sdncModelName != null && sdncModelVersion != null) {
133             JsonObject controllerProperties = new JsonObject();
134             controllerProperties.addProperty(SDNC_MODEL_NAME, sdncModelName);
135             controllerProperties.addProperty(SDNC_MODEL_VERSION, sdncModelVersion);
136
137             CdsBpWorkFlowListResponse response =
138                     queryCdsToGetWorkFlowList(sdncModelName, sdncModelVersion);
139             if (response == null) {
140                 return controllerProperties;
141             }
142
143             JsonObject workFlowProps = new JsonObject();
144             for (String workFlow : response.getWorkflows()) {
145                 logger.info("Found CDS workflow " + workFlow + " for model name " + sdncModelName + " and version "
146                         + sdncModelVersion);
147                 JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(),
148                         response.getVersion(), workFlow);
149                 workFlowProps.add(workFlow, inputs);
150             }
151
152             controllerProperties.add("workflows", workFlowProps);
153             return controllerProperties;
154         }
155         return null;
156     }
157
158
159     private CdsBpWorkFlowListResponse queryCdsToGetWorkFlowList(String artifactName, String artifactVersion) {
160         return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion);
161     }
162
163     private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion,
164                                                             String workFlow) {
165         return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow);
166     }
167 }