diff options
Diffstat (limited to 'holmes-actions/src/main')
3 files changed, 232 insertions, 47 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index 71472ed..36479d7 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -13,35 +13,21 @@ */ package org.onap.holmes.common.aai; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MultivaluedHashMap; -import org.glassfish.jersey.client.ClientConfig; +import java.util.HashMap; +import java.util.Map; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.HttpsUtils; public class AaiQuery { private AaiResponseUtil aaiResponseUtil; public VnfEntity getAaiVnfData(String vnfId, String vnfName) throws CorrelationException { - Client client = ClientBuilder.newClient(new ClientConfig()); - WebTarget webTarget = client - .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id=" - + vnfId); - String response = webTarget.request("application/json").headers(getHeaders()).get() - .readEntity(String.class); - if (response == null) { - webTarget = client - .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name=" - + vnfName); - response = webTarget.request("application/json").headers(getHeaders()).get() - .readEntity(String.class); - } + String response = getVnfDataResponse(vnfId, vnfName); try { return aaiResponseUtil.convertJsonToVnfEntity(response); } catch (Exception e) { @@ -50,11 +36,8 @@ public class AaiQuery { } public VmEntity getAaiVmData(String vserverId, String vserverName) throws CorrelationException { - Client client = ClientBuilder.newClient(new ClientConfig()); - String response = client - .target(MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(client, - vserverId, vserverName)).request("application/json").headers(getHeaders()) - .get().readEntity(String.class); + String url = MicroServiceConfig.getMsbServerAddr() + getVmResourceLinks(vserverId, vserverName); + String response = getResponse(url); try { return aaiResponseUtil.convertJsonToVmEntity(response); } catch (Exception e) { @@ -62,18 +45,8 @@ public class AaiQuery { } } - private String getVmResourceLinks(Client client, String vserverId, String vserverName) throws CorrelationException { - WebTarget webTarget = client - .target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR - + "vserver-id:EQUALS:" + vserverId); - String response = webTarget.request("application/json").headers(getHeaders()).get() - .readEntity(String.class); - if (response == null) { - webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR - + "vserver-name:EQUALS:" + vserverName); - response = webTarget.request("application/json").headers(getHeaders()).get() - .readEntity(String.class); - } + private String getVmResourceLinks(String vserverId, String vserverName) throws CorrelationException { + String response = getResourceLinksResponse(vserverId, vserverName); try { return aaiResponseUtil.convertJsonToVmResourceLink(response).get(0).getResourceLink(); } catch (Exception e) { @@ -81,11 +54,46 @@ public class AaiQuery { } } - private MultivaluedHashMap getHeaders() { - MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>(); - headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID); - headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID); - headers.add("Authorization", AaiConfig.getAuthenticationCredentials()); + private String getResourceLinksResponse(String vserverId, String vserverName) throws CorrelationException { + String url = + MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + "vserver-id:EQUALS:" + + vserverId; + String response = getResponse(url); + if (response.equals("")) { + url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VM_ADDR + + "vserver-name:EQUALS:" + vserverName; + response = getResponse(url); + } + return response; + } + + private String getVnfDataResponse(String vnfId, String vnfName) throws CorrelationException { + String url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-id=" + vnfId; + String response = getResponse(url); + if (response.equals("")) { + url = MicroServiceConfig.getMsbServerAddr() + AaiConfig.VNF_ADDR + "vnf-name=" + + vnfName; + response = getResponse(url); + } + return response; + } + + private String getResponse(String url) throws CorrelationException { + String response = ""; + try { + response = HttpsUtils.get(url, getHeaders()); + } catch (Exception e) { + throw new CorrelationException("Failed to get data from aai", e); + } + return response; + } + + private Map getHeaders() { + Map<String, String> headers = new HashMap<>(); + headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID); + headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID); + headers.put("Authorization", AaiConfig.getAuthenticationCredentials()); + headers.put("Accept", "application/json"); return headers; } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java index d0a4160..894b6f1 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/CorrelationUtil.java @@ -34,17 +34,17 @@ public class CorrelationUtil { return LazyHolder.INSTANCE; } - public boolean isTopologicallyRelated(String childId, String rootId) { + public boolean isTopologicallyRelated(String eventId, String sourceId, String sourceName) { - return Optional.ofNullable(getVmEntity(rootId)).map(vmEntity -> - getIsRelated(childId, vmEntity)).orElse(false); + return Optional.ofNullable(getVmEntity(sourceId, sourceName)).map(vmEntity -> + getIsRelated(eventId, vmEntity)).orElse(false); } - private boolean getIsRelated(String childId, VmEntity vmEntity) { + private boolean getIsRelated(String eventId, VmEntity vmEntity) { List<Relationship> relationships = vmEntity.getRelationshipList().getRelationships(); for (Relationship relationship : relationships) { boolean isRelated = relationship.getRelationshipDataList().stream().anyMatch( - relationshipData -> relationshipData.getRelationshipValue().equals(childId)); + relationshipData -> relationshipData.getRelationshipValue().equals(eventId)); if (isRelated) { return true; } @@ -52,10 +52,10 @@ public class CorrelationUtil { return false; } - private VmEntity getVmEntity(String rootId) { + private VmEntity getVmEntity(String sourceId, String sourceName) { VmEntity vmEntity = null; try { - vmEntity = aaiQuery.getAaiVmData("", rootId); + vmEntity = aaiQuery.getAaiVmData(sourceId, sourceName); } catch (CorrelationException e) { log.error("Failed to get vm data", e.getMessage()); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java new file mode 100644 index 0000000..3ef1201 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/HttpsUtils.java @@ -0,0 +1,177 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * 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. + */ + +package org.onap.holmes.common.utils; + +import java.io.IOException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.Consts; +import org.apache.http.HeaderIterator; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.ParseException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.TrustStrategy; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.util.EntityUtils; +import org.onap.holmes.common.exception.CorrelationException; + +@Slf4j +public class HttpsUtils { + private static final String HTTP = "http"; + private static final String HTTPS = "https"; + private static SSLConnectionSocketFactory sslConnectionSocketFactory = null; + private static PoolingHttpClientConnectionManager connectionManager = null; + private static SSLContextBuilder sslContextBuilder = null; + static { + try { + sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { + public boolean isTrusted(X509Certificate[] x509Certificates, String s) + throws CertificateException { + return true; + } + }); + sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), + new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, + NoopHostnameVerifier.INSTANCE); + Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() + .register(HTTP, new PlainConnectionSocketFactory()) + .register(HTTPS, sslConnectionSocketFactory) + .build(); + connectionManager = new PoolingHttpClientConnectionManager(registry); + connectionManager.setMaxTotal(200); + } catch (Exception e) { + log.error("Failed to init ssl builder" + e.getMessage()); + } + } + + public static String post(String url, Map<String, String> header, Map<String, String> param, + HttpEntity entity) throws Exception { + String result = ""; + CloseableHttpClient httpClient = null; + try { + httpClient = getHttpClient(); + HttpPost httpPost = new HttpPost(url); + if (!header.isEmpty()) { + for (Map.Entry<String, String> entry : header.entrySet()) { + httpPost.addHeader(entry.getKey(), entry.getValue()); + } + } + if (!param.isEmpty()) { + List<NameValuePair> formparams = new ArrayList<>(); + for (Map.Entry<String, String> entry : param.entrySet()) { + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formparams, + Consts.UTF_8); + httpPost.setEntity(urlEncodedFormEntity); + } + if (entity != null) { + httpPost.setEntity(entity); + } + HttpResponse httpResponse = httpClient.execute(httpPost); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode == HttpStatus.SC_OK) { + HttpEntity resEntity = httpResponse.getEntity(); + result = EntityUtils.toString(resEntity); + } else { + readHttpResponse(httpResponse); + } + } catch (Exception e) { + throw new CorrelationException("Failed to use post method to get data from server"); + } finally { + if (httpClient != null) { + httpClient.close(); + } + } + return result; + } + + public static String get(String url, Map<String, String> header) throws Exception { + String result = ""; + CloseableHttpClient httpClient = null; + try { + httpClient = getHttpClient(); + HttpGet httpGet = new HttpGet(url); + if (!header.isEmpty()) { + for (Map.Entry<String, String> entry : header.entrySet()) { + httpGet.addHeader(entry.getKey(), entry.getValue()); + } + } + HttpResponse httpResponse = httpClient.execute(httpGet); + int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode == HttpStatus.SC_OK) { + HttpEntity resEntity = httpResponse.getEntity(); + result = EntityUtils.toString(resEntity); + } else { + readHttpResponse(httpResponse); + } + } catch (Exception e) { + throw new CorrelationException("Failed to use get method get data from server"); + } finally { + if (httpClient != null) { + httpClient.close(); + } + } + return result; + } + + private static CloseableHttpClient getHttpClient() throws Exception { + CloseableHttpClient httpClient = HttpClients.custom() + .setSSLSocketFactory(sslConnectionSocketFactory) + .setConnectionManager(connectionManager) + .setConnectionManagerShared(true) + .build(); + return httpClient; + } + + private static String readHttpResponse(HttpResponse httpResponse) + throws ParseException, IOException { + StringBuilder builder = new StringBuilder(); + HttpEntity entity = httpResponse.getEntity(); + builder.append("status:" + httpResponse.getStatusLine()); + builder.append("headers:"); + HeaderIterator iterator = httpResponse.headerIterator(); + while (iterator.hasNext()) { + builder.append("\t" + iterator.next()); + } + if (entity != null) { + String responseString = EntityUtils.toString(entity); + builder.append("response length:" + responseString.length()); + builder.append("response content:" + responseString.replace("\r\n", "")); + } + return builder.toString(); + } +} |