2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Ericsson. All rights reserved.
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
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.asdc.etsi.pkg.processor;
22 import static org.onap.so.asdc.etsi.pkg.processor.HttpRestServiceProviderConfiguration.ETSI_CATALOG_HTTP_REST_SERVICE_PROVIDER_BEAN;
23 import org.onap.so.asdc.etsi.pkg.processor.exceptions.EtsiCatalogManagerRequestFailureException;
24 import org.onap.so.rest.exceptions.InvalidRestRequestException;
25 import org.onap.so.rest.exceptions.RestProcessingException;
26 import org.onap.so.rest.service.HttpRestServiceProvider;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Qualifier;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Service;
38 * @author Waqas Ikram (waqas.ikram@est.tech)
42 public class EtsiCatalogServiceProvider {
44 private static final Logger LOGGER = LoggerFactory.getLogger(EtsiCatalogServiceProvider.class);
46 private final HttpRestServiceProvider httpServiceProvider;
48 @Value("${etsi-catalog-manager.endpoint:http://modeling-etsicatalog.onap:8806/api/catalog/v1}")
49 private String etsiCatalogManagerEndpoint;
52 public EtsiCatalogServiceProvider(
53 @Qualifier(ETSI_CATALOG_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
54 this.httpServiceProvider = httpServiceProvider;
57 public EtsiCatalogPackageOnboardingJob onBoardResource(
58 final EtsiCatalogPackageOnboardingRequest packageOnboardingRequest) {
60 final String url = etsiCatalogManagerEndpoint + "/vnfpackages";
61 final ResponseEntity<EtsiCatalogPackageOnboardingJob> responseEntity = httpServiceProvider.postHttpRequest(
62 packageOnboardingRequest, url, getHeaders(), EtsiCatalogPackageOnboardingJob.class);
64 if (responseEntity.getStatusCode().is2xxSuccessful()) {
65 if (responseEntity.hasBody()) {
66 return responseEntity.getBody();
68 LOGGER.error("Received response without body");
70 final String message = "Unexpected status code received " + responseEntity.getStatusCode();
71 LOGGER.error(message);
72 throw new EtsiCatalogManagerRequestFailureException(message);
74 } catch (final InvalidRestRequestException | RestProcessingException exception) {
75 final String message = "Unable to process onboarding request";
76 LOGGER.error(message, exception);
77 throw new EtsiCatalogManagerRequestFailureException(message);
82 public EtsiCatalogPackageOnboadingJobStatus getJobStatus(final String jobId) {
84 final String url = etsiCatalogManagerEndpoint + "/jobs/" + jobId;
86 final ResponseEntity<EtsiCatalogPackageOnboadingJobStatus> responseEntity =
87 httpServiceProvider.getHttpResponse(url, getHeaders(), EtsiCatalogPackageOnboadingJobStatus.class);
89 if (responseEntity.getStatusCode().is2xxSuccessful()) {
90 if (responseEntity.hasBody()) {
91 return responseEntity.getBody();
93 LOGGER.error("Received response without body");
95 final String message =
96 "Unexpected status code received while getting job status " + responseEntity.getStatusCode();
97 LOGGER.error(message);
98 throw new EtsiCatalogManagerRequestFailureException(message);
99 } catch (final InvalidRestRequestException | RestProcessingException exception) {
100 final String message = "Unable to get job status";
101 LOGGER.error(message, exception);
102 throw new EtsiCatalogManagerRequestFailureException(message);
107 private HttpHeaders getHeaders() {
108 final HttpHeaders headers = new HttpHeaders();
109 headers.setContentType(MediaType.APPLICATION_JSON);