From 6ad41e3ccd398a2721f41ad61c80b7bb03f7d127 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Mon, 31 Dec 2018 17:21:27 +0200 Subject: Merge from ECOMP's repository Main Features -------------- - Async-Instantiation jobs mechanism major update; still WIP (package `org.onap.vid.job`) - New features in View/Edit: Activate fabric configuration; show related networks; soft delete - Support AAI service-tree traversal (`AAIServiceTree`) - In-memory cache for SDC models and certain A&AI queries (`CacheProviderWithLoadingCache`) - Upgrade TOSCA Parser and add parsing options; fix malformed TOSCA models - Resolve Cloud-Owner values for MSO - Pass X-ONAP headers to MSO Infrastructure -------------- - Remove codehaus' jackson mapper; use soley fasterxml 2.9.7 - Surefire invokes both TestNG and JUnit tests - Support Kotlin source files - AaiController2 which handles errors in a "Spring manner" - Inline generated-sources and remove jsonschema2pojo Quality -------- - Cumulative bug fixes (A&AI API, UI timeouts, and many more) - Many Sonar issues cleaned-up - Some unused classes removed - Minor changes in vid-automation project, allowing some API verification to run Hard Merges ------------ - HTTP Clients (MSO, A&AI, WebConfig, OutgoingRequestHeadersTest) - Moved `package org.onap.vid.controllers` to `controller`, without plural -- just to keep semantic sync with ECOMP. Reference commit in ECOMP: 3d1141625 Issue-ID: VID-378 Change-Id: I9c8d1e74caa41815891d441fc0760bb5f29c5788 Signed-off-by: Ittay Stern --- .../org/onap/vid/aai/util/AAIRestInterface.java | 108 +++++++++---------- .../org/onap/vid/aai/util/AAITreeConverter.java | 89 ++++++++++++++++ .../main/java/org/onap/vid/aai/util/CacheConfig.kt | 52 +++++++++ .../java/org/onap/vid/aai/util/CacheProvider.java | 37 +++++++ .../aai/util/CacheProviderWithLoadingCache.java | 100 ++++++++++++++++++ .../org/onap/vid/aai/util/HttpsAuthClient.java | 28 +++-- .../onap/vid/aai/util/HttpsComponentsClient.java | 117 --------------------- .../JettyObfuscationConversionCommandLineUtil.java | 4 +- .../vid/aai/util/ServiceInstanceStandardQuery.java | 93 ++++++++++++++++ .../onap/vid/aai/util/SystemPropertyHelper.java | 11 ++ 10 files changed, 455 insertions(+), 184 deletions(-) create mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java create mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/CacheConfig.kt create mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProvider.java create mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProviderWithLoadingCache.java delete mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsComponentsClient.java create mode 100644 vid-app-common/src/main/java/org/onap/vid/aai/util/ServiceInstanceStandardQuery.java (limited to 'vid-app-common/src/main/java/org/onap/vid/aai/util') diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java index f5625ce21..f4401ab3a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java @@ -22,12 +22,14 @@ package org.onap.vid.aai.util; import com.att.eelf.configuration.EELFLogger; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.vid.aai.ExceptionWithRequestInfo; import org.onap.vid.aai.ResponseWithRequestInfo; import org.onap.vid.aai.exceptions.InvalidPropertyException; import org.onap.vid.utils.Logging; +import org.onap.vid.utils.Unchecked; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; @@ -37,6 +39,7 @@ import javax.ws.rs.client.Invocation; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.net.URLEncoder; import java.util.Optional; import java.util.UUID; @@ -55,7 +58,6 @@ public class AAIRestInterface { protected final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("aai"); - /** The client. */ private Client client = null; @@ -105,9 +107,9 @@ public class AAIRestInterface { return URLEncoder.encode(nodeKey, "UTF-8").replaceAll("\\+", "%20"); } - private void initRestClient() { - initRestClient(false); - } + protected void initRestClient() { + initRestClient(false); + } private void initRestClient(boolean propagateExceptions) { @@ -160,47 +162,61 @@ public class AAIRestInterface { * @param xml the xml * @return the string */ - public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml) { + public ResponseWithRequestInfo RestGet(String fromAppId, String transId, URI requestUri, boolean xml) { return RestGet(fromAppId, transId, requestUri, xml, false); } - public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml, boolean propagateExceptions) { - String methodName = "RestGet"; - String url = systemPropertyHelper.getFullServicePath(requestUri); + public ResponseWithRequestInfo RestGet(String fromAppId, String transId, URI requestUri, boolean xml, boolean propagateExceptions) { + return doRest(fromAppId, transId, requestUri, null, HttpMethod.GET, xml, propagateExceptions); + } + + public ResponseWithRequestInfo doRest(String fromAppId, String transId, URI requestUri, String payload, HttpMethod method, boolean xml, boolean propagateExceptions) { + String url = null; + String methodName = "Rest"+method.name(); try { + + url = systemPropertyHelper.getFullServicePath(requestUri); + initRestClient(propagateExceptions); logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); logger.debug(EELFLoggerDelegate.debugLogger, url + " for the get REST API"); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); + Logging.logRequest(outgoingRequestsLogger, method, url, payload); final Response response; - Invocation.Builder requestBuilder = client.target(url) - .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) - .header(TRANSACTION_ID_HEADER, transId) - .header(FROM_APP_ID_HEADER, fromAppId) - .header("Content-Type", MediaType.APPLICATION_JSON) - .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()); - response = systemPropertyHelper.isClientCertEnabled() ? - requestBuilder.get() : authenticateRequest(requestBuilder).get(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); - - if (response.getStatusInfo().equals(Response.Status.OK)) { + Invocation.Builder requestBuilder = client.target(url) + .request() + .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) + .header(TRANSACTION_ID_HEADER, transId) + .header(FROM_APP_ID_HEADER, fromAppId) + .header("Content-Type", MediaType.APPLICATION_JSON) + .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()); + + requestBuilder = systemPropertyHelper.isClientCertEnabled() ? + requestBuilder : authenticateRequest(requestBuilder); + + Invocation restInvocation = StringUtils.isEmpty(payload) ? + requestBuilder.build(method.name()) : + requestBuilder.build(method.name(), Entity.entity(payload, MediaType.APPLICATION_JSON)); + + response = restInvocation.invoke(); + Logging.logResponse(outgoingRequestsLogger, method, url, response); + + if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { logger.debug(EELFLoggerDelegate.debugLogger, methodName + SUCCESSFUL_API_MESSAGE); logger.info(EELFLoggerDelegate.errorLogger, methodName + SUCCESSFUL_API_MESSAGE); } else { logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response)); } - return new ResponseWithRequestInfo(response, url, HttpMethod.GET); + return new ResponseWithRequestInfo(response, url, method); } catch (Exception e) { logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e)); if (propagateExceptions) { - throw new ExceptionWithRequestInfo(HttpMethod.GET, defaultIfNull(url, requestUri), e); - } else { - return new ResponseWithRequestInfo(null, url, HttpMethod.GET); - } + throw new ExceptionWithRequestInfo(method, defaultIfNull(url, requestUri.toASCIIString()), e); + } else { + return new ResponseWithRequestInfo(null, url, method); + } } } @@ -222,7 +238,7 @@ public class AAIRestInterface { transId += ":" + UUID.randomUUID().toString(); logger.debug(methodName + START_STRING); Boolean response = false; - String url = systemPropertyHelper.getFullServicePath(path);; + String url = systemPropertyHelper.getFullServicePath(path); try { initRestClient(); @@ -263,37 +279,11 @@ public class AAIRestInterface { * @param path the path * @param payload the payload * @param xml the xml + * @param propagateExceptions * @return the string */ - public Response RestPut(String fromAppId, String path, String payload, boolean xml) { - String methodName = "RestPut"; - String url=systemPropertyHelper.getFullServicePath(path); - String transId = UUID.randomUUID().toString(); - logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); - - Response response = null; - try { - initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, payload); - response = authenticateRequest(client.target(url) - .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) - .header(TRANSACTION_ID_HEADER, transId) - .header(FROM_APP_ID_HEADER, fromAppId)) - .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) - .put(Entity.entity(payload, MediaType.APPLICATION_JSON)); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); - - if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { - logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName)); - logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName)); - } else { - logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response)); - } - } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e)); - } - return response; + public ResponseWithRequestInfo RestPut(String fromAppId, String path, String payload, boolean xml, boolean propagateExceptions) { + return doRest(fromAppId, UUID.randomUUID().toString(), Unchecked.toURI(path), payload, HttpMethod.PUT, xml, propagateExceptions); } @@ -313,13 +303,13 @@ public class AAIRestInterface { String transId = UUID.randomUUID().toString(); logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); - Response response = null; + Response response = null; try { initRestClient(); Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); - response = authenticateRequest(client.target(systemPropertyHelper.getServiceBasePath(path)) + response = authenticateRequest(client.target(url) .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) + .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) .header(TRANSACTION_ID_HEADER, transId) .header(FROM_APP_ID_HEADER, fromAppId)) .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java new file mode 100644 index 000000000..8fa6f6c6d --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAITreeConverter.java @@ -0,0 +1,89 @@ +package org.onap.vid.aai.util; + +import org.apache.commons.lang3.StringUtils; +import org.onap.vid.model.aaiTree.*; +import org.onap.vid.mso.model.ModelInfo; +import org.onap.vid.services.AAITreeNodeBuilder; +import org.springframework.stereotype.Component; + +import java.util.Objects; + +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE; + +@Component +public class AAITreeConverter { + + public static final String VNF_TYPE = "vnf-type"; + public static final String NETWORK_TYPE = "network-type"; + + public static final String IS_BASE_VF_MODULE = "is-base-vf-module"; + + public enum ModelType { + service, + vnf, + network, + instanceGroup, + vfModule + } + + public ServiceInstance convertTreeToUIModel(AAITreeNode rootNode, String globalCustomerId, String serviceType, String instantiationType) { + ServiceInstance serviceInstance = new ServiceInstance(); + serviceInstance.setInstanceId(rootNode.getId()); + serviceInstance.setInstanceName(rootNode.getName()); + serviceInstance.setOrchStatus(rootNode.getOrchestrationStatus()); + serviceInstance.setGlobalSubscriberId(globalCustomerId); + serviceInstance.setSubscriptionServiceType(serviceType); + serviceInstance.setIsALaCarte(StringUtils.equals(instantiationType, A_LA_CARTE)); + + serviceInstance.setModelInfo(createModelInfo(rootNode, ModelType.service)); + + //set children: vnf, network,group + rootNode.getChildren().forEach(child -> { + if (child.getType().equals(AAITreeNodeBuilder.GENERIC_VNF)) { + serviceInstance.getVnfs().put(child.getUniqueNodeKey(), Vnf.from(child)); + } else if (child.getType().equals(AAITreeNodeBuilder.NETWORK)) { + serviceInstance.getNetworks().put(child.getUniqueNodeKey(), Network.from(child)); + } else if (child.getType().equals(AAITreeNodeBuilder.INSTANCE_GROUP)) { + serviceInstance.getVnfGroups().put(child.getUniqueNodeKey(), VnfGroup.from(child)); + } + }); + + serviceInstance.setExistingVNFCounterMap( + serviceInstance.getVnfs().entrySet().stream() + .map(k -> k.getValue().getModelInfo().getModelVersionId()) + .collect(groupingBy(identity(), counting())) + ); + + serviceInstance.setExistingNetworksCounterMap( + serviceInstance.getNetworks().entrySet().stream() + .map(k -> k.getValue().getModelInfo().getModelVersionId()) + .filter(Objects::nonNull) + .collect(groupingBy(identity(), counting())) + ); + + + serviceInstance.setExistingVnfGroupCounterMap( + serviceInstance.getVnfGroups().entrySet().stream() + .map(k -> k.getValue().getModelInfo().getModelVersionId()) + .filter(Objects::nonNull) + .collect(groupingBy(identity(), counting())) + ); + + return serviceInstance; + } + + private static ModelInfo createModelInfo(AAITreeNode aaiNode, ModelType modelType) { + ModelInfo modelInfo = new ModelInfo(); + modelInfo.setModelType(modelType.name()); + modelInfo.setModelName(aaiNode.getModelName()); + modelInfo.setModelVersion(aaiNode.getModelVersion()); + modelInfo.setModelVersionId(aaiNode.getModelVersionId()); + modelInfo.setModelInvariantId(aaiNode.getModelInvariantId()); + modelInfo.setModelCustomizationId(aaiNode.getModelCustomizationId()); + + return modelInfo; + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheConfig.kt b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheConfig.kt new file mode 100644 index 000000000..0ff604144 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheConfig.kt @@ -0,0 +1,52 @@ +package org.onap.vid.aai.util + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.KotlinModule +import com.google.common.collect.ImmutableMap +import org.springframework.stereotype.Component + +//I use a regular kotlin class because I want that when jackson read +//a json with null values (or missing fields) they would get default values. +//for other cases it's better to use data class for POJO class +//for more information you can read here : +//https://github.com/FasterXML/jackson-module-kotlin/issues/130 +class CacheConfig constructor( + isActive: Boolean?, + expireAfterWriteHours: Long?, + refreshAfterWriteSeconds: Long?) { + val isActive: Boolean = isActive ?: true + val expireAfterWriteHours: Long = expireAfterWriteHours ?: 24L + val refreshAfterWriteSeconds: Long = refreshAfterWriteSeconds ?: 10L + + companion object { + val defaultCacheConfig = CacheConfig(null, null, null) + } + +} + + +interface CacheConfigProvider { + fun getCacheConfig(cacheName:String): CacheConfig +} + +@Component +class CacheConfigProviderImpl() : CacheConfigProvider { + private val mapper = ObjectMapper().apply { registerModule(KotlinModule()) } + + private fun readMapOfCacheConfig(): Map { + val configInputStream = CacheConfigProviderImpl::class.java.classLoader.getResourceAsStream("cacheConfig.json") + + return if (configInputStream == null) { + ImmutableMap.of() + } else { + mapper.readValue(configInputStream, object : TypeReference>() {}) + } + } + + override fun getCacheConfig(cacheName: String): CacheConfig { + return readMapOfCacheConfig()[cacheName] ?: CacheConfig.defaultCacheConfig + } +} + + diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProvider.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProvider.java new file mode 100644 index 000000000..63615f492 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProvider.java @@ -0,0 +1,37 @@ +package org.onap.vid.aai.util; + +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; + +public interface CacheProvider { + String KEY_DELIMITER = "!@#'"; + /* + Returns the cache associated with given name; creates one if wasn't any + */ + Cache aaiClientCacheFor(String name, Function loader); + + /* + reset cache if exist. Otherwise do nothing + */ + void resetCache(String name); + + interface Cache { + V get(K key); + } + + static String compileKey(List args) { + return compileKey(args.toArray(new String[0])); + } + + static String compileKey(String... args) { + return Stream.of(args).map(arg->defaultIfNull(arg, "")).collect( Collectors.joining( KEY_DELIMITER ) ); + } + + static String[] decompileKey(String key) { + return key.split(KEY_DELIMITER); + } +} diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProviderWithLoadingCache.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProviderWithLoadingCache.java new file mode 100644 index 000000000..26a3ebf25 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/CacheProviderWithLoadingCache.java @@ -0,0 +1,100 @@ +package org.onap.vid.aai.util; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.util.concurrent.UncheckedExecutionException; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.jetbrains.annotations.NotNull; +import org.onap.vid.properties.Features; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.togglz.core.manager.FeatureManager; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; + +import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; + +@Component +public class CacheProviderWithLoadingCache implements CacheProvider { + + private final ExecutorService cacheReloadPool; + private final FeatureManager featureManager; + private final CacheConfigProvider cacheConfigProvider; + private final ConcurrentHashMap caches; + + + @Autowired + public CacheProviderWithLoadingCache(FeatureManager featureManager, CacheConfigProvider cacheConfigProvider) { + this.featureManager = featureManager; + this.cacheConfigProvider = cacheConfigProvider; + this.cacheReloadPool = Executors.newFixedThreadPool(3); + this.caches = new ConcurrentHashMap<>(); + } + + /* + Returns the cache associated with given name; creates one if wasn't any + */ + @Override + public Cache aaiClientCacheFor(String name, Function loader) { + return (Cache) caches.computeIfAbsent(name, s -> buildAaiClientCacheFrom(loader, name)); + } + + @Override + public void resetCache(String name) { + caches.remove(name); + } + + /* + Creates and returns a Cache that use provided `loader` to fetch values for + search keys, and stores the result for reuse over a certain time. + The cache will not use any stored key if FLAG_1810_AAI_LOCAL_CACHE is turned off. + In that case, `loader` will be invoked for any `get()` request from the cache. The + cache adheres the flag in real-time; so no restart is required. + */ + protected Cache buildAaiClientCacheFrom(Function loader, String name) { + final LoadingCache activeCache = buildAaiClientActiveCacheFrom(loader, name); + + // this works because Cache interface has only a single method: "get()" + // can be replaced with new anonimous class; e.g.: + // return new Cache() { ... } + return key -> { + if (featureManager.isActive(Features.FLAG_1810_AAI_LOCAL_CACHE) && + defaultIfNull(cacheConfigProvider.getCacheConfig(name).isActive(), true)) { + try { + return activeCache.getUnchecked(key); + } + catch (UncheckedExecutionException exception) { + return ExceptionUtils.rethrow(exception.getCause()); + } + } else { + activeCache.invalidateAll(); + activeCache.cleanUp(); + return loader.apply(key); + } + }; + } + + private LoadingCache buildAaiClientActiveCacheFrom(Function loader, String name) { + return createCacheBuilder(name).build(createAsyncReloadingCacheLoaderFrom(loader)); + + } + + @NotNull + protected CacheBuilder createCacheBuilder(String name) { + CacheConfig cacheConfig = cacheConfigProvider.getCacheConfig(name); + return CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterWrite(cacheConfig.getExpireAfterWriteHours(), TimeUnit.HOURS) + .refreshAfterWrite(cacheConfig.getRefreshAfterWriteSeconds(), TimeUnit.SECONDS); + } + + private CacheLoader createAsyncReloadingCacheLoaderFrom(Function loader) { + return CacheLoader.asyncReloading(CacheLoader.from(loader::apply), cacheReloadPool); + } + +} diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsAuthClient.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsAuthClient.java index 15f81439b..489d2f1b6 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsAuthClient.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsAuthClient.java @@ -22,11 +22,16 @@ package org.onap.vid.aai.util; +import org.apache.http.conn.ssl.DefaultHostnameVerifier; +import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.HttpUrlConnectorProvider; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.vid.aai.exceptions.HttpClientBuilderException; +import org.onap.vid.properties.Features; +import org.togglz.core.manager.FeatureManager; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -47,16 +52,19 @@ public class HttpsAuthClient { private final SystemPropertyHelper systemPropertyHelper; private final SSLContextProvider sslContextProvider; - public HttpsAuthClient(String certFilePath, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider) { + public HttpsAuthClient(String certFilePath, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider, FeatureManager featureManager) { this.certFilePath = certFilePath; this.systemPropertyHelper = systemPropertyHelper; this.sslContextProvider = sslContextProvider; + this.featureManager = featureManager; } private final String certFilePath; + FeatureManager featureManager; + /** The logger. */ - static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsAuthClient.class); + static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(org.onap.vid.aai.util.HttpsAuthClient.class); /** @@ -70,7 +78,7 @@ public class HttpsAuthClient { try { setSystemProperties(); - ignoreHostname(); + optionallyVerifyHostname(); return systemPropertyHelper.isClientCertEnabled() ? getTrustedClient(config, getKeystorePath(), systemPropertyHelper.getDecryptedKeystorePassword(), mode) @@ -83,8 +91,8 @@ public class HttpsAuthClient { } - private void ignoreHostname() { - HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); + private void optionallyVerifyHostname() { + HttpsURLConnection.setDefaultHostnameVerifier(getHostnameVerifier()); } private Client getUntrustedClient(ClientConfig config) { @@ -94,12 +102,20 @@ public class HttpsAuthClient { private Client getTrustedClient(ClientConfig config, String keystorePath, String keystorePassword, HttpClientMode httpClientMode) throws HttpClientBuilderException { return ClientBuilder.newBuilder() .sslContext(sslContextProvider.getSslContext(keystorePath, keystorePassword, httpClientMode)) - .hostnameVerifier((s, sslSession) -> true) + .hostnameVerifier(getHostnameVerifier()) .withConfig(config) .build() .register(CustomJacksonJaxBJsonProvider.class); } + protected HostnameVerifier getHostnameVerifier() { + if(featureManager.isActive(Features.FLAG_EXP_USE_DEFAULT_HOST_NAME_VERIFIER)){ + return new DefaultHostnameVerifier(); + } + + return new NoopHostnameVerifier(); + } + private String getKeystorePath() { return getCertificatesPath() + FileSystems.getDefault().getSeparator() + systemPropertyHelper.getAAIKeystoreFilename(); } diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsComponentsClient.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsComponentsClient.java deleted file mode 100644 index d1f1cfc86..000000000 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsComponentsClient.java +++ /dev/null @@ -1,117 +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.aai.util; - -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLContextBuilder; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.eclipse.jetty.util.security.Password; -import org.onap.vid.exceptions.GenericUncheckedException; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.portalsdk.core.util.SystemProperties; - -import javax.net.ssl.SSLContext; -import java.io.FileInputStream; -import java.security.GeneralSecurityException; -import java.security.KeyManagementException; -import java.security.KeyStore; - - -/** - * The Class HttpsComponentsClient. - */ -public class HttpsComponentsClient{ - - static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HttpsComponentsClient.class); - - /** - * Gets the client. - * - * @param certFilePath the cert file path - * @return the client - * @throws KeyManagementException the key management exception - */ - public static CloseableHttpClient getClient(String certFilePath) { - CloseableHttpClient httpclient = null; - try { - - String truststore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_FILENAME); - String truststore_password = SystemProperties.getProperty(AAIProperties.AAI_TRUSTSTORE_PASSWD_X); - String decrypted_truststore_password = Password.deobfuscate(truststore_password); - String keystore_path = certFilePath + AAIProperties.FILESEPARTOR + SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_FILENAME); - String keystore_password = SystemProperties.getProperty(AAIProperties.AAI_KEYSTORE_PASSWD_X); - String decrypted_keystore_password = Password.deobfuscate(keystore_password); - - SSLContextBuilder sslContextB = new SSLContextBuilder(); - - KeyStore ks = KeyStore.getInstance("PKCS12"); - char[] pwd = decrypted_keystore_password.toCharArray(); - - try(FileInputStream fin = new FileInputStream(keystore_path)) { - ks.load(fin, pwd); - } - catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up keystore"); - logger.error(EELFLoggerDelegate.errorLogger, "Error loading keystore materials: (keystore path: {}, obfuascated keystore password: {})", keystore_path, keystore_password); - throw new GenericUncheckedException(e); - } - - sslContextB.loadKeyMaterial(ks, pwd); - - KeyStore ts = KeyStore.getInstance("JKS"); - char[] pwd1 = decrypted_truststore_password.toCharArray(); - - try(FileInputStream fin1 = new FileInputStream(truststore_path)) { - ts.load(fin1, pwd1); - } - catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger, "Error setting up truststore"); - logger.error(EELFLoggerDelegate.errorLogger, "Error loading truststore materials: (truststore path: {}, obfuascated truststore password: {})", truststore_path, truststore_password); - throw new GenericUncheckedException(e); - } - - sslContextB.loadTrustMaterial(ts); - sslContextB.loadKeyMaterial(ks, pwd); - sslContextB.useTLS(); - - SSLContext sslcontext = sslContextB.build(); - - SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory( - sslcontext, - new String[] { "TLSv1.1", "TLSv1.2" }, - null, - SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER ); - - httpclient = HttpClients.custom() - .setSSLSocketFactory(sslFactory) - .build(); - - - } catch (GeneralSecurityException e) { - throw new GenericUncheckedException(e); - } - return httpclient; - } - - - -} diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/JettyObfuscationConversionCommandLineUtil.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/JettyObfuscationConversionCommandLineUtil.java index cfc56d187..b39c20315 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/JettyObfuscationConversionCommandLineUtil.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/JettyObfuscationConversionCommandLineUtil.java @@ -48,8 +48,8 @@ public class JettyObfuscationConversionCommandLineUtil { System.out.println(encoded); } else if (cmd.hasOption("d")) { toProcess = cmd.getOptionValue("d"); - String decoded_str = Password.deobfuscate(toProcess); - System.out.println(decoded_str); + String decodedStr = Password.deobfuscate(toProcess); + System.out.println(decodedStr); } else { usage(); } diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/ServiceInstanceStandardQuery.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/ServiceInstanceStandardQuery.java new file mode 100644 index 000000000..22743bbf9 --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/ServiceInstanceStandardQuery.java @@ -0,0 +1,93 @@ +package org.onap.vid.aai.util; + +import com.google.common.collect.ImmutableMap; +import org.apache.commons.text.StrSubstitutor; +import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.*; +import org.onap.vid.aai.model.interfaces.AaiModelWithRelationships; +import org.onap.vid.utils.Multival; +import org.onap.vid.utils.Unchecked; + +import javax.inject.Inject; +import java.net.URI; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toSet; + +public class ServiceInstanceStandardQuery { + + private static final String SERVICE_INSTANCE_URI_TEMPLATE = "" + + "business/customers/customer/${global-customer-id}" + + "/service-subscriptions/service-subscription/${service-type}" + + "/service-instances/service-instance/${service-instance-id}"; + + private final AaiClientInterface aaiClient; + + @Inject + public ServiceInstanceStandardQuery(AaiClientInterface aaiClient) { + this.aaiClient = aaiClient; + } + + public ServiceInstance fetchServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId) { + final String serviceInstanceUri = getServiceInstanceUri(globalCustomerId, serviceType, serviceInstanceId); + + return fetchServiceInstance(Unchecked.toURI(serviceInstanceUri)); + } + + ServiceInstance fetchServiceInstance(URI serviceInstanceUri) { + return objectByUri(ServiceInstance.class, serviceInstanceUri); + } + + protected T objectByUri(Class clazz, URI aaiResourceUri) { + return aaiClient.typedAaiGet(aaiResourceUri, clazz); + } + + public Multival fetchRelatedVnfs(ServiceInstance serviceInstance) { + return fetchRelated("service", serviceInstance, "generic-vnf", Vnf.class); + } + + public Multival fetchRelatedL3Networks(String sourceType, K source) { + return fetchRelated(sourceType, source, "l3-network", Network.class); + } + + public Multival fetchRelatedVlanTags(Network network) { + return fetchRelated("network", network, "vlan-tag", Vlan.class); + } + + private String getServiceInstanceUri(String globalCustomerId, String serviceType, String serviceInstanceId) { + return new StrSubstitutor(ImmutableMap.of( + "global-customer-id", globalCustomerId, + "service-type", serviceType, + "service-instance-id", serviceInstanceId + )).replace(SERVICE_INSTANCE_URI_TEMPLATE); + } + + private Multival fetchRelated(String sourceType, K source, String destType, Class destClass) { + return Multival.of( + sourceType, + source, + destType, + fetchRelatedInner(source, destType, destClass) + ); + } + + private Set fetchRelatedInner(K source, String destType, Class destClass) { + return getURIsOf(source, relationship -> relatedTo(relationship, destType)) + .map(destUri -> objectByUri(destClass, destUri)) + .collect(toSet()); + } + + protected Stream getURIsOf(AaiModelWithRelationships aaiModel, Predicate predicate) { + return aaiModel.getRelationshipList().getRelationship().stream() + .filter(predicate) + .map(r -> r.relatedLink) + .map(Unchecked::toURI); + } + + protected static boolean relatedTo(Relationship r, String relationshipName) { + return relationshipName.equals(r.getRelatedTo()); + } + +} diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/SystemPropertyHelper.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/SystemPropertyHelper.java index 4d43d3ba2..939bfe5b6 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/SystemPropertyHelper.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/SystemPropertyHelper.java @@ -23,8 +23,10 @@ package org.onap.vid.aai.util; import org.eclipse.jetty.util.security.Password; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.exceptions.InvalidPropertyException; +import org.onap.vid.utils.Unchecked; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.util.Base64; import java.util.Optional; @@ -74,6 +76,15 @@ public class SystemPropertyHelper { return getAAIServerUrl().orElse("") + path; } + public String getFullServicePath(URI requestUri) { + // resolve() will merge two paths, handling the restiveness: + // Especially if requestUri starts with a '/' -- result will be + // AAI_SERVER_URL host, post, etc., and the path will be just + // requestUri. + return Unchecked.toURI(getAAIServerUrl().orElse("")) + .resolve(requestUri).toASCIIString(); + } + public String getServiceBasePath(String path) { return getAAIServerBaseUrl().orElse("") + path; } -- cgit 1.2.3-korg