From: Smokowski, Kevin (ks6305) Date: Tue, 2 Jun 2020 19:21:04 +0000 (+0000) Subject: restapicallnode fix X-Git-Tag: 1.0.0~2 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=ccsdk%2Fsli%2Fplugins.git;a=commitdiff_plain;h=ad5c70ad6ab8514c5a5e4ed87d76877b1b451ccd restapicallnode fix restapicallnode should support commas in urls Issue-ID: CCSDK-2399 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: If177498b6f854aaa6f89811ab8a134f696e7e700 --- diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java index 709774bb..b131301d 100755 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java @@ -50,6 +50,9 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -112,6 +115,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { protected Integer httpReadTimeout; protected HashMap partnerStore; + private static final Pattern retryPattern = Pattern.compile(".*,(http|https):.*"); public RestapiCallNode() { String configDir = System.getProperty(PROPERTIES_DIR_KEY, DEFAULT_PROPERTIES_DIR); @@ -246,8 +250,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { * @throws SvcLogicException when URL validation fails */ private static void validateUrl(String restapiUrl) throws SvcLogicException { - if (restapiUrl.contains(",")) { - String[] urls = restapiUrl.split(","); + if (containsMultipleUrls(restapiUrl)) { + String[] urls = getMultipleUrls(restapiUrl); for (String url : urls) { validateUrl(url); } @@ -483,8 +487,8 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { if(p.targetEntity != null && !p.targetEntity.isEmpty()) { MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, p.targetEntity); } - if (p.restapiUrl.contains(",") && retryPolicy == null) { - String[] urls = p.restapiUrl.split(","); + if (containsMultipleUrls(p.restapiUrl) && retryPolicy == null) { + String[] urls = getMultipleUrls(p.restapiUrl); retryPolicy = new RetryPolicy(urls, urls.length * 2); p.restapiUrl = urls[0]; } @@ -1260,6 +1264,30 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { return defaultValue; } + protected static String[] getMultipleUrls(String restapiUrl) { + List urls = new ArrayList(); + int start = 0; + for (int i = 0; i < restapiUrl.length(); i++) { + if (restapiUrl.charAt(i) == ',') { + if (i + 9 < restapiUrl.length()) { + String part = restapiUrl.substring(i + 1, i + 9); + if (part.equals("https://") || part.startsWith("http://")) { + urls.add(restapiUrl.substring(start, i)); + start = i + 1; + } + } + } else if (i == restapiUrl.length() - 1) { + urls.add(restapiUrl.substring(start, i + 1)); + } + } + String[] arr = new String[urls.size()]; + return urls.toArray(arr); + } + + protected static boolean containsMultipleUrls(String restapiUrl) { + Matcher m = retryPattern.matcher(restapiUrl); + return m.matches(); + } private static class FileParam { diff --git a/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java b/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java index b2f75bcd..a993bb94 100755 --- a/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java +++ b/restapi-call-node/provider/src/test/java/org/onap/ccsdk/sli/plugins/restapicall/TestRestapiCallNode.java @@ -22,8 +22,11 @@ package org.onap.ccsdk.sli.plugins.restapicall; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import java.util.HashMap; import java.util.Map; import org.codehaus.jettison.json.JSONObject; @@ -562,5 +565,40 @@ public class TestRestapiCallNode { //This will throw a JSONException and fail the test case if rest api call node doesn't form valid JSON assertNotNull(new JSONObject(request)); } + + @Test + public void testGetMultipleUrls() throws Exception{ + String[] urls = RestapiCallNode.getMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana,https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato"); + assertEquals("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana",urls[0]); + assertEquals("https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato",urls[1]); + + urls = RestapiCallNode.getMultipleUrls("https://wiki.onap.org/,http://localhost:7001/,http://wiki.onap.org/"); + assertEquals("https://wiki.onap.org/",urls[0]); + assertEquals("http://localhost:7001/",urls[1]); + assertEquals("http://wiki.onap.org/",urls[2]); + + urls = RestapiCallNode.getMultipleUrls("https://wiki.onap.org/test=4,5,6,http://localhost:7001/test=1,2,3,http://wiki.onap.org/test=7,8,9,10"); + assertEquals("https://wiki.onap.org/test=4,5,6",urls[0]); + assertEquals("http://localhost:7001/test=1,2,3",urls[1]); + assertEquals("http://wiki.onap.org/test=7,8,9,10",urls[2]); + + urls = RestapiCallNode.getMultipleUrls("https://wiki.onap.org/,https://readthedocs.org/projects/onap/"); + assertEquals("https://wiki.onap.org/",urls[0]); + assertEquals("https://readthedocs.org/projects/onap/",urls[1]); + } + + @Test + public void testContainsMultipleUrls() throws Exception{ + assertFalse(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/")); + assertFalse(RestapiCallNode.containsMultipleUrls("http://wiki.onap.org/")); + assertFalse(RestapiCallNode.containsMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana")); + assertFalse(RestapiCallNode.containsMultipleUrls("https://localhost:8008/params=1,2,3,4,5,6")); + + assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/,https://readthedocs.org/projects/onap/")); + assertTrue(RestapiCallNode.containsMultipleUrls("http://localhost:7001/,http://localhost:7002")); + assertTrue(RestapiCallNode.containsMultipleUrls("http://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Banana,https://localhost:8008/rest/restconf/data/abc:def/abc:action=Create,deviceType=Potato")); + assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/,http://localhost:7001/,http://wiki.onap.org/")); + assertTrue(RestapiCallNode.containsMultipleUrls("https://wiki.onap.org/test=4,5,6,http://localhost:7001/test=1,2,3,http://wiki.onap.org/test=7,8,9,10")); + } }