2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.mso.bpmn.common.resource;
22 import com.google.gson.Gson;
23 import com.google.gson.reflect.TypeToken;
24 import org.jboss.resteasy.client.jaxrs.ResteasyClient;
25 import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
26 import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
27 import org.openecomp.mso.logger.MsoLogger;
28 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
29 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
30 import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory;
31 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
32 import org.openecomp.sdc.toscaparser.api.Property;
33 import org.openecomp.sdc.toscaparser.api.functions.GetInput;
34 import org.openecomp.sdc.toscaparser.api.parameters.Input;
36 import javax.ws.rs.core.Response;
38 import java.util.HashMap;
39 import java.util.LinkedHashMap;
40 import java.util.List;
42 import java.util.Optional;
44 public class ResourceRequestBuilder {
46 public static String CUSTOMIZATION_UUID = "customizationUUID";
47 public static String SERVICE_URL_TOSCA_CSAR = "http://localhost:8080/ecomp/mso/catalog/v3/serviceToscaCsar?serviceModelUuid=";
49 private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
51 public static Map<String, Object> buildResouceRequest(String serviceUuid,
52 String resourceCustomizationUuid,
53 Map<String, Object> serviceInputs) throws SdcToscaParserException {
55 Map<String, Object> resouceRequest = new HashMap<>();
57 String csarpath = null;
59 csarpath = getCsarFromUuid(serviceUuid);
60 } catch (Exception e) {
61 LOGGER.debug("csar file is not available for service uuid:" + serviceUuid, e);
62 return resouceRequest;
65 SdcToscaParserFactory toscaParser = SdcToscaParserFactory.getInstance();
66 ISdcCsarHelper iSdcCsarHelper = toscaParser.getSdcCsarHelper(csarpath);
68 List<Input> serInput = iSdcCsarHelper.getServiceInputs();
69 Optional<NodeTemplate> nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream()
70 .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid))
73 if (nodeTemplateOpt.isPresent()) {
74 NodeTemplate nodeTemplate = nodeTemplateOpt.get();
75 LinkedHashMap<String, Property> resourceProperties = nodeTemplate.getProperties();
77 for (String key: resourceProperties.keySet()) {
78 Property property = resourceProperties.get(key);
80 Object value = getValue(property.getValue(), serviceInputs, serInput);
81 resouceRequest.put(key, value);
84 return resouceRequest;
87 private static Object getValue(Object value, Map<String, Object> serviceInputs,
88 List<Input> servInputs) {
89 if (value instanceof Map) {
90 Map<String, Object> valueMap = new HashMap<>();
92 Map<String, Object> propertyMap = (Map<String, Object>) value;
94 for (String key: propertyMap.keySet()) {
95 valueMap.put(key, getValue(propertyMap.get(key), serviceInputs, servInputs));
97 return valueMap; // return if the value is nested hashmap
98 } else if (value instanceof GetInput) {
99 String inputName = ((GetInput) value).getInputName();
101 if (serviceInputs.get(inputName) != null) {
102 value = serviceInputs.get(inputName);
104 for (Input input: servInputs) {
105 if (input.getName().equals(inputName)) {
106 return input.getDefault(); // return default value
111 return value; // return property value
114 private static String getCsarFromUuid(String uuid) throws Exception {
116 ResteasyClient client = new ResteasyClientBuilder().build();
117 ResteasyWebTarget target = client.target(SERVICE_URL_TOSCA_CSAR + uuid);
118 Response response = target.request().get();
119 String value = response.readEntity(String.class);
121 HashMap<String,String> map = new Gson().fromJson(value, new TypeToken<HashMap<String, String>>(){}.getType());
123 File csarFile = new File(System.getProperty("mso.config.path") + "ASDC/" + map.get("name"));
125 if (!csarFile.exists()) {
126 throw new Exception("csar file does not exist.");
129 return csarFile.getAbsolutePath();