2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.asdc.rest;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
25 import org.onap.portalsdk.core.util.SystemProperties;
26 import org.onap.vid.asdc.AsdcCatalogException;
27 import org.onap.vid.asdc.AsdcClient;
28 import org.onap.vid.asdc.beans.Service;
29 import org.onap.vid.model.ModelConstants;
30 import org.onap.vid.properties.VidProperties;
31 import org.onap.vid.utils.Logging;
32 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.springframework.http.HttpMethod;
35 import javax.ws.rs.ProcessingException;
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.client.Client;
38 import javax.ws.rs.client.ResponseProcessingException;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.Response;
42 import java.io.IOException;
43 import java.io.InputStream;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.StandardCopyOption;
48 import java.util.Collections;
49 import java.util.UUID;
51 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
53 * The Class RestfulAsdcClient.
55 @SuppressWarnings("Duplicates")
56 public class RestfulAsdcClient implements AsdcClient {
62 public static class Builder {
67 private final Client client;
72 private final URI uri;
77 private String auth = null;
80 * Instantiates a new builder.
82 * @param client the client
85 public Builder(Client client, URI uri) {
87 this.client.register(JacksonJsonProvider.class);
94 * @param auth the auth
97 public Builder auth(String auth) {
105 * @return the restful asdc client
107 public RestfulAsdcClient build() {
108 return new RestfulAsdcClient(this);
115 static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class);
117 final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("asdc");
122 private final Client client;
127 private final URI uri;
130 * The common headers.
132 private final MultivaluedHashMap<String, Object> commonHeaders;
137 private final String auth;
140 * Instantiates a new restful asdc client.
142 * @param builder the builder
144 RestfulAsdcClient(Builder builder) {
145 client = builder.client;
149 commonHeaders = new MultivaluedHashMap<String, Object>();
150 commonHeaders.put("X-ECOMP-InstanceID", Collections.singletonList((Object) (SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME))));
151 commonHeaders.put("Authorization", Collections.singletonList((Object) (auth)));
154 private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException {
157 csarFile = Files.createTempFile("csar", ".zip");
158 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
159 } catch (IOException e) {
160 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
170 private Client getClient() {
175 * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
177 public Service getService(UUID uuid) throws AsdcCatalogException {
179 String path = VidProperties.getPropertyWithDefault(
180 ModelConstants.ASDC_SVC_API_PATH,
181 ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
183 String url = uri+path + "/" + uuid.toString() + "/metadata";
184 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
186 Response response = getClient()
188 .path(path + "/" + uuid.toString() + "/metadata")
189 .request(MediaType.APPLICATION_JSON)
190 .headers(commonHeaders)
191 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
193 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);
194 return response.readEntity(Service.class);
195 } catch (ResponseProcessingException e) {
196 //Couldn't convert response to Java type
197 throw new AsdcCatalogException("SDC response could not be processed", e);
198 } catch (ProcessingException e) {
199 //IO problems during request
200 throw new AsdcCatalogException("Failed to get a response from SDC service", e);
201 } catch (WebApplicationException e) {
202 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
203 throw new AsdcCatalogException(e);
209 * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
211 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
212 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH,
213 ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
215 String url = uri+path + "/" + serviceUuid + "/toscaModel";
216 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
218 final InputStream csarInputStream = getClient()
220 .path(path + "/" + serviceUuid + "/toscaModel")
221 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
222 .headers(commonHeaders)
223 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
224 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
225 .get(InputStream.class);
226 Path toscaFilePath = createTmpFile(csarInputStream);
227 outgoingRequestsLogger.debug("Received {} {} . Tosca file was saved at: {}", HttpMethod.GET.name(), url, toscaFilePath.toAbsolutePath());
228 return toscaFilePath;
229 } catch (ResponseProcessingException e) {
230 //Couldn't convert response to Java type
231 throw new AsdcCatalogException("SDC response could not be processed", e);
232 } catch (ProcessingException e) {
233 //IO problems during request
234 throw new AsdcCatalogException("Failed to get a response from SDC service. Cause: "+e.getMessage(), e);
235 } catch (WebApplicationException e) {
236 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
237 throw new AsdcCatalogException(e);