2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
22 package org.openecomp.sdcrests.vsp.rest.services;
24 import static javax.ws.rs.core.HttpHeaders.ACCEPT;
25 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.util.Optional;
30 import java.util.Properties;
31 import org.jetbrains.annotations.Nullable;
32 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
33 import org.openecomp.sdc.common.CommonConfigurationManager;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.openecomp.sdc.common.errors.CatalogRestClientException;
36 import org.openecomp.sdc.common.errors.Messages;
37 import org.openecomp.sdc.common.http.client.api.HttpRequest;
38 import org.openecomp.sdc.common.http.client.api.HttpResponse;
39 import org.openecomp.sdc.logging.api.Logger;
40 import org.openecomp.sdc.logging.api.LoggerFactory;
41 import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpConfiguration;
42 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
44 public class CatalogVspClientImpl implements CatalogVspClient {
46 private static final Logger LOGGER = LoggerFactory.getLogger(CatalogVspClientImpl.class);
47 private static final String URL_GET_RESOURCE_BY_CSAR_UUID = "%s://%s:%s/sdc2/rest/v1/catalog/resources/csar/%s";
48 private static final String CONFIG_SECTION = "catalogNotificationsConfig";
49 public static final String NAME = "name";
50 public static final String SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID = "sdc2/rest/v1/catalog/resources/csar/{csaruuid}";
53 * Returns the name of a VF which is using the provided VSP.
54 * It returns an empty optional in case the VSP is not used by any VF,
55 * or throws ans exception if any error occurs during the process.
57 * @param vspId the id of the vsp
58 * @param user the user to perform the action
61 public Optional<String> findNameOfVfUsingVsp(String vspId, String user) throws CatalogRestClientException {
63 HttpConfiguration httpConfig = getHttpConfiguration();
64 if (null == httpConfig) {
65 throw new CatalogRestClientException(ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(),
66 vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID));
68 final Properties headers = new Properties();
69 headers.put(Constants.USER_ID_HEADER, user);
70 headers.put(ACCEPT, APPLICATION_JSON);
71 String url = String.format(URL_GET_RESOURCE_BY_CSAR_UUID, httpConfig.getCatalogBeProtocol(),
72 httpConfig.getCatalogBeFqdn(), httpConfig.getCatalogBeHttpPort(), vspId);
73 final HttpResponse<String> httpResponse;
74 httpResponse = HttpRequest.get(url, headers);
75 ObjectMapper mapper = new ObjectMapper();
76 Map<String, Object> respObject = mapper.readValue(httpResponse.getResponse(), Map.class);
77 return Optional.of((String) respObject.get(NAME));
79 } catch (Exception e) {
80 String formattedErrorMessage = ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(),
81 vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID);
82 LOGGER.error(formattedErrorMessage, e);
83 throw new CatalogRestClientException(formattedErrorMessage, e);
88 private HttpConfiguration getHttpConfiguration() throws CatalogRestClientException {
89 HttpConfiguration httpConfig;
91 Object config = getEndpointConfiguration();
92 ObjectMapper mapper = new ObjectMapper();
93 httpConfig = mapper.convertValue(config, HttpConfiguration.class);
94 } catch (Exception e) {
95 LOGGER.error("Failed to load configuration. ", e);
96 throw new CatalogRestClientException("Failed to load configuration. ", e);
101 private static Object getEndpointConfiguration() {
102 final var commonConfigurationManager = CommonConfigurationManager.getInstance();
103 return commonConfigurationManager.getConfigValue(CONFIG_SECTION);