diff options
Diffstat (limited to 'src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java')
-rw-r--r-- | src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java | 117 |
1 files changed, 85 insertions, 32 deletions
diff --git a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java index 4dbe852d..bdd1fff6 100644 --- a/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java +++ b/src/main/java/org/onap/cps/ncmp/dmi/rest/controller/DmiRestController.java @@ -37,11 +37,12 @@ import org.onap.cps.ncmp.dmi.model.ModuleReferencesRequest; import org.onap.cps.ncmp.dmi.model.ModuleResourcesReadRequest; import org.onap.cps.ncmp.dmi.model.ModuleSet; import org.onap.cps.ncmp.dmi.model.YangResources; +import org.onap.cps.ncmp.dmi.notifications.async.AsyncTaskExecutor; import org.onap.cps.ncmp.dmi.rest.api.DmiPluginApi; import org.onap.cps.ncmp.dmi.rest.api.DmiPluginInternalApi; import org.onap.cps.ncmp.dmi.service.DmiService; -import org.onap.cps.ncmp.dmi.service.NcmpKafkaPublisherService; import org.onap.cps.ncmp.dmi.service.model.ModuleReference; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -54,13 +55,13 @@ import org.springframework.web.bind.annotation.RestController; public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { private final DmiService dmiService; - private final ObjectMapper objectMapper; - - private final NcmpKafkaPublisherService ncmpKafkaPublisherService; - + private final AsyncTaskExecutor asyncTaskExecutor; private static final Map<OperationEnum, HttpStatus> operationToHttpStatusMap = new HashMap<>(6); + @Value("${notification.async.executor.time-out-value-in-ms:2000}") + private int timeOutInMillis; + static { operationToHttpStatusMap.put(null, HttpStatus.OK); operationToHttpStatusMap.put(OperationEnum.READ, HttpStatus.OK); @@ -70,10 +71,9 @@ public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { operationToHttpStatusMap.put(OperationEnum.DELETE, HttpStatus.NO_CONTENT); } - @Override public ResponseEntity<ModuleSet> getModuleReferences(final String cmHandle, - final @Valid ModuleReferencesRequest body) { + final @Valid ModuleReferencesRequest body) { // For onap-dmi-plugin we don't need cmHandleProperties, so DataAccessReadRequest is not used. final ModuleSet moduleSet = dmiService.getModulesForCmHandle(cmHandle); return ResponseEntity.ok(moduleSet); @@ -104,66 +104,119 @@ public class DmiRestController implements DmiPluginApi, DmiPluginInternalApi { } /** - * This method fetches the resource for given cm handle using pass through operational. It filters the response on - * the basis of options query parameters and returns response. Does not support write operations. + * This method fetches the resource for given cm handle using pass through operational datastore. It filters the + * response on the basis of options query parameters and returns response. Does not support write operations. * * @param resourceIdentifier resource identifier to fetch data * @param cmHandle cm handle identifier * @param dataAccessRequest data Access Request * @param optionsParamInQuery options query parameter - * @param topicParamInQuery optional topic parameter + * @param topicParamInQuery topic name for (triggering) async responses * @return {@code ResponseEntity} response entity */ @Override public ResponseEntity<Object> dataAccessPassthroughOperational(final String resourceIdentifier, final String cmHandle, - final @Valid DataAccessRequest - dataAccessRequest, + final @Valid DataAccessRequest dataAccessRequest, final @Valid String optionsParamInQuery, final String topicParamInQuery) { if (isReadOperation(dataAccessRequest)) { - final String resourceDataAsJson = dmiService.getResourceData(cmHandle, - resourceIdentifier, - optionsParamInQuery, - DmiService.RESTCONF_CONTENT_PASSTHROUGH_OPERATIONAL_QUERY_PARAM); + if (hasTopic(topicParamInQuery)) { + return handleAsyncRequest(resourceIdentifier, cmHandle, dataAccessRequest, optionsParamInQuery, + topicParamInQuery); + } + + final String resourceDataAsJson = dmiService.getResourceData(cmHandle, resourceIdentifier, + optionsParamInQuery, DmiService.RESTCONF_CONTENT_PASSTHROUGH_OPERATIONAL_QUERY_PARAM); return ResponseEntity.ok(resourceDataAsJson); } return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } + /** + * This method fetches the resource for given cm handle using pass through running datastore. It filters the + * response on the basis of options query parameters and returns response. It supports both read and write + * operation. + * + * @param resourceIdentifier resource identifier to fetch data + * @param cmHandle cm handle identifier + * @param dataAccessRequest data Access Request + * @param optionsParamInQuery options query parameter + * @param topicParamInQuery topic name for (triggering) async responses + * @return {@code ResponseEntity} response entity + */ @Override public ResponseEntity<Object> dataAccessPassthroughRunning(final String resourceIdentifier, final String cmHandle, - final @Valid DataAccessRequest - dataAccessRequest, + final @Valid DataAccessRequest dataAccessRequest, final @Valid String optionsParamInQuery, final String topicParamInQuery) { - final String sdncResponse; - if (isReadOperation(dataAccessRequest)) { - sdncResponse = dmiService.getResourceData(cmHandle, - resourceIdentifier, - optionsParamInQuery, - DmiService.RESTCONF_CONTENT_PASSTHROUGH_RUNNING_QUERY_PARAM); - } else { - sdncResponse = dmiService.writeData( + if (hasTopic(topicParamInQuery)) { + asyncTaskExecutor.executeAsyncTask(() -> + getSdncResponseForPassThroughRunning( + resourceIdentifier, + cmHandle, + dataAccessRequest, + optionsParamInQuery), + topicParamInQuery, + dataAccessRequest.getRequestId(), dataAccessRequest.getOperation(), - cmHandle, - resourceIdentifier, - dataAccessRequest.getDataType(), - dataAccessRequest.getData()); + timeOutInMillis + ); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + + final String sdncResponse = + getSdncResponseForPassThroughRunning(resourceIdentifier, cmHandle, dataAccessRequest, optionsParamInQuery); return new ResponseEntity<>(sdncResponse, operationToHttpStatusMap.get(dataAccessRequest.getOperation())); } + private String getSdncResponseForPassThroughRunning(final String resourceIdentifier, + final String cmHandle, + final DataAccessRequest dataAccessRequest, + final String optionsParamInQuery) { + if (isReadOperation(dataAccessRequest)) { + return dmiService.getResourceData(cmHandle, resourceIdentifier, optionsParamInQuery, + DmiService.RESTCONF_CONTENT_PASSTHROUGH_RUNNING_QUERY_PARAM); + } + + return dmiService.writeData(dataAccessRequest.getOperation(), cmHandle, resourceIdentifier, + dataAccessRequest.getDataType(), dataAccessRequest.getData()); + } + private boolean isReadOperation(final @Valid DataAccessRequest dataAccessRequest) { return dataAccessRequest.getOperation() == null || dataAccessRequest.getOperation().equals(DataAccessRequest.OperationEnum.READ); } private List<ModuleReference> convertRestObjectToJavaApiObject( - final ModuleResourcesReadRequest moduleResourcesReadRequest) { + final ModuleResourcesReadRequest moduleResourcesReadRequest) { return objectMapper .convertValue(moduleResourcesReadRequest.getData().getModules(), - new TypeReference<List<ModuleReference>>() {}); + new TypeReference<List<ModuleReference>>() {}); + } + + private boolean hasTopic(final String topicParamInQuery) { + return !(topicParamInQuery == null || topicParamInQuery.isBlank()); } + + private ResponseEntity<Object> handleAsyncRequest(final String resourceIdentifier, + final String cmHandle, + final DataAccessRequest dataAccessRequest, + final String optionsParamInQuery, + final String topicParamInQuery) { + asyncTaskExecutor.executeAsyncTask(() -> + dmiService.getResourceData( + cmHandle, + resourceIdentifier, + optionsParamInQuery, + DmiService.RESTCONF_CONTENT_PASSTHROUGH_OPERATIONAL_QUERY_PARAM), + topicParamInQuery, + dataAccessRequest.getRequestId(), + dataAccessRequest.getOperation(), + timeOutInMillis + ); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + } |