implement generic retry and partners in restapicallnode
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / restconfapicall / RestconfApiCallNode.java
index 3007b5a..620df28 100644 (file)
@@ -43,10 +43,10 @@ import org.dom4j.DocumentHelper;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
+import org.onap.ccsdk.sli.plugins.restapicall.Format;
 import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse;
 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
-import org.onap.ccsdk.sli.plugins.restapicall.RetryException;
-import org.onap.ccsdk.sli.plugins.restapicall.RetryPolicy;
+import org.onap.ccsdk.sli.plugins.restapicall.XmlParser;
 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializer;
 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializerContext;
 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerFactory;
@@ -163,14 +163,9 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin {
     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx,
                             Integer retryCount) throws SvcLogicException {
         RestapiCallNode rest = getRestapiCallNode();
-        RetryPolicy retryPolicy = null;
         HttpResponse r = new HttpResponse();
         try {
             YangParameters p = getYangParameters(paramMap);
-            if (p.partner != null) {
-                retryPolicy = rest.getRetryPolicyStore()
-                        .getRetryPolicy(p.partner);
-            }
 
             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
             Map<String, String> props = new HashMap<>((Map)ctx.toProperties());
@@ -195,10 +190,14 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin {
 
             String response = getResponse(ctx, p, pp, r);
             if (response != null) {
-                Map<String, String> resProp = serializeResponse(
-                        p, uri, response, insIdCtx);
-                for (Map.Entry<String, String> pro : resProp.entrySet()) {
-                    ctx.setAttribute(pro.getKey(), pro.getValue());
+                try {
+                    Map<String, String> resProp = serializeResponse(
+                            p, uri, response, insIdCtx);
+                    for (Map.Entry<String, String> pro : resProp.entrySet()) {
+                        ctx.setAttribute(pro.getKey(), pro.getValue());
+                    }
+                } catch (SvcLogicException e) {
+                    convertToNormalRes(ctx, p, pp, response);
                 }
             }
         } catch (SvcLogicException e) {
@@ -209,30 +208,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin {
 
             log.error(REQ_ERR + e.getMessage(), e);
             String prefix = parseParam(paramMap, RES_PRE, false, null);
-            if (retryPolicy == null || !shouldRetry) {
-                setFailureResponseStatus(ctx, prefix, e.getMessage());
-            } else {
-                if (retryCount == null) {
-                    retryCount = 0;
-                }
-                log.debug(format(ATTEMPTS_MSG, retryCount,
-                                 retryPolicy.getMaximumRetries()));
-                try {
-                    retryCount = retryCount + 1;
-                    if (retryCount < retryPolicy.getMaximumRetries() + 1) {
-                        setRetryUri(paramMap, retryPolicy);
-                        log.debug(format(RETRY_COUNT, retryCount, retryPolicy
-                                .getMaximumRetries()));
-                        sendRequest(paramMap, ctx, retryCount);
-                    } else {
-                        log.debug(MAX_RETRY_ERR);
-                        setFailureResponseStatus(ctx, prefix, e.getMessage());
-                    }
-                } catch (Exception ex) {
-                    log.error(NO_MORE_RETRY, ex);
-                    setFailureResponseStatus(ctx, prefix, RETRY_FAIL);
-                }
-            }
+            setFailureResponseStatus(ctx, prefix, e.getMessage());
         }
 
         if (r != null && r.code >= 300) {
@@ -241,6 +217,27 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin {
         }
     }
 
+    private void convertToNormalRes(SvcLogicContext ctx ,
+                                    YangParameters p, String pp, String body)
+            throws SvcLogicException {
+        if (p.convertResponse) {
+            Map<String, String> mm = null;
+            if (p.format == Format.XML) {
+                mm = XmlParser.convertToProperties(body, p.listNameList);
+            } else if (p.format == Format.JSON) {
+                mm = org.onap.ccsdk.sli.plugins.restapicall.JsonParser
+                        .convertToProperties(body);
+            }
+
+            if (mm != null) {
+                for (Map.Entry<String, String> entry : mm.entrySet()) {
+                    ctx.setAttribute(pp + entry.getKey(),
+                                     entry.getValue());
+                }
+            }
+        }
+    }
+
     /**
      * Serializes the request message to JSON or XML from the properties.
      *
@@ -375,31 +372,6 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin {
         ctx.setAttribute(prefix + RES_MSG, res.message);
     }
 
-    /**
-     * Sets the retry URI to the param map from the retry policies different
-     * host.
-     *
-     * @param paramMap            parameter map
-     * @param retryPolicy         retry policy
-     * @throws URISyntaxException when new URI creation fails
-     * @throws RetryException     when retry policy cannot give another host
-     */
-    private void setRetryUri(Map<String, String> paramMap,
-                             RetryPolicy retryPolicy)
-            throws URISyntaxException, RetryException {
-        URI uri = new URI(paramMap.get(REST_API_URL));
-        String hostName = uri.getHost();
-        String retryString = retryPolicy.getNextHostName(uri.toString());
-
-        URI uriTwo = new URI(retryString);
-        URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(
-                uriTwo.getPort()).scheme(uriTwo.getScheme()).build();
-
-        paramMap.put(REST_API_URL, retryUri.toString());
-        log.debug(UPDATED_URL + retryUri.toString());
-        log.debug(format(COMM_FAIL, hostName, retryString));
-    }
-
     /**
      * Updates request message for JSON and XML data format, when the HTTP
      * method points it as PUT or PATCH.