diff options
Diffstat (limited to 'src/main/java/org/openecomp/datarouter/util')
6 files changed, 459 insertions, 40 deletions
diff --git a/src/main/java/org/openecomp/datarouter/util/AaiUiSvcPolicyUtil.java b/src/main/java/org/openecomp/datarouter/util/AaiUiSvcPolicyUtil.java new file mode 100644 index 0000000..d5388d6 --- /dev/null +++ b/src/main/java/org/openecomp/datarouter/util/AaiUiSvcPolicyUtil.java @@ -0,0 +1,77 @@ +/** + * ============LICENSE_START======================================================= + * DataRouter + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.datarouter.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + + +public class AaiUiSvcPolicyUtil { + + static ObjectMapper mapper = new ObjectMapper(); + + public static JsonNode getOriginPayload(JsonNode payload) throws Exception{ + /* + *{ + "origin-uri": "/routerService/1search1", + "origin-payload": {} + } + */ + JsonNode origPayload = null; + + if (payload.has("origin-payload")){ + origPayload = payload.get("origin-payload"); + } + return origPayload; + } + + public static String getOriginUri ( JsonNode payload ) throws Exception { + String originUri = ""; + if (payload.has("origin-uri")){ + originUri = payload.get("origin-uri").textValue(); + } + return originUri; + } + + public static String getTargetUri(JsonNode payload) throws Exception{ + /* + *{ + "origin-uri": "/routerService/1search1", + "origin-payload": {} + } + */ + String uri = ""; + String originUri = getOriginUri(payload); + final Matcher m = Pattern.compile("/routerService/(.*)").matcher(originUri); + if ( m.find() ) { + uri = m.group(1); + } + return uri; + } + +} diff --git a/src/main/java/org/openecomp/datarouter/util/DataRouterConstants.java b/src/main/java/org/openecomp/datarouter/util/DataRouterConstants.java index f9f5df3..fff16f2 100644 --- a/src/main/java/org/openecomp/datarouter/util/DataRouterConstants.java +++ b/src/main/java/org/openecomp/datarouter/util/DataRouterConstants.java @@ -27,16 +27,18 @@ package org.openecomp.datarouter.util; public class DataRouterConstants { public static final String DR_FILESEP = (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator"); + + public static final String DR_AJSC_HOME = System.getProperty("AJSC_HOME"); public static final String DR_SPECIFIC_CONFIG = System.getProperty("CONFIG_HOME") + DR_FILESEP; public static final String DR_BUNDLECONFIG_NAME = (System.getProperty("BUNDLECONFIG_DIR") == null) ? "bundleconfig" : System.getProperty("BUNDLECONFIG_DIR"); - public static final String DR_HOME_BUNDLECONFIG = (System.getProperty("AJSC_HOME") == null) + public static final String DR_HOME_BUNDLECONFIG = (DR_AJSC_HOME == null) ? DR_FILESEP + "opt" + DR_FILESEP + "app" + DR_FILESEP + "datalayer" + DR_FILESEP + DR_BUNDLECONFIG_NAME - : System.getProperty("AJSC_HOME") + DR_FILESEP + DR_BUNDLECONFIG_NAME; + : DR_AJSC_HOME + DR_FILESEP + DR_BUNDLECONFIG_NAME; /** This is the etc directory, relative to AAI_HOME. */ public static final String DR_HOME_ETC = DR_HOME_BUNDLECONFIG + DR_FILESEP + "etc" + DR_FILESEP; @@ -48,6 +50,8 @@ public class DataRouterConstants { public static final String DR_HOME_ETC_OXM = DR_HOME_ETC + "oxm" + DR_FILESEP; + public static final String UI_FILTER_LIST_FILE = + DR_SPECIFIC_CONFIG + DR_FILESEP + "filters" + DR_FILESEP + "aaiui_filters.json"; // AAI Related public static final String AAI_ECHO_SERVICE = "/util/echo"; diff --git a/src/main/java/org/openecomp/datarouter/util/RouterServiceUtil.java b/src/main/java/org/openecomp/datarouter/util/RouterServiceUtil.java index b30c9f9..f2ff7ac 100644 --- a/src/main/java/org/openecomp/datarouter/util/RouterServiceUtil.java +++ b/src/main/java/org/openecomp/datarouter/util/RouterServiceUtil.java @@ -26,16 +26,40 @@ package org.openecomp.datarouter.util; import com.fasterxml.jackson.databind.JsonNode; +import org.apache.camel.Exchange; +import org.apache.camel.component.cxf.common.message.CxfConstants; +import org.apache.cxf.message.Message; import org.json.JSONObject; +import org.openecomp.cl.mdc.MdcContext; +import org.openecomp.restclient.client.Headers; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; + +import javax.servlet.ServletRequest; public class RouterServiceUtil { + + public static void setMdcContext(Exchange exchange){ + String txnID = exchange.getIn().getHeader(Headers.TRANSACTION_ID, + Arrays.asList(UUID.randomUUID())).toString(); + String remote = exchange.getIn().getHeader(Headers.FROM_APP_ID, "").toString(); + String remoteAddress = ""; + Message cxfMessage = exchange.getIn().getHeader(CxfConstants.CAMEL_CXF_MESSAGE, Message.class); + if (cxfMessage != null) { + ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST"); + if ( request != null) + remoteAddress = request.getRemoteAddr(); + } + + MdcContext.initialize(txnID, "Synapse", "", remote, remoteAddress); + } public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) { @@ -223,4 +247,27 @@ public class RouterServiceUtil { String json = jsonObject.toString(); return json; } + + /** + * Helper utility to concatenate substrings of a URI together to form a proper URI. + * + * @param suburis the list of substrings to concatenate together + * @return the concatenated list of substrings + */ + public static String concatSubUri(String... suburis) { + String finalUri = ""; + + for (String suburi : suburis) { + + if (suburi != null) { + // Remove any leading / since we only want to append / + suburi = suburi.replaceFirst("^/*", ""); + + // Add a trailing / if one isn't already there + finalUri += suburi.endsWith("/") ? suburi : suburi + "/"; + } + } + + return finalUri; + } } diff --git a/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java b/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java index 91f5910..4954a92 100644 --- a/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java +++ b/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java @@ -38,52 +38,63 @@ public class SearchSuggestionPermutation { * @param list The list of statuses to create permutations of * @return A list which contains a array list of all possible combinations */ - @SuppressWarnings("serial") - public List<ArrayList<String>> getSuggestionsPermutation(List<String> list) { - List<String> statusList = new ArrayList<>(list); - List<String> dupStatusList; - ArrayList<ArrayList<String>> uniqueList = new ArrayList<>(); - int mainLoopIndexCounter = 0; + public static ArrayList<ArrayList<String>> getUniqueListForSuggestions( + List<String> originalList) { + ArrayList<ArrayList<String>> lists = new ArrayList<ArrayList<String>>(); + if (originalList.isEmpty()) { + lists.add(new ArrayList<String>()); + return lists; + } + List<String> list = new ArrayList<String>(originalList); + String head = list.get(0); + ArrayList<String> rest = new ArrayList<String>(list.subList(1, list.size())); + + for (ArrayList<String> activeList : getUniqueListForSuggestions(rest)) { + ArrayList<String> newList = new ArrayList<String>(); + newList.add(head); + newList.addAll(activeList); + lists.add(newList); + lists.add(activeList); + } + return lists; + } + + public static ArrayList<ArrayList<String>> getNonEmptyUniqueLists(List<String> list){ + ArrayList<ArrayList<String>> lists = getUniqueListForSuggestions(list); + // remove empty list from the power set + for (ArrayList<String> emptyList : lists ){ + if ( emptyList.isEmpty() ) { + lists.remove(emptyList); + break; + } + } + return lists; + } - for (String status : statusList) { - // Add the single entity subset - //This will add the unique single values eg [A],[B],[C],[D] - uniqueList.add(new ArrayList<String>() { - { - add(status); - } - }); + public static List<List<String>> getListPermutations(List<String> list) { + List<String> inputList = new ArrayList<String>(); + inputList.addAll(list); + if (inputList.size() == 0) { + List<List<String>> result = new ArrayList<List<String>>(); + result.add(new ArrayList<String>()); + return result; + } - // Remove all the elements to left till the current index - dupStatusList = truncateListUntill(statusList, mainLoopIndexCounter); + List<List<String>> listOfLists = new ArrayList<List<String>>(); - while (!dupStatusList.isEmpty()) { - ArrayList<String> suggListInIterate= new ArrayList<>(); - suggListInIterate.add(status); + String firstElement = inputList.remove(0); - for (String dupStatus : dupStatusList) { - suggListInIterate.add(dupStatus); - } + List<List<String>> recursiveReturn = getListPermutations(inputList); + for (List<String> li : recursiveReturn) { - uniqueList.add(suggListInIterate); - dupStatusList.remove(0); + for (int index = 0; index <= li.size(); index++) { + List<String> temp = new ArrayList<String>(li); + temp.add(index, firstElement); + listOfLists.add(temp); } - mainLoopIndexCounter++; } - - return uniqueList; + return listOfLists; } - private List<String> truncateListUntill(List<String> lists, int index) { - List<String> truncatedList = new ArrayList<>(lists); - int counter = 0; - - while (counter <= index) { - truncatedList.remove(0); - counter++; - } - - return truncatedList; - } } diff --git a/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java b/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java new file mode 100644 index 0000000..33874a8 --- /dev/null +++ b/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java @@ -0,0 +1,226 @@ +/** + * ============LICENSE_START======================================================= + * DataRouter + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.datarouter.util.client; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.apache.camel.Exchange; +import org.apache.camel.component.http.HttpMessage; +import org.apache.camel.Message; +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.cl.mdc.MdcContext; +import org.openecomp.datarouter.logging.DataRouterMsgs; +import org.openecomp.datarouter.util.AaiUiSvcPolicyUtil; +import org.openecomp.datarouter.util.NodeUtils; +import org.openecomp.datarouter.util.RouterServiceUtil; +import org.openecomp.restclient.client.Headers; +import org.openecomp.restclient.client.OperationResult; +import org.openecomp.restclient.client.RestClient; +import org.openecomp.restclient.enums.RestAuthenticationMode; +import org.slf4j.MDC; + +import org.springframework.http.HttpStatus; + +import org.openecomp.restclient.rest.HttpUtil; +import com.fasterxml.jackson.databind.JsonNode; + +public class NoAuthRestClient implements SvcRoutingRestClient { + + private RestClient restClient; + + private String host; + private String port; + private String originUrl; + private String targetUri; + private JsonNode targetPayload; + private Logger logger; + private Logger auditLogger; + + public NoAuthRestClient(int connectTimeOut, int readTimeOut) { + LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance(); + logger = loggerFactoryInstance.getLogger(NoAuthRestClient.class.getName()); + auditLogger = loggerFactoryInstance.getAuditLogger(NoAuthRestClient.class.getName()); + restClient = new RestClient().authenticationMode(RestAuthenticationMode.HTTP_NOAUTH) + .connectTimeoutMs(connectTimeOut).readTimeoutMs(readTimeOut); + } + + + private OperationResult getResults(String url, JsonNode payload){ + Map<String, List<String>> headers = new HashMap<>(); + headers.put(Headers.FROM_APP_ID, Arrays.asList("Synapse")); + headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID))); + return this.getRestClient().post(url, payload.asText(), headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + } + + public final void handleRequest (String host, String port, Exchange exchange) throws Exception { + RouterServiceUtil.setMdcContext(exchange); + Message message = exchange.getIn(); + String body = message.getBody(String.class); + OperationResult result = new OperationResult(); + + this.setHost(host); + this.setPort(port); + + this.setOriginUrl(message.getHeader(Exchange.HTTP_URL).toString()); + if (body != null && body.length() != 0) { + JsonNode node = NodeUtils.convertJsonStrToJsonNode(body); + this.setTargetPayload(AaiUiSvcPolicyUtil.getOriginPayload(node)); + this.setTargetUri(AaiUiSvcPolicyUtil.getTargetUri(node)); + } + + if ( this.getTargetPayload() == null || this.getTargetUri() == null){ + logger.error(DataRouterMsgs.INVALID_ORIGIN_PAYLOAD, body); + result.setResultCode(HttpStatus.BAD_REQUEST.value()); + result.setFailureCause("Invalid payload"); + } + + String targetUrl = "http://" + host + ":" + port + "/" + this.targetUri; + auditLogger.info(DataRouterMsgs.ROUTING_FROM_TO, this.getOriginUrl(), targetUrl); + long startTimeInMs = System.currentTimeMillis(); + + result = this.getResults(targetUrl, targetPayload); + + long targetMsOpTime = (System.currentTimeMillis() - startTimeInMs); + auditLogger.info(DataRouterMsgs.OP_TIME, "Target service at "+ targetUrl, String.valueOf(targetMsOpTime)); + + int rc = result.getResultCode(); + String resultStr = ""; + if (HttpUtil.isHttpResponseClassSuccess(rc)) { + resultStr = result.getResult(); + } else { + resultStr = result.getFailureCause(); + } + + logger.debug(DataRouterMsgs.ROUTING_RESPONSE, targetUrl, result.toString()); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, rc); + exchange.getOut().setBody(resultStr); + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getHost() + */ + @Override + public String getHost() { + return host; + } + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setHost(java.lang.String) + */ + @Override + public void setHost(String host) { + this.host = host; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getPort() + */ + @Override + public String getPort() { + return port; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setPort(java.lang.String) + */ + @Override + public void setPort(String port) { + this.port = port; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetUri() + */ + @Override + public String getTargetUri() { + return targetUri; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setTargetUri(java.lang.String) + */ + @Override + public void setTargetUri(String targetUri) { + this.targetUri = targetUri; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetPayload() + */ + @Override + public JsonNode getTargetPayload() { + return targetPayload; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setTargetPayload(com.fasterxml.jackson.databind.JsonNode) + */ + @Override + public void setTargetPayload(JsonNode targetPayload) { + this.targetPayload = targetPayload; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getRestClient() + */ + @Override + public RestClient getRestClient() { + return restClient; + } + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setRestClient() + */ + @Override + public void setRestClient(RestClient client) { + this.restClient = client; + } + + + public String getOriginUrl() { + return originUrl; + } + + + public void setOriginUrl(String originUrl) { + this.originUrl = originUrl; + } + + +} diff --git a/src/main/java/org/openecomp/datarouter/util/client/SvcRoutingRestClient.java b/src/main/java/org/openecomp/datarouter/util/client/SvcRoutingRestClient.java new file mode 100644 index 0000000..b1af73c --- /dev/null +++ b/src/main/java/org/openecomp/datarouter/util/client/SvcRoutingRestClient.java @@ -0,0 +1,54 @@ +/** + * ============LICENSE_START======================================================= + * DataRouter + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.datarouter.util.client; + +import org.openecomp.restclient.client.RestClient; + +import com.fasterxml.jackson.databind.JsonNode; + + +public interface SvcRoutingRestClient { + + String getHost(); + + void setHost(String host); + + String getPort(); + + void setPort(String port); + + String getTargetUri(); + + void setTargetUri(String targetUri); + + JsonNode getTargetPayload(); + + void setTargetPayload(JsonNode targetPayload); + + RestClient getRestClient(); + + void setRestClient(RestClient client); + +} |