diff options
author | Ofir Sonsino <os0695@intl.att.com> | 2018-08-30 08:35:12 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-30 08:35:12 +0000 |
commit | 81bd169638241c8505cc9d59e0dc5b00bf26e8fc (patch) | |
tree | 1b1ab00c9ad62a4c91165afc7de2ae5931a21ade /vid-app-common/src/main | |
parent | ab0bee6be2fc4be7c659216eaf54f1eab560d547 (diff) | |
parent | 17e8b54e83547d8dc37c335c5df1b989f8b5ff3c (diff) |
Merge "Replace sdc client"
Diffstat (limited to 'vid-app-common/src/main')
5 files changed, 168 insertions, 286 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/asdc/AsdcClient.java b/vid-app-common/src/main/java/org/onap/vid/asdc/AsdcClient.java index 4e0254cde..fdbf2c288 100644 --- a/vid-app-common/src/main/java/org/onap/vid/asdc/AsdcClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/asdc/AsdcClient.java @@ -29,7 +29,10 @@ import java.util.UUID; * The Interface AsdcClient. */ public interface AsdcClient { - + class URIS{ + public static final String METADATA_URL_TEMPLATE = "%s%s/%s/metadata"; + public static final String TOSCA_MODEL_URL_TEMPLATE = "%s%s/%s/toscaModel"; + } /** * Gets the service. * diff --git a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/RestfulAsdcClient.java b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/RestfulAsdcClient.java deleted file mode 100644 index 5dd0f4cce..000000000 --- a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/RestfulAsdcClient.java +++ /dev/null @@ -1,242 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.asdc.rest; - -import com.att.eelf.configuration.EELFLogger; -import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; -import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.vid.asdc.AsdcCatalogException; -import org.onap.vid.asdc.AsdcClient; -import org.onap.vid.asdc.beans.Service; -import org.onap.vid.model.ModelConstants; -import org.onap.vid.properties.VidProperties; -import org.onap.vid.utils.Logging; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.springframework.http.HttpMethod; - -import javax.ws.rs.ProcessingException; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ResponseProcessingException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.Response; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.Collections; -import java.util.UUID; - -import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; -/** - * The Class RestfulAsdcClient. - */ -@SuppressWarnings("Duplicates") -public class RestfulAsdcClient implements AsdcClient { - - - /** - * The Class Builder. - */ - public static class Builder { - - /** - * The client. - */ - private final Client client; - - /** - * The uri. - */ - private final URI uri; - - /** - * The auth. - */ - private String auth = null; - - /** - * Instantiates a new builder. - * - * @param client the client - * @param uri the uri - */ - public Builder(Client client, URI uri) { - this.client = client; - this.client.register(JacksonJsonProvider.class); - this.uri = uri; - } - - /** - * Auth. - * - * @param auth the auth - * @return the builder - */ - public Builder auth(String auth) { - this.auth = auth; - return this; - } - - /** - * Builds the. - * - * @return the restful asdc client - */ - public RestfulAsdcClient build() { - return new RestfulAsdcClient(this); - } - } - - /** - * The Constant LOG. - */ - static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class); - - final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("asdc"); - - /** - * The client. - */ - private final Client client; - - /** - * The uri. - */ - private final URI uri; - - /** - * The common headers. - */ - private final MultivaluedHashMap<String, Object> commonHeaders; - - /** - * The auth. - */ - private final String auth; - - /** - * Instantiates a new restful asdc client. - * - * @param builder the builder - */ - RestfulAsdcClient(Builder builder) { - client = builder.client; - uri = builder.uri; - auth = builder.auth; - - commonHeaders = new MultivaluedHashMap<String, Object>(); - commonHeaders.put("X-ECOMP-InstanceID", Collections.singletonList((Object) (SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)))); - commonHeaders.put("Authorization", Collections.singletonList((Object) (auth))); - } - - private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException { - final Path csarFile; - try { - csarFile = Files.createTempFile("csar", ".zip"); - Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - throw new AsdcCatalogException("Caught IOException while creating CSAR", e); - } - return csarFile; - } - - /** - * Gets the client. - * - * @return the client - */ - private Client getClient() { - return client; - } - - /* (non-Javadoc) - * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID) - */ - public Service getService(UUID uuid) throws AsdcCatalogException { - - String path = VidProperties.getPropertyWithDefault( - ModelConstants.ASDC_SVC_API_PATH, - ModelConstants.DEFAULT_ASDC_SVC_API_PATH); - - String url = uri+path + "/" + uuid.toString() + "/metadata"; - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); - try { - Response response = getClient() - .target(uri) - .path(path + "/" + uuid.toString() + "/metadata") - .request(MediaType.APPLICATION_JSON) - .headers(commonHeaders) - .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()) - .get(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); - return response.readEntity(Service.class); - } catch (ResponseProcessingException e) { - //Couldn't convert response to Java type - throw new AsdcCatalogException("SDC response could not be processed", e); - } catch (ProcessingException e) { - //IO problems during request - throw new AsdcCatalogException("Failed to get a response from SDC service", e); - } catch (WebApplicationException e) { - //Web service returned data, but the response status wasn't a good one (i.e. non 2xx) - throw new AsdcCatalogException(e); - } - } - - - /* (non-Javadoc) - * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID) - */ - public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException { - String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, - ModelConstants.DEFAULT_ASDC_SVC_API_PATH); - - String url = uri+path + "/" + serviceUuid + "/toscaModel"; - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); - try { - final InputStream csarInputStream = getClient() - .target(uri) - .path(path + "/" + serviceUuid + "/toscaModel") - .request(MediaType.APPLICATION_OCTET_STREAM_TYPE) - .headers(commonHeaders) - .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM) - .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()) - .get(InputStream.class); - Path toscaFilePath = createTmpFile(csarInputStream); - outgoingRequestsLogger.debug("Received {} {} . Tosca file was saved at: {}", HttpMethod.GET.name(), url, toscaFilePath.toAbsolutePath()); - return toscaFilePath; - } catch (ResponseProcessingException e) { - //Couldn't convert response to Java type - throw new AsdcCatalogException("SDC response could not be processed", e); - } catch (ProcessingException e) { - //IO problems during request - throw new AsdcCatalogException("Failed to get a response from SDC service. Cause: "+e.getMessage(), e); - } catch (WebApplicationException e) { - //Web service returned data, but the response status wasn't a good one (i.e. non 2xx) - throw new AsdcCatalogException(e); - } - } - -} - diff --git a/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java new file mode 100644 index 000000000..b4096f9b2 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/asdc/rest/SdcRestClient.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.asdc.rest; + +import com.att.eelf.configuration.EELFLogger; +import com.google.common.collect.ImmutableMap; +import io.vavr.control.Try; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.asdc.AsdcCatalogException; +import org.onap.vid.asdc.AsdcClient; +import org.onap.vid.asdc.beans.Service; +import org.onap.vid.client.SyncRestClientInterface; +import org.onap.vid.model.ModelConstants; +import org.onap.vid.properties.VidProperties; +import org.onap.vid.utils.Logging; +import org.springframework.http.HttpMethod; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; +import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; +import static org.onap.portalsdk.core.util.SystemProperties.APP_DISPLAY_NAME; +import static org.onap.vid.asdc.AsdcClient.URIS.METADATA_URL_TEMPLATE; +import static org.onap.vid.asdc.AsdcClient.URIS.TOSCA_MODEL_URL_TEMPLATE; +import static org.onap.vid.client.SyncRestClientInterface.HEADERS.AUTHORIZATION; +import static org.onap.vid.client.SyncRestClientInterface.HEADERS.CONTENT_TYPE; +import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID; +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; +import static org.onap.vid.utils.Logging.logRequest; + +public class SdcRestClient implements AsdcClient { + + private String baseUrl; + private String path; + private String auth; + private static final EELFLogger LOGGER = Logging.getRequestsLogger("asdc"); + + private SyncRestClientInterface syncRestClient; + + + public SdcRestClient(String baseUrl, String auth, SyncRestClientInterface client) { + this.syncRestClient = client; + this.auth = auth; + this.baseUrl = baseUrl; + this.path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH); + } + + + @Override + public Service getService(UUID uuid) throws AsdcCatalogException { + String finalUrl = String.format(METADATA_URL_TEMPLATE, baseUrl, path, uuid); + logRequest(LOGGER, HttpMethod.GET, finalUrl); + + return Try + .of(() -> syncRestClient.get(finalUrl, prepareHeaders(auth, APPLICATION_JSON), Collections.emptyMap(), Service.class)) + .getOrElseThrow(AsdcCatalogException::new) + .getBody(); + + } + + @Override + public Path getServiceToscaModel(UUID uuid) throws AsdcCatalogException { + String finalUrl = String.format(TOSCA_MODEL_URL_TEMPLATE, baseUrl, path, uuid); + logRequest(LOGGER, HttpMethod.GET, finalUrl); + + InputStream inputStream = Try + .of(() -> syncRestClient.getStream(finalUrl, prepareHeaders(auth, APPLICATION_OCTET_STREAM), Collections.emptyMap())) + .getOrElseThrow(AsdcCatalogException::new) + .getBody(); + + return createTmpFile(inputStream); + } + + + private Map<String, String> prepareHeaders(String auth, String contentType) { + return ImmutableMap.of( + X_ECOMP_INSTANCE_ID, SystemProperties.getProperty(APP_DISPLAY_NAME), + AUTHORIZATION, auth, + REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId(), + CONTENT_TYPE, contentType + ); + } + + private Path createTmpFile(InputStream csarInputStream) throws AsdcCatalogException { + return Try + .of(() -> tryToCreateTmpFile(csarInputStream)) + .getOrElseThrow(throwable -> new AsdcCatalogException("Caught IOException while creating CSAR", throwable)); + } + + private Path tryToCreateTmpFile(InputStream csarInputStream) throws IOException { + Path csarFile = Files.createTempFile("csar", ".zip"); + Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING); + + LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath()); + + return csarFile; + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClientInterface.java b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClientInterface.java index 80663d65b..142adde15 100644 --- a/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClientInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/client/SyncRestClientInterface.java @@ -2,16 +2,22 @@ package org.onap.vid.client; import io.joshworks.restclient.http.HttpResponse; import io.joshworks.restclient.http.JsonNode; + import java.io.InputStream; import java.util.Map; public interface SyncRestClientInterface { + class HEADERS { + public static final String CONTENT_TYPE = "Content-Type"; + public static final String AUTHORIZATION = "Authorization"; + public static final String X_ECOMP_INSTANCE_ID = "X-ECOMP-InstanceID"; + } HttpResponse<JsonNode> post(String url, Map<String, String> headers, Object body); <T> HttpResponse<T> post(String url, Map<String, String> headers, Object body, Class<T> aClass); - HttpResponse<JsonNode> get(String url, Map<String, String> headers, Map<String, String> routeParams); + HttpResponse<JsonNode> get(String url, Map<String, String> headers, Map<String, String> routeParams); <T> HttpResponse<T> get(String url, Map<String, String> headers, Map<String, String> routeParams, Class<T> aClass); @@ -19,9 +25,9 @@ public interface SyncRestClientInterface { HttpResponse<JsonNode> put(String url, Map<String, String> headers, Object body); - <T> HttpResponse<T> put(String url, Map<String, String> headers, Object body, Class<T> aClass); + <T> HttpResponse<T> put(String url, Map<String, String> headers, Object body, Class<T> aClass); - <T> HttpResponse<T> delete(String url, Map<String, String> headers, Class<T> aClass); + <T> HttpResponse<T> delete(String url, Map<String, String> headers, Class<T> aClass); HttpResponse<JsonNode> delete(String url, Map<String, String> headers); diff --git a/vid-app-common/src/main/java/org/onap/vid/controllers/WebConfig.java b/vid-app-common/src/main/java/org/onap/vid/controllers/WebConfig.java index cf32e92e0..0f4b536a1 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controllers/WebConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controllers/WebConfig.java @@ -22,14 +22,30 @@ package org.onap.vid.controllers; import com.fasterxml.jackson.databind.ObjectMapper; -import org.onap.vid.aai.*; +import org.onap.vid.aai.AaiClient; +import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.aai.AaiResponseTranslator; +import org.onap.vid.aai.PombaClientImpl; +import org.onap.vid.aai.PombaClientInterface; +import org.onap.vid.aai.PombaRestInterface; import org.onap.vid.aai.model.PortDetailsTranslator; -import org.onap.vid.aai.util.*; +import org.onap.vid.aai.util.AAIRestInterface; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.aai.util.SSLContextProvider; +import org.onap.vid.aai.util.ServletRequestHelper; +import org.onap.vid.aai.util.SystemPropertyHelper; import org.onap.vid.asdc.AsdcClient; import org.onap.vid.asdc.parser.ToscaParserImpl2; -import org.onap.vid.asdc.rest.RestfulAsdcClient; -import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.asdc.rest.SdcRestClient; +import org.onap.vid.client.SyncRestClient; +import org.onap.vid.client.SyncRestClientInterface; import org.onap.vid.properties.AsdcClientConfiguration; +import org.onap.vid.services.AaiService; +import org.onap.vid.services.AaiServiceImpl; +import org.onap.vid.services.PombaService; +import org.onap.vid.services.PombaServiceImpl; +import org.onap.vid.services.VidService; +import org.onap.vid.services.VidServiceImpl; import org.onap.vid.scheduler.SchedulerRestInterface; import org.onap.vid.scheduler.SchedulerRestInterfaceIfc; import org.onap.vid.services.*; @@ -38,15 +54,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.togglz.core.manager.FeatureManager; -import javax.net.ssl.SSLContext; import javax.servlet.ServletContext; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; import java.io.File; -import java.net.URI; -import java.net.URISyntaxException; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; @Configuration public class WebConfig { @@ -119,35 +128,18 @@ public class WebConfig { } @Bean - public AsdcClient asdcClient(AsdcClientConfiguration asdcClientConfig) { - - final String protocol = asdcClientConfig.getAsdcClientProtocol(); - final String host = asdcClientConfig.getAsdcClientHost(); - final int port = asdcClientConfig.getAsdcClientPort(); - final String auth = asdcClientConfig.getAsdcClientAuth(); - Client cl = null; - if (protocol.equalsIgnoreCase("https")) { - try { - SSLContext ctx = SSLContext.getInstance("TLSv1.2"); - ctx.init(null, null, null); - cl = ClientBuilder.newBuilder().sslContext(ctx).build(); - } catch (NoSuchAlgorithmException n) { - throw new GenericUncheckedException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n); - } catch (KeyManagementException k) { - throw new GenericUncheckedException("SDC Client could not be instantiated due to a key management exception", k); - } - } else { - cl = ClientBuilder.newBuilder().build(); - } - - try { - final URI uri = new URI(protocol + "://" + host + ":" + port + "/"); - return new RestfulAsdcClient.Builder(cl, uri) - .auth(auth) - .build(); - } catch (URISyntaxException e) { - throw new GenericUncheckedException("SDC Client could not be instantiated due to a syntax error in the URI", e); - } + public AsdcClient sdcClient(AsdcClientConfiguration asdcClientConfiguration, SyncRestClientInterface syncRestClient) { + String auth = asdcClientConfiguration.getAsdcClientAuth(); + String host = asdcClientConfiguration.getAsdcClientHost(); + String protocol = asdcClientConfiguration.getAsdcClientProtocol(); + int port = asdcClientConfiguration.getAsdcClientPort(); + + return new SdcRestClient(protocol + "://" + host + ":" + port + "/", auth, syncRestClient); + } + + @Bean + public SyncRestClientInterface syncRestClient() { + return new SyncRestClient(); } @Bean |