--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.camunda.delegate.otf.common;\r
+\r
+import org.oran.otf.cadi.configuration.FilterCondition;\r
+import org.oran.otf.camunda.exception.TestExecutionException;\r
+import org.oran.otf.camunda.model.ExecutionConstants;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.model.local.DMaaPRequest;\r
+import org.oran.otf.common.utility.Utility;\r
+import org.oran.otf.common.utility.gson.Convert;\r
+import org.oran.otf.common.utility.http.RequestUtility;\r
+import com.fasterxml.jackson.core.type.TypeReference;\r
+\r
+import java.util.Base64;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import javax.ws.rs.core.MediaType;\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.util.EntityUtils;\r
+import org.camunda.bpm.engine.delegate.DelegateExecution;\r
+import org.camunda.bpm.engine.delegate.JavaDelegate;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.beans.factory.annotation.Value;\r
+import org.springframework.context.annotation.Conditional;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class PostResultsToDMaaPDelegate implements JavaDelegate {\r
+\r
+ private static Logger logger = LoggerFactory.getLogger(PostResultsToDMaaPDelegate.class);\r
+\r
+ @Value("${otf.cadi.aaf-mech-id}")\r
+ private String AAF_APPID;\r
+\r
+ @Value("${otf.cadi.aaf-mech-password}")\r
+ private String AAF_APPPASS;\r
+\r
+ @Value("${otf.environment}")\r
+ private String env;\r
+\r
+ @Autowired private WorkflowUtility utility;\r
+\r
+ private final String template = "https://<hostname>:3905/events/<topic>";\r
+\r
+ @Override\r
+ public void execute(DelegateExecution execution) throws Exception {\r
+ logger.info("[PostResultsToDMaaP] Starting to post test results to dmaap.");\r
+ final String logPrefix = Utility.getLoggerPrefix();\r
+\r
+ // Get the current test execution object.\r
+ TestExecution testExecution = utility.getTestExecution(execution.getVariables(), logPrefix);\r
+\r
+ List<Object> testDataActivity = null;\r
+ Object dataByActivity =\r
+ utility.getTestDataByActivity(\r
+ execution.getVariables(), execution.getCurrentActivityId(), logPrefix);\r
+ if (!(dataByActivity instanceof List)) {\r
+ logger.error(\r
+ execution.getActivityInstanceId()\r
+ + ": Failed to retrieve dmaap requests in test data as list");\r
+ throw new TestExecutionException(\r
+ execution.getActivityInstanceId()\r
+ + ": Missing data to post to dmaap. Failed to retrieve dmaap requests in test data as list");\r
+ }\r
+\r
+ // convert data to map and grab dmaaprequest array\r
+ testDataActivity = (List) dataByActivity;\r
+ List<DMaaPRequest> dmaapRequests = null;\r
+ try {\r
+ dmaapRequests =\r
+ Convert.listToObjectList(testDataActivity, new TypeReference<List<DMaaPRequest>>() {});\r
+ } catch (Exception e) {\r
+ logger.error(\r
+ execution.getActivityInstanceId() + ": Failed to get dmaap requests from test data");\r
+ throw new TestExecutionException(\r
+ execution.getActivityInstanceId() + ": Missing data to post to dmaap. " + e.getMessage(),\r
+ e);\r
+ }\r
+ if (dmaapRequests == null || dmaapRequests.isEmpty()) {\r
+ logger.error(execution.getActivityInstanceId() + ": Failed to retrieve dmaap request list");\r
+ throw new TestExecutionException(\r
+ execution.getActivityInstanceId() + ": Missing dmaap request list");\r
+ }\r
+\r
+ // Get the testDetails object\r
+ Map<String, Object> testDetails = utility.getTestDetails(execution.getVariables(), logPrefix);\r
+\r
+ // Post results to Dmaap\r
+ Map<String, Object> results = postResultsToDmaap(testExecution, dmaapRequests, logPrefix);\r
+\r
+ // Set test details to show results of each post to dmaap\r
+ testDetails.put(execution.getCurrentActivityId(), results);\r
+ execution.setVariable(ExecutionConstants.ExecutionVariable.TEST_DETAILS, testDetails);\r
+ logger.info("[PostResultsToDMaaP] Finished posting test results to dmaap.");\r
+ }\r
+\r
+ private Map<String, Object> postResultsToDmaap(\r
+ TestExecution execution, List<DMaaPRequest> dmaapRequests, String logPrefix) {\r
+ String payload = execution.toString();\r
+ Map<String, Object> results = new HashMap<>();\r
+ Map<String, String> headers = new HashMap<>();\r
+ headers.put("Authorization", getAuthorizationHeader());\r
+ headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
+\r
+ for (DMaaPRequest request : dmaapRequests) {\r
+ String url = new String(template);\r
+ url = url.replace("<hostname>", request.getHostname());\r
+ url = url.replace("<topic>", request.getAsyncTopic());\r
+\r
+ try {\r
+ results.put(url, getResponse(url, payload, headers, request.getRequiresProxy()));\r
+ } catch (Exception e) {\r
+ e.printStackTrace();\r
+ logger.info(logPrefix + "Error while posting to dmaap : " + e.getMessage());\r
+ results.put(url, e.getMessage());\r
+ }\r
+ }\r
+ return results;\r
+ }\r
+\r
+ private Map<String, Object> getResponse(\r
+ String url, String payload, Map<String, String> headers, boolean proxy)\r
+ throws Exception {\r
+ HttpResponse response = RequestUtility.postSync(url, payload, headers, proxy);\r
+ String sRes = EntityUtils.toString(response.getEntity());\r
+ Map<String, Object> res;\r
+ try {\r
+ res = Convert.jsonToMap(sRes);\r
+ } catch (Exception e) {\r
+ res = new HashMap<>();\r
+ res.put("response", sRes);\r
+ }\r
+ return res;\r
+ }\r
+\r
+ private String getAuthorizationHeader() {\r
+ return "Basic "\r
+ + new String(Base64.getEncoder().encode((AAF_APPID + ":" + AAF_APPPASS).getBytes()));\r
+ }\r
+}\r