9a5962099376a6759e1fd9f9d6429ef832b90c0f
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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  *
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
21 package org.onap.so.adapters.vnfmadapter.extclients.etsicatalog;
22
23 import java.util.Optional;
24 import org.onap.so.adapters.vnfmadapter.rest.exceptions.EtsiCatalogManagerRequestFailureException;
25 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgConflictException;
26 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfPkgNotFoundException;
27 import org.onap.so.rest.exceptions.HttpResouceNotFoundException;
28 import org.onap.so.rest.exceptions.RestProcessingException;
29 import org.onap.so.rest.service.HttpRestServiceProvider;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * Provides the implementations of the REST Requests to the ETSI Catalog Manager.
40  * 
41  * @author gareth.roper@est.tech
42  */
43 @Service
44 public class EtsiCatalogServiceProviderImpl implements EtsiCatalogServiceProvider {
45     private static final Logger logger = LoggerFactory.getLogger(EtsiCatalogServiceProviderImpl.class);
46
47     @Qualifier
48     private final HttpRestServiceProvider httpServiceProvider;
49     private final EtsiCatalogUrlProvider etsiCatalogUrlProvider;
50
51     @Autowired
52     public EtsiCatalogServiceProviderImpl(final EtsiCatalogUrlProvider etsiCatalogUrlProvider,
53             final HttpRestServiceProvider httpServiceProvider) {
54         this.etsiCatalogUrlProvider = etsiCatalogUrlProvider;
55         this.httpServiceProvider = httpServiceProvider;
56     }
57
58     @Override
59     public Optional<byte[]> getVnfPackageContent(String vnfPkgId) throws EtsiCatalogManagerRequestFailureException {
60         try {
61             ResponseEntity<byte[]> response = httpServiceProvider
62                     .getHttpResponse(etsiCatalogUrlProvider.getVnfPackageContentUrl(vnfPkgId), byte[].class);
63             logger.info("getVnfPackageContent Request to ETSI Catalog Manager Status Code: {}",
64                     response.getStatusCodeValue());
65             if (response.getStatusCode() == HttpStatus.OK) {
66                 return Optional.ofNullable(response.getBody());
67             }
68         } catch (HttpResouceNotFoundException httpResouceNotFoundException) {
69             logger.error("Caught HttpResouceNotFoundException", httpResouceNotFoundException);
70             throw new VnfPkgNotFoundException("No Vnf Package found with vnfPkgId: " + vnfPkgId);
71         } catch (RestProcessingException restProcessingException) {
72             logger.error("Caught RestProcessingException with Status Code: {}", restProcessingException.getStatusCode(),
73                     restProcessingException);
74             if (restProcessingException.getStatusCode() == HttpStatus.CONFLICT.value()) {
75                 throw new VnfPkgConflictException("A conflict occurred with the state of the resource,\n"
76                         + "due to the attribute: onboardingState not being set to ONBOARDED.");
77             }
78         }
79         throw new EtsiCatalogManagerRequestFailureException("Internal Server Error Occurred.");
80     }
81 }