f6a303291f05ef90e4751b85baae6e2357dde54a
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / etsi / pkg / processor / EtsiCatalogServiceProvider.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.asdc.etsi.pkg.processor;
21
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;
36
37 /**
38  * @author Waqas Ikram (waqas.ikram@est.tech)
39  *
40  */
41 @Service
42 public class EtsiCatalogServiceProvider {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(EtsiCatalogServiceProvider.class);
45
46     private final HttpRestServiceProvider httpServiceProvider;
47
48     @Value("${etsi-catalog-manager.endpoint:http://modeling-etsicatalog.onap:8806/api/catalog/v1}")
49     private String etsiCatalogManagerEndpoint;
50
51     @Autowired
52     public EtsiCatalogServiceProvider(
53             @Qualifier(ETSI_CATALOG_HTTP_REST_SERVICE_PROVIDER_BEAN) final HttpRestServiceProvider httpServiceProvider) {
54         this.httpServiceProvider = httpServiceProvider;
55     }
56
57     public EtsiCatalogPackageOnboardingJob onBoardResource(
58             final EtsiCatalogPackageOnboardingRequest packageOnboardingRequest) {
59         try {
60             final String url = etsiCatalogManagerEndpoint + "/vnfpackages";
61             final ResponseEntity<EtsiCatalogPackageOnboardingJob> responseEntity = httpServiceProvider.postHttpRequest(
62                     packageOnboardingRequest, url, getHeaders(), EtsiCatalogPackageOnboardingJob.class);
63
64             if (responseEntity.getStatusCode().is2xxSuccessful()) {
65                 if (responseEntity.hasBody()) {
66                     return responseEntity.getBody();
67                 }
68                 LOGGER.error("Received response without body");
69             }
70             final String message = "Unexpected status code received " + responseEntity.getStatusCode();
71             LOGGER.error(message);
72             throw new EtsiCatalogManagerRequestFailureException(message);
73
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);
78         }
79
80     }
81
82     public EtsiCatalogPackageOnboadingJobStatus getJobStatus(final String jobId) {
83         try {
84             final String url = etsiCatalogManagerEndpoint + "/jobs/" + jobId;
85
86             final ResponseEntity<EtsiCatalogPackageOnboadingJobStatus> responseEntity =
87                     httpServiceProvider.getHttpResponse(url, getHeaders(), EtsiCatalogPackageOnboadingJobStatus.class);
88
89             if (responseEntity.getStatusCode().is2xxSuccessful()) {
90                 if (responseEntity.hasBody()) {
91                     return responseEntity.getBody();
92                 }
93                 LOGGER.error("Received response without body");
94             }
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);
103         }
104
105     }
106
107     private HttpHeaders getHeaders() {
108         final HttpHeaders headers = new HttpHeaders();
109         headers.setContentType(MediaType.APPLICATION_JSON);
110         return headers;
111     }
112 }