diff options
author | BOSLET, CORY <cory.boslet@att.com> | 2021-03-10 17:22:28 -0500 |
---|---|---|
committer | AT&T Open Source <g22940@att.com> | 2021-03-10 17:22:29 -0500 |
commit | 39761146b77f58973612f8fdee3de9f987278edf (patch) | |
tree | ab96535b565898abf67e765b0141fb02fc0c6641 /adapters/mso-sdnc-adapter | |
parent | 1d316a3c50f978313eca360c1a1636de68afee9d (diff) |
add new sdnc interaction
Added Cors auth support option for sdnc interaction
Issue-ID: SO-3580
Signed-off-by: AT&T Open Source <g22940@att.com>
Change-Id: I54d17ea5e02a20114f5857940cc4dabc72854dea
Diffstat (limited to 'adapters/mso-sdnc-adapter')
5 files changed, 278 insertions, 0 deletions
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java index da0fcd3d04..047686ccc3 100644 --- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java +++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java @@ -31,6 +31,7 @@ public interface Constants { public static final String SDNC_AUTH_PROP = "org.onap.so.adapters.sdnc.sdncauth"; public static final String BPEL_AUTH_PROP = "org.onap.so.adapters.sdnc.bpelauth"; + public static final String SDNC_HOST = "org.onap.so.adapters.sdnc.sdncHost"; public static final String SDNC_SVCCFGRESP_ROOT = "input"; public static final String SDNC_REQ_ID = "/svc-request-id"; diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/SDNCService.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/SDNCService.java new file mode 100644 index 0000000000..e5b7049934 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/SDNCService.java @@ -0,0 +1,136 @@ +package org.onap.so.adapters.sdnc.tasks; + +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.UriBuilder; +import javax.xml.bind.DatatypeConverter; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.onap.logging.filter.spring.SpringClientPayloadFilter; +import org.onap.so.adapters.sdnc.impl.Constants; +import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.onap.so.utils.CryptoUtils; +import org.onap.so.utils.ExternalTaskUtils; +import org.onap.so.utils.RetrySequenceLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Component +public class SDNCService extends ExternalTaskUtils { + + private static final Logger logger = LoggerFactory.getLogger(SDNCService.class); + + @Autowired + private AuditMDCSetup mdcSetup; + + @Autowired + private RestTemplate restTemplate; + + @Autowired + private Environment env; + + private ObjectMapper objMapper = new ObjectMapper(); + + public SDNCService() { + super(RetrySequenceLevel.SHORT); + objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + // TODO maybe make a new sdnc client + public void executePostTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + mdcSetup.setupMDC(externalTask); + logger.debug("Executing External Task SDNC Post Service"); + Map<String, Object> variables = new HashMap<>(); + boolean success = false; + String errorMessage = ""; + try { + Object request = externalTask.getVariable("sdncRequest"); + String jsonRequest = buildJsonRequest(request); + + UriBuilder url = UriBuilder.fromUri(env.getProperty(Constants.SDNC_HOST)); + url.path((String) externalTask.getVariable("sdncUri")); + + HttpEntity<String> requestEntity = new HttpEntity<String>(jsonRequest, getHttpHeader()); + ResponseEntity<Object> responseEntity = + restTemplate.exchange(url.build(), HttpMethod.POST, requestEntity, Object.class); + + if (responseEntity.getStatusCode().equals(HttpStatus.ACCEPTED)) { + success = true; + } else { + errorMessage = "SDNC returned a " + responseEntity.getStatusCode().value(); + } + } catch (Exception e) { + logger.error("Error during External Task SDNC Post Service", e); + errorMessage = "Error during External Task SDNC Post Service: " + e.getMessage(); + } + + if (success) { + externalTaskService.complete(externalTask, variables); + logger.debug("The External Task {} was Successful", externalTask.getId()); + } else { + if (externalTask.getRetries() == null) { + logger.debug("The External Task {} Failed, Setting Retries to Default Start Value {}", + externalTask.getId(), getRetrySequence().length); + externalTaskService.handleFailure(externalTask, errorMessage, "errorDetails", getRetrySequence().length, + 10000); + } else if (externalTask.getRetries() != null && externalTask.getRetries() - 1 == 0) { + logger.debug("The External Task {} Failed, All Retries Exhausted", externalTask.getId()); + variables.put("errorMessage", errorMessage); + externalTaskService.handleBpmnError(externalTask, "SDNCWorkflowException", null, variables); + } else { + logger.debug("The External Task {} Failed, Decrementing Retries to {} with Retry Delay {}", + externalTask.getId(), externalTask.getRetries() - 1, + calculateRetryDelay(externalTask.getRetries())); + externalTaskService.handleFailure(externalTask, errorMessage, "errorDetails", + externalTask.getRetries() - 1, calculateRetryDelay(externalTask.getRetries())); + } + } + } + + public void executeGetTask(ExternalTask externalTask, ExternalTaskService externalTaskService) { + logger.debug("Executing External Task SDNC Get Service"); + + } + + private String buildJsonRequest(Object request) throws JsonProcessingException { + String jsonRequest = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request); + return jsonRequest; + } + + private HttpHeaders getHttpHeader() throws GeneralSecurityException { + HttpHeaders httpHeader = new HttpHeaders(); + httpHeader.set("Authorization", getAuth()); + httpHeader.setContentType(MediaType.APPLICATION_JSON); + List<MediaType> acceptMediaTypes = new ArrayList<>(); + acceptMediaTypes.add(MediaType.APPLICATION_JSON); + acceptMediaTypes.add(MediaType.TEXT_PLAIN); + httpHeader.setAccept(acceptMediaTypes); + return httpHeader; + } + + protected String getAuth() throws GeneralSecurityException { + String auth = CryptoUtils.decrypt(env.getProperty(Constants.SDNC_AUTH_PROP), + env.getProperty(Constants.ENCRYPTION_KEY_PROP)); + return "Basic " + DatatypeConverter.printBase64Binary(auth.getBytes()); + } + +} diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/TaskServices.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/TaskServices.java new file mode 100644 index 0000000000..fd95b44aee --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/tasks/TaskServices.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. 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========================================================= + */ + +package org.onap.so.adapters.sdnc.tasks; + +import javax.annotation.PostConstruct; +import org.camunda.bpm.client.ExternalTaskClient; +import org.onap.so.utils.ExternalTaskServiceUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Component +@Profile("!test") +public class TaskServices { + + @Autowired + private ExternalTaskServiceUtils externalTaskServiceUtils; + + @Autowired + private SDNCService service; + + @PostConstruct + public void post() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("sdncPost").lockDuration(externalTaskServiceUtils.getLockDurationLong()) + .handler(service::executePostTask).open(); + } + } + + @PostConstruct + public void get() throws Exception { + for (int i = 0; i < externalTaskServiceUtils.getMaxClients(); i++) { + ExternalTaskClient client = externalTaskServiceUtils.createExternalTaskClient(); + client.subscribe("sdncGet").lockDuration(externalTaskServiceUtils.getLockDurationLong()) + .handler(service::executeGetTask).open(); + } + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/tasks/SDNCServiceTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/tasks/SDNCServiceTest.java new file mode 100644 index 0000000000..386d83f87d --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/tasks/SDNCServiceTest.java @@ -0,0 +1,72 @@ +package org.onap.so.adapters.sdnc.tasks; + +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.util.HashMap; +import org.camunda.bpm.client.task.ExternalTask; +import org.camunda.bpm.client.task.ExternalTaskService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.so.logging.tasks.AuditMDCSetup; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +@RunWith(MockitoJUnitRunner.class) +public class SDNCServiceTest { + + private String RESOURCE_PATH = "src/test/resources"; + + @Mock + private ExternalTask mockExternalTask; + + @Mock + private ExternalTaskService mockExternalTaskService; + + @Mock + private AuditMDCSetup mdcSetup; + + @Mock + private RestTemplate restTemplate; + + @Mock + private ResponseEntity<Object> response; + + @Mock + private Environment env; + + @Spy + @InjectMocks + private SDNCService sdncService; + + @Test + public void testExecutePostTask() throws IOException, GeneralSecurityException { + String payload = new String(Files.readAllBytes(Paths.get(RESOURCE_PATH + "/sdncRequest.json"))); + + Mockito.when(env.getProperty("org.onap.so.adapters.sdnc.sdncHost")).thenReturn("sdncHost"); + Mockito.when(mockExternalTask.getVariable("sdncRequest")).thenReturn(payload); + Mockito.when(mockExternalTask.getVariable("sdncUri")).thenReturn("/sdnc/action"); + Mockito.doReturn("Basic 123").when(sdncService).getAuth(); + Mockito.when(restTemplate.exchange(Mockito.any(URI.class), Mockito.eq(HttpMethod.POST), + Mockito.any(HttpEntity.class), Mockito.eq(Object.class))).thenReturn(response); + Mockito.when(response.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); + Mockito.doNothing().when(mockExternalTaskService).complete(Mockito.any(), Mockito.any()); + + sdncService.executePostTask(mockExternalTask, mockExternalTaskService); + + Mockito.verify(mockExternalTaskService).complete(Mockito.eq(mockExternalTask), Mockito.eq(new HashMap<>())); + + } + +} diff --git a/adapters/mso-sdnc-adapter/src/test/resources/sdncRequest.json b/adapters/mso-sdnc-adapter/src/test/resources/sdncRequest.json new file mode 100644 index 0000000000..879b9b3765 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/resources/sdncRequest.json @@ -0,0 +1,12 @@ +{ + "request_control": { + "transaction_id": "migId", + "request_id": "reqId", + "invocation_id": "subReqId", + "async": true, + "callback_url": "host/callback" + }, + "input": { + "id": "id" + } +}
\ No newline at end of file |