221c3a7fc5c9fe654cf372c6ee1e7a7d4f1a397c
[sdc.git] /
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  *
19  *
20  */
21
22 package org.openecomp.sdcrests.vsp.rest.services;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.jetbrains.annotations.Nullable;
26 import org.onap.sdc.tosca.services.YamlUtil;
27 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
28 import org.openecomp.sdc.common.CommonConfigurationManager;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.errors.CatalogRestClientException;
31 import org.openecomp.sdc.common.errors.Messages;
32 import org.openecomp.sdc.common.http.client.api.HttpRequest;
33 import org.openecomp.sdc.common.http.client.api.HttpResponse;
34 import org.openecomp.sdc.logging.api.Logger;
35 import org.openecomp.sdc.logging.api.LoggerFactory;
36 import org.openecomp.sdcrests.item.rest.services.catalog.notification.EntryNotConfiguredException;
37 import org.openecomp.sdcrests.item.rest.services.catalog.notification.http.HttpConfiguration;
38 import org.openecomp.sdcrests.vsp.rest.CatalogVspClient;
39
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.Properties;
47
48 import static javax.ws.rs.core.HttpHeaders.ACCEPT;
49 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
50
51 public class CatalogVspClientImpl implements CatalogVspClient {
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(CatalogVspClientImpl.class);
54     private static final String URL_GET_RESOURCE_BY_CSAR_UUID = "%s://%s:%s/sdc2/rest/v1/catalog/resources/csar/%s";
55     private static final String CONFIG_FILE_PROPERTY = "configuration.yaml";
56     private static final String CONFIG_SECTION = "catalogNotificationsConfig";
57     public static final String NAME = "name";
58     public static final String SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID = "sdc2/rest/v1/catalog/resources/csar/{csaruuid}";
59
60     public CatalogVspClientImpl() { }
61
62     /**
63      * Returns the name of a VF which is using the provided VSP.
64      * It returns an empty optional in case the VSP is not used by any VF,
65      * or throws ans exception if any error occurs during the process.
66      *
67      * @param vspId        the id of the vsp
68      * @param user         the user to perform the action
69      */
70     @Override
71     public Optional<String> findNameOfVfUsingVsp(String vspId, String user) throws CatalogRestClientException {
72         try {
73             HttpConfiguration httpConfig = getHttpConfiguration();
74             if (null == httpConfig) {
75                 throw new CatalogRestClientException(ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(),
76                         vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID));
77             }
78             final Properties headers = new Properties();
79             headers.put(Constants.USER_ID_HEADER, user);
80             headers.put(ACCEPT, APPLICATION_JSON);
81             String url = String.format(URL_GET_RESOURCE_BY_CSAR_UUID, httpConfig.getCatalogBeProtocol(),
82                     httpConfig.getCatalogBeFqdn(), httpConfig.getCatalogBeHttpPort(), vspId);
83             final HttpResponse<String> httpResponse;
84             httpResponse = HttpRequest.get(url, headers);
85             ObjectMapper mapper = new ObjectMapper();
86             Map<String, Object> respObject = mapper.readValue(httpResponse.getResponse(), Map.class);
87             return Optional.of((String) respObject.get(NAME));
88
89         } catch (Exception e) {
90             String formattedErrorMessage = ErrorMessagesFormatBuilder.getErrorWithParameters(Messages.DELETE_VSP_UNEXPECTED_ERROR_USED_BY_VF.getErrorMessage(),
91                     vspId, SDC_2_REST_V_1_CATALOG_RESOURCES_CSAR_CSARUUID);
92             LOGGER.error(formattedErrorMessage,  e);
93             throw new CatalogRestClientException(formattedErrorMessage, e);
94         }
95     }
96
97     @Nullable
98     private HttpConfiguration getHttpConfiguration() throws CatalogRestClientException {
99         HttpConfiguration httpConfig;
100         try {
101             Object config = getEndpointConfiguration();
102             ObjectMapper mapper = new ObjectMapper();
103             httpConfig = mapper.convertValue(config, HttpConfiguration.class);
104         } catch (Exception e) {
105             LOGGER.error("Failed to load configuration. ", e);
106             throw new CatalogRestClientException("Failed to load configuration. ", e);
107         }
108         return httpConfig;
109     }
110
111     private static Object getEndpointConfiguration() {
112         final var commonConfigurationManager = CommonConfigurationManager.getInstance();
113         return commonConfigurationManager.getConfigValue(CONFIG_SECTION);
114     }
115 }