X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=nokiav2%2Fdriver%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fvfc%2Fnfvo%2Fdriver%2Fvnfm%2Fsvnfm%2Fnokia%2Fonap%2Fdirect%2FAAIRestApiProvider.java;h=14bdea11c7e893b596b424f4405f01ff9698ff0d;hb=refs%2Fchanges%2F59%2F38259%2F4;hp=141ba8471d20cf103706c6ae3bc404e7a3d2356f;hpb=6caea5a826752aa3177e0dbc2897923d247580a8;p=vfc%2Fnfvo%2Fdriver%2Fvnfm%2Fsvnfm.git diff --git a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/AAIRestApiProvider.java b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/AAIRestApiProvider.java index 141ba847..14bdea11 100644 --- a/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/AAIRestApiProvider.java +++ b/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/AAIRestApiProvider.java @@ -16,32 +16,20 @@ package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct; import com.google.common.annotations.VisibleForTesting; -import org.onap.aai.restclient.client.Headers; -import org.onap.aai.restclient.client.OperationResult; -import org.onap.aai.restclient.client.RestClient; -import org.onap.aai.restclient.enums.RestAuthenticationMode; +import okhttp3.Credentials; +import okhttp3.Request; +import org.onap.aai.ApiClient; +import org.onap.aai.api.CloudInfrastructureApi; +import org.onap.aai.api.ExternalSystemApi; +import org.onap.aai.api.NetworkApi; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions; -import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Conditional; -import org.springframework.http.MediaType; import org.springframework.stereotype.Component; -import javax.xml.bind.JAXBContext; -import java.io.ByteArrayOutputStream; -import java.io.StringReader; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; - -import static com.google.common.collect.Lists.newArrayList; -import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE; import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager.SERVICE_NAME; -import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure; -import static org.slf4j.LoggerFactory.getLogger; /** * Responsible for providing access to AAI APIs. @@ -50,118 +38,63 @@ import static org.slf4j.LoggerFactory.getLogger; @Component @Conditional(value = Conditions.UseForDirect.class) public class AAIRestApiProvider { - private static final String AAI_VERSION = "v11"; - private static Logger logger = getLogger(AAIRestApiProvider.class); private final MsbApiProvider msbApiProvider; + private final AaiSecurityProvider aaiSecurityProvider; @Value("${aaiUsername}") private String aaiUsername; @Value("${aaiPassword}") private String aaiPassword; @Autowired - AAIRestApiProvider(MsbApiProvider msbApiProvider) { + AAIRestApiProvider(MsbApiProvider msbApiProvider, AaiSecurityProvider aaiSecurityProvider) { this.msbApiProvider = msbApiProvider; + this.aaiSecurityProvider = aaiSecurityProvider; } /** - * @param logger the logger of the class that requests unmarshalling - * @param service the AAI service of the request - * @param url the URL of the request after the base URL (ex. /cloud-infrastructure/...) - * @param clazz the class of the result - * @param the type of the result - * @return the result of the GET request - */ - public T get(Logger logger, AAIService service, String url, Class clazz) { - return expectSuccess(logger, buildClient().get(getBaseUrl(service.getServiceName()) + url, buildCommonHeaders(), APPLICATION_XML_TYPE), clazz, url); - } - - /** - * @param logger the logger of the class that requests unmarshalling - * @param service the AAI service of the request - * @param url the URL of the request after the base URL (ex. /cloud-infrastructure/...) - * @param payload the payload of the request (non serialized) - * @param clazz the class of the result - * @param the type of the result - * @return the result of the PUT request + * @return API to access the cloud infrastructure */ - public T put(Logger logger, AAIService service, String url, S payload, Class clazz) { - String marshalledContent = marshall(payload); - OperationResult result = buildClient().put(getBaseUrl(service.getServiceName()) + url, marshalledContent, buildCommonHeaders(), APPLICATION_XML_TYPE, APPLICATION_XML_TYPE); - return expectSuccess(logger, result, clazz, url); + public CloudInfrastructureApi getCloudInfrastructureApi() { + return buildApiClient(AAIService.CLOUD).createService(CloudInfrastructureApi.class); } /** - * Execute a delete request on the given URL - * - * @param logger the logger of the class that requests unmarshalling - * @param service the AAI service of the request - * @param url the URL of the request after the base URL (ex. /cloud-infrastructure/...) + * @return API to access the external systems */ - public void delete(Logger logger, AAIService service, String url) { - buildClient().delete(getBaseUrl(service.getServiceName()) + url, buildCommonHeaders(), APPLICATION_XML_TYPE); + public ExternalSystemApi getExternalSystemApi() { + return buildApiClient(AAIService.ESR).createService(ExternalSystemApi.class); } /** - * @param serviceName the name of the AAI service on MSB - * @return the base URL of the service + * @return API to access the networking */ - private String getBaseUrl(String serviceName) { - return msbApiProvider.getMicroServiceUrl(serviceName, AAI_VERSION); - } + public NetworkApi getNetworkApi() { + return buildApiClient(AAIService.NETWORK).createService(NetworkApi.class); - private T expectSuccess(Logger logger, OperationResult result, Class clazz, String url) { - if (!result.wasSuccessful()) { - if (result.getResultCode() == 404) { - logger.debug("The resource at " + url + " does not exists"); - throw new NoSuchElementException("The resource at " + url + " does not exists"); - } - throw buildFatalFailure(logger, "Bad response. Code: " + result.getResultCode() + " cause: " + result.getFailureCause()); - } - if (clazz.isAssignableFrom(Void.class)) { - return null; - } - return unmarshal(result.getResult(), clazz); - } - - private T unmarshal(String content, Class clazz) { - try { - return (T) JAXBContext.newInstance(clazz).createUnmarshaller().unmarshal(new StringReader(content)); - } catch (Exception e) { - throw buildFatalFailure(logger, "Unable to unmarshal content", e); - } - } - - private String marshall(Object object) { - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - JAXBContext.newInstance(object.getClass()).createMarshaller().marshal(object, bos); - return bos.toString(); - } catch (Exception e) { - throw buildFatalFailure(logger, "Unable to marshal content", e); - } - } - - /** - * @return the common mandatory headers for AAI requests - */ - private Map> buildCommonHeaders() { - Map> headers = new HashMap<>(); - headers.put(Headers.ACCEPT, newArrayList(MediaType.APPLICATION_XML_VALUE)); - headers.put(Headers.FROM_APP_ID, newArrayList(SERVICE_NAME)); - return headers; - } - - - private RestClient buildClient() { - return buildRawClient().basicAuthUsername(aaiUsername).basicAuthPassword(aaiPassword).authenticationMode(RestAuthenticationMode.SSL_BASIC); } @VisibleForTesting - RestClient buildRawClient() { - return new RestClient(); + ApiClient buildApiClient(AAIService service) { + ApiClient apiClient = new ApiClient(); + apiClient.getOkBuilder().sslSocketFactory(aaiSecurityProvider.buildSSLSocketFactory(), aaiSecurityProvider.buildTrustManager()); + apiClient.getOkBuilder().hostnameVerifier(aaiSecurityProvider.buildHostnameVerifier()); + apiClient.getOkBuilder().addInterceptor(chain -> { + Request request = chain.request().newBuilder().addHeader("X-FromAppId", SERVICE_NAME).build(); + return chain.proceed(request); + }); + apiClient.getOkBuilder().authenticator((route, response) -> { + String credential = Credentials.basic(aaiUsername, aaiPassword); + return response.request().newBuilder().header("Authorization", credential).build(); + }); + String url = msbApiProvider.getMicroServiceUrl(service.getServiceName(), "v11"); + if (!url.endsWith("/")) { + url = url + "/"; + } + apiClient.getAdapterBuilder().baseUrl(url); + return apiClient; } - public enum AAIService { + enum AAIService { NETWORK { String getServiceName() { return "aai-network";