X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=sdnc-a1-controller%2Fnorthbound%2Fnonrt-ric-api%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fo_ran_sc%2Fnonrtric%2Fsdnc_a1%2Fnorthbound%2Frestadapter%2FRestAdapterImpl.java;fp=sdnc-a1-controller%2Fnorthbound%2Fnonrt-ric-api%2Fprovider%2Fsrc%2Fmain%2Fjava%2Forg%2Fo_ran_sc%2Fnonrtric%2Fsdnc_a1%2Fnorthbound%2Frestadapter%2FRestAdapterImpl.java;h=0000000000000000000000000000000000000000;hb=b2ef1d832b94af02c7a3acefb4c8c5e4db03242b;hp=d2e602ffbe4017e6a812d2faa02c91d6d9b0a271;hpb=dfff24a65d219ce3985edbf744b034d1882e452f;p=nonrtric.git diff --git a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImpl.java b/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImpl.java deleted file mode 100644 index d2e602ff..00000000 --- a/sdnc-a1-controller/northbound/nonrt-ric-api/provider/src/main/java/org/o_ran_sc/nonrtric/sdnc_a1/northbound/restadapter/RestAdapterImpl.java +++ /dev/null @@ -1,145 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Properties; -import org.apache.http.client.HttpClient; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.TrustAllStrategy; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.SSLContextBuilder; -import org.apache.http.ssl.SSLContexts; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.util.ResourceUtils; -import org.springframework.web.client.RestTemplate; - -/** - * This class provides the Generic Rest Adapter interface to the RestTemplate - * - * @author lathishbabu.ganesan@est.tech - * - */ - -public class RestAdapterImpl implements RestAdapter { - - private final Logger log = LoggerFactory.getLogger(RestAdapterImpl.class); - - private RestTemplate restTemplateHttp; - private RestTemplate restTemplateHttps; - - public RestAdapterImpl() { - restTemplateHttp = new RestTemplate(); - try { - restTemplateHttps = createRestTemplateForHttps(); - } catch (IOException | UnrecoverableKeyException | KeyManagementException | CertificateException - | NoSuchAlgorithmException | KeyStoreException ex) { - log.error("Caught exception when trying to create restTemplateHttps: {}", ex.getMessage()); - } - } - - private RestTemplate createRestTemplateForHttps() throws IOException, UnrecoverableKeyException, CertificateException, - NoSuchAlgorithmException, KeyStoreException, KeyManagementException { - try (InputStream inputStream = new FileInputStream(ResourceUtils.getFile("/opt/onap/sdnc/data/properties/https-props.properties"))) { - Properties properties = new Properties(); - properties.load(inputStream); - final String keyPassword = properties.getProperty("key-password"); - final String keystorePassword = properties.getProperty("keystore-password"); - final String truststorePassword = properties.getProperty("truststore-password"); - final boolean isTrustStoreUsed = Boolean.parseBoolean(properties.getProperty("isTrustStoreUsed")); - SSLContextBuilder builder = SSLContexts.custom() - .loadKeyMaterial(ResourceUtils.getFile(properties.getProperty("key-store")), - keystorePassword.toCharArray(), keyPassword.toCharArray()); - if (isTrustStoreUsed) { - builder.loadTrustMaterial(ResourceUtils.getFile(properties.getProperty("trust-store")), - truststorePassword.toCharArray()); - } else { - builder.loadTrustMaterial(null, new TrustAllStrategy()); - } - SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE); - HttpClient client = HttpClients.custom().setSSLSocketFactory(scsf).build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); - requestFactory.setHttpClient(client); - return new RestTemplate(requestFactory); - } - } - - private HttpEntity getHttpEntity(final Object object) { - return new HttpEntity<>(object); - } - - @Override - public ResponseEntity get(String uri, Class clazz) { - HttpEntity entity = getHttpEntity(null); - return invokeHttpRequest(uri, HttpMethod.GET, clazz, entity); - } - - @Override - public ResponseEntity put(String uri, String body, Class clazz) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - HttpEntity entity = new HttpEntity<>(body, headers); - return invokeHttpRequest(uri, HttpMethod.PUT, clazz, entity); - } - - @Override - public ResponseEntity delete(String uri) { - HttpEntity entity = getHttpEntity(null); - return invokeHttpRequest(uri, HttpMethod.DELETE, null, entity); - } - - @SuppressWarnings("unchecked") - private ResponseEntity invokeHttpRequest(String uri, HttpMethod httpMethod, Class clazz, - HttpEntity entity) { - try { - URL url = new URL(uri); - if (url.getProtocol().equals("https")) { - return (ResponseEntity) restTemplateHttps.exchange(uri, httpMethod, entity, clazz); - } else if (url.getProtocol().equals("http")) { - return (ResponseEntity) restTemplateHttp.exchange(uri, httpMethod, entity, clazz); - } else { - log.error("Invalid protocol in URL"); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - } catch (MalformedURLException ex) { - log.error("URL is not valid, exception: {}", ex.getMessage()); - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - } -} \ No newline at end of file