Update license for ResourceRequest
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / resource / ResourceRequestBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.mso.bpmn.common.resource;
21
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;
35
36 import javax.ws.rs.core.Response;
37 import java.io.File;
38 import java.util.HashMap;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Optional;
43
44 public class ResourceRequestBuilder {
45
46     public static String CUSTOMIZATION_UUID = "customizationUUID";
47     public static String SERVICE_URL_TOSCA_CSAR = "http://localhost:8080/ecomp/mso/catalog/v3/serviceToscaCsar?serviceModelUuid=";
48
49     private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
50
51     public static Map<String, Object> buildResouceRequest(String serviceUuid,
52                                               String resourceCustomizationUuid,
53                                               Map<String, Object> serviceInputs) throws SdcToscaParserException {
54
55         Map<String, Object> resouceRequest = new HashMap<>();
56
57         String csarpath = null;
58         try {
59             csarpath = getCsarFromUuid(serviceUuid);
60         } catch (Exception e) {
61             LOGGER.debug("csar file is not available for service uuid:" + serviceUuid, e);
62             return resouceRequest;
63         }
64
65         SdcToscaParserFactory toscaParser = SdcToscaParserFactory.getInstance();
66         ISdcCsarHelper iSdcCsarHelper = toscaParser.getSdcCsarHelper(csarpath);
67
68         List<Input> serInput = iSdcCsarHelper.getServiceInputs();
69         Optional<NodeTemplate> nodeTemplateOpt = iSdcCsarHelper.getServiceNodeTemplates().stream()
70                 .filter(e -> e.getMetaData().getValue(CUSTOMIZATION_UUID).equals(resourceCustomizationUuid))
71                 .findFirst();
72
73         if (nodeTemplateOpt.isPresent()) {
74             NodeTemplate nodeTemplate = nodeTemplateOpt.get();
75             LinkedHashMap<String, Property> resourceProperties = nodeTemplate.getProperties();
76
77             for (String key: resourceProperties.keySet()) {
78                 Property property = resourceProperties.get(key);
79
80                 Object value = getValue(property.getValue(), serviceInputs, serInput);
81                 resouceRequest.put(key, value);
82             }
83         }
84         return resouceRequest;
85     }
86
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<>();
91
92             Map<String, Object> propertyMap = (Map<String, Object>) value;
93
94             for (String key: propertyMap.keySet()) {
95                 valueMap.put(key, getValue(propertyMap.get(key), serviceInputs, servInputs));
96             }
97             return valueMap; // return if the value is nested hashmap
98         } else if (value instanceof GetInput) {
99             String inputName = ((GetInput) value).getInputName();
100
101             if (serviceInputs.get(inputName) != null) {
102                 value = serviceInputs.get(inputName);
103             } else {
104                 for (Input input: servInputs) {
105                     if (input.getName().equals(inputName)) {
106                         return input.getDefault();  // return default value
107                     }
108                 }
109             }
110         }
111         return value; // return property value
112     }
113
114     private static String getCsarFromUuid(String uuid) throws Exception {
115
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);
120
121         HashMap<String,String> map = new Gson().fromJson(value, new TypeToken<HashMap<String, String>>(){}.getType());
122
123         File csarFile = new File(System.getProperty("mso.config.path") + "ASDC/" + map.get("name"));
124
125         if (!csarFile.exists()) {
126             throw new Exception("csar file does not exist.");
127         }
128
129         return csarFile.getAbsolutePath();
130     }
131 }