2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.loop.cds;
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;
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.
47 public class CdsDataInstaller {
49 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CdsDataInstaller.class);
52 CdsServices cdsServices;
55 ServicesRepository serviceRepository;
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";
62 * This method installs the service model properties for CDS in the service object given in input.
64 * @param csar The csar from sdc
65 * @param service the service object already provisioned with csar data
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);
86 serviceRepository.save(service);
87 logger.info("Successfully installed the CDS data in Service");
92 * This method updates the service model properties for CDS in the service object given in input.
94 * @param service the service object already provisioned with csar data
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)
109 resourcesPropByType.getAsJsonObject(resourceName)
110 .getAsJsonObject(CONTROLLER_PROPERTIES).get(SDNC_MODEL_VERSION)
112 if (controllerProperties != null) {
113 resourcesPropByType.getAsJsonObject(resourceName)
114 .add(CONTROLLER_PROPERTIES, controllerProperties);
119 serviceRepository.save(service);
120 logger.info("Successfully updated the CDS data in Service");
125 * Retrieve CDS artifacts information from node template and save in resource object.
127 * @param sdncModelName sdnc model name
128 * @param sdncModelVersion sdnc model version
129 * @return Returns CDS artifacts information
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);
137 CdsBpWorkFlowListResponse response =
138 queryCdsToGetWorkFlowList(sdncModelName, sdncModelVersion);
139 if (response == null) {
140 return controllerProperties;
143 JsonObject workFlowProps = new JsonObject();
144 for (String workFlow : response.getWorkflows()) {
145 logger.info("Found CDS workflow " + workFlow + " for model name " + sdncModelName + " and version "
147 JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(),
148 response.getVersion(), workFlow);
149 workFlowProps.add(workFlow, inputs);
152 controllerProperties.add("workflows", workFlowProps);
153 return controllerProperties;
159 private CdsBpWorkFlowListResponse queryCdsToGetWorkFlowList(String artifactName, String artifactVersion) {
160 return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion);
163 private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion,
165 return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow);