diff options
Diffstat (limited to 'ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java')
-rw-r--r-- | ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java | 258 |
1 files changed, 121 insertions, 137 deletions
diff --git a/ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java b/ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java index cee94df..eb4d87f 100644 --- a/ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java +++ b/ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/client/RestClient.java @@ -20,60 +20,57 @@ package org.onap.ransim.rest.client; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.eclipse.persistence.internal.oxm.conversion.Base64; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; - +import java.io.*; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import javax.net.ssl.HttpsURLConnection; import java.net.URL; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLSession; -import org.slf4j.LoggerFactory; - +import java.nio.charset.Charset; import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.List; +import java.util.Map; + +import javax.net.ssl.*; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; -import javax.net.ssl.*; -import java.io.*; -import java.security.KeyStore; -import java.security.MessageDigest; -import java.security.cert.CertificateException; -import org.apache.http.client.HttpClient; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + 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.SSLContextBuilder; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; +import org.apache.log4j.Logger; +import org.eclipse.persistence.internal.oxm.conversion.Base64; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.apache.http.impl.client.HttpClientBuilder; +import org.springframework.web.client.RestTemplate; public class RestClient { -private static class NullHostnameVerifier implements HostnameVerifier { - public boolean verify(String hostname, SSLSession session) { - return true; - } + private static class NullHostnameVerifier implements HostnameVerifier { + public boolean verify(String hostname, SSLSession session) { + return true; } - private static class SavingTrustManager implements X509TrustManager { + } + private static class SavingTrustManager implements X509TrustManager { private final X509TrustManager tm; private X509Certificate[] chain; @@ -85,16 +82,14 @@ private static class NullHostnameVerifier implements HostnameVerifier { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; - + } - public void checkClientTrusted(X509Certificate[] chain, String authType) - throws CertificateException { + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { throw new UnsupportedOperationException(); } - public void checkServerTrusted(X509Certificate[] chain, String authType) - throws CertificateException { + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { this.chain = chain; tm.checkServerTrusted(chain, authType); } @@ -120,142 +115,131 @@ private static class NullHostnameVerifier implements HostnameVerifier { * Sends mount request to sdnr. * * @param serverId - * netconf server id name + * netconf server id name * @param ip - * server ip address + * server ip address * @param port - * port number + * port number * @param agentIp - * agent ip address + * agent ip address * @param agentPort - * agent port number + * agent port number * @param agentUsername - * agent username + * agent username * @param agentPassword - * agent password + * agent password * @return returns the message to be passed */ - - public String sendMountRequestToSdnr(String serverId, String ip, int port, String agentIp, String agentPort, + + public String sendMountRequestToSdnr(String serverId, String ip, int port, String agentIp, String agentPort, String agentUsername, String agentPassword) { + ResponseEntity<String> result = null; + try { + String requestBody = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\"> <node-id> " + serverId + + " </node-id> <username xmlns=\"urn:opendaylight:netconf-node-topology\">admin</username> <password xmlns=\"urn:opendaylight:netconf-node-topology\">admin</password> <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + + agentIp + "</host> <schema-cache-directory xmlns=\"urn:opendaylight:netconf-node-topology\">" + + serverId + "</schema-cache-directory> <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + + agentPort + + "</port> <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only> <schemaless xmlns=\"urn:opendaylight:netconf-node-topology\">false</schemaless> <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts> <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis> <default-request-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">60000</default-request-timeout-millis> <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.1</sleep-factor> <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis> <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema> <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">60</keepalive-delay> <concurrent-rpc-limit xmlns=\"urn:opendaylight:netconf-node-topology\">0</concurrent-rpc-limit> <actor-response-wait-time xmlns=\"urn:opendaylight:netconf-node-topology\">60</actor-response-wait-time></node>"; + + String response = ""; + HttpsURLConnection connection = null; + BufferedReader br = null; + log.info("Change in http to https"); + char[] passphrase; + String p = "changeit"; + passphrase = p.toCharArray(); + File file = new File("jssecacerts"); + if (file.isFile() == false) { + char SEP = File.separatorChar; + File dir = new File(SEP + "tmp" + SEP + "ransim-install" + SEP + "config"); + file = new File(dir, "jssecacerts"); + } + log.info("Loading new KeyStores" + file + "..."); + InputStream in = new FileInputStream(file); + KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); + ks.load(in, passphrase); + in.close(); + SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder() + .loadTrustMaterial(null, new TrustSelfSignedStrategy()).loadKeyMaterial(ks, passphrase).build(), + NoopHostnameVerifier.INSTANCE); - ResponseEntity<String> result=null; - try{ - String requestBody = "<node xmlns=\"urn:TBD:params:xml:ns:yang:network-topology\"> <node-id> " + serverId + " </node-id> <username xmlns=\"urn:opendaylight:netconf-node-topology\">admin</username> <password xmlns=\"urn:opendaylight:netconf-node-topology\">admin</password> <host xmlns=\"urn:opendaylight:netconf-node-topology\">" + agentIp + "</host> <schema-cache-directory xmlns=\"urn:opendaylight:netconf-node-topology\">" + serverId + "</schema-cache-directory> <port xmlns=\"urn:opendaylight:netconf-node-topology\">" + agentPort + "</port> <tcp-only xmlns=\"urn:opendaylight:netconf-node-topology\">false</tcp-only> <schemaless xmlns=\"urn:opendaylight:netconf-node-topology\">false</schemaless> <max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">0</max-connection-attempts> <connection-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">20000</connection-timeout-millis> <default-request-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">60000</default-request-timeout-millis> <sleep-factor xmlns=\"urn:opendaylight:netconf-node-topology\">1.1</sleep-factor> <between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis> <reconnect-on-changed-schema xmlns=\"urn:opendaylight:netconf-node-topology\">false</reconnect-on-changed-schema> <keepalive-delay xmlns=\"urn:opendaylight:netconf-node-topology\">60</keepalive-delay> <concurrent-rpc-limit xmlns=\"urn:opendaylight:netconf-node-topology\">0</concurrent-rpc-limit> <actor-response-wait-time xmlns=\"urn:opendaylight:netconf-node-topology\">60</actor-response-wait-time></node>"; + HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build(); + ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); - - String response = ""; - HttpsURLConnection connection = null; - BufferedReader br = null; - log.info("Change in http to https"); - char[] passphrase; - String p ="changeit"; - passphrase = p.toCharArray(); - File file = new File("jssecacerts"); - if (file.isFile() == false) { - char SEP = File.separatorChar; - File dir = new File( SEP - + "tmp" + SEP +"ransim-install"+ SEP + "config"); - file = new File(dir, "jssecacerts"); - } - log.info("Loading new KeyStores" + file + "..."); - InputStream in = new FileInputStream(file); - KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); - ks.load(in, passphrase); - in.close(); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( - new SSLContextBuilder() - .loadTrustMaterial(null, new TrustSelfSignedStrategy()) - .loadKeyMaterial(ks, passphrase) - .build(), - NoopHostnameVerifier.INSTANCE); - - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory( - socketFactory).build(); - - ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( - httpClient); - - SSLContext context = SSLContext.getInstance("TLS"); - TrustManagerFactory tmf = - TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); - tmf.init(ks); - X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0]; - SavingTrustManager tm = new SavingTrustManager(defaultTrustManager); - context.init(null, new TrustManager[]{tm}, null); - - SSLContext.setDefault(context); - SSLSocketFactory factory = context.getSocketFactory(); - - log.info("Using Authorization"); - - SSLSocket socket = (SSLSocket) factory.createSocket(ip,port); - socket.setSoTimeout(10000); - - try{ - socket.startHandshake(); - } - catch (SSLException e) { - - log.error("Exc insocket handshake", e); + SSLContext context = SSLContext.getInstance("TLS"); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + tmf.init(ks); + X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0]; + SavingTrustManager tm = new SavingTrustManager(defaultTrustManager); + context.init(null, new TrustManager[] {tm}, null); - } + SSLContext.setDefault(context); + SSLSocketFactory factory = context.getSocketFactory(); - log.info("Started SSL handshake without hostname verifier..."); - - RestTemplate restTemplate = new RestTemplate(requestFactory); - HttpClientBuilder httpClientBuilder = HttpClients.custom() - .setSSLContext(SSLContext.getDefault()) - .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE) - .useSystemProperties(); + log.info("Using Authorization"); - restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build())); + SSLSocket socket = (SSLSocket) factory.createSocket(ip, port); + socket.setSoTimeout(10000); + try { + socket.startHandshake(); + } catch (SSLException e) { - HttpHeaders headers = createHeaders(agentUsername, agentPassword); + log.error("Exc insocket handshake", e); - log.info("request : " + requestBody); - log.info("headers : " + headers); - for (Map.Entry<String, List<String>> entry : headers.entrySet()) { - log.info("Key:" + entry.getKey() + " , Value:" + entry.getValue()); - } - String url = "https://" + ip + ":" + port - + "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + serverId; - + } - HttpEntity<String> entity = new HttpEntity<String>(requestBody, headers); - result = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class); + log.info("Started SSL handshake without hostname verifier..."); - log.info("Request sent, result: " + result); - socket.close(); + RestTemplate restTemplate = new RestTemplate(requestFactory); + HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLContext(SSLContext.getDefault()) + .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).useSystemProperties(); + + restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build())); + + HttpHeaders headers = createHeaders(agentUsername, agentPassword); + + log.info("request : " + requestBody); + log.info("headers : " + headers); + for (Map.Entry<String, List<String>> entry : headers.entrySet()) { + log.info("Key:" + entry.getKey() + " , Value:" + entry.getValue()); } - catch (SSLException e) { - System.out.println(); - e.printStackTrace(System.out); - } + String url = "https://" + ip + ":" + port + + "/restconf/config/network-topology:network-topology/topology/topology-netconf/node/" + serverId; + + HttpEntity<String> entity = new HttpEntity<String>(requestBody, headers); + result = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class); - catch (Exception e) { + log.info("Request sent, result: " + result); + socket.close(); + } catch (SSLException e) { + System.out.println(); + e.printStackTrace(System.out); + } + + catch (Exception e) { - log.error("Exc in post {}", e); - } - return result.toString(); + log.error("Exc in post {}", e); + } + return result.toString(); } /** * Sends an unmount request to sdnr. * * @param serverId - * netconf server id name + * netconf server id name * @param ip - * ip address + * ip address * @param port - * port number + * port number * @param sdnrUsername - * sdnr username + * sdnr username * @param sdnrPassword - * sdnr password + * sdnr password * @return returns the message to be passed */ public String sendUnmountRequestToSdnr(String serverId, String ip, int port, String sdnrUsername, |