X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ncmp-rest%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fncmp%2Frest%2Fcontroller%2FNetworkCmProxyController.java;h=2f6668a35178d26eeff6494d49d35a8548a2a074;hb=8f55ef0b08bfde2878b286c7f2d3029a470db3fc;hp=b20487119b59894e38b016fe63ad54cf6374133a;hpb=4e596846aa6f1b799487c553d6830004489d96a1;p=cps.git diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java index b20487119..2f6668a35 100755 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java @@ -29,21 +29,19 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.UUID; import java.util.stream.Collectors; -import javax.validation.Valid; -import javax.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; -import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException; import org.onap.cps.ncmp.api.inventory.CompositeState; import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi; -import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor; +import org.onap.cps.ncmp.rest.controller.handlers.DatastoreType; +import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandler; +import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory; +import org.onap.cps.ncmp.rest.exceptions.InvalidDatastoreException; import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper; import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties; import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters; @@ -53,9 +51,7 @@ import org.onap.cps.ncmp.rest.model.RestOutputCmHandle; import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState; import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties; import org.onap.cps.ncmp.rest.util.DeprecationHelper; -import org.onap.cps.utils.CpsValidator; import org.onap.cps.utils.JsonObjectMapper; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; @@ -68,94 +64,65 @@ import org.springframework.web.bind.annotation.RestController; public class NetworkCmProxyController implements NetworkCmProxyApi { private static final String NO_BODY = null; - private static final String NO_REQUEST_ID = null; - private static final String NO_TOPIC = null; private final NetworkCmProxyDataService networkCmProxyDataService; private final JsonObjectMapper jsonObjectMapper; - private final DeprecationHelper deprecationHelper; private final NcmpRestInputMapper ncmpRestInputMapper; private final CmHandleStateMapper cmHandleStateMapper; - private final CpsNcmpTaskExecutor cpsNcmpTaskExecutor; - @Value("${notification.async.executor.time-out-value-in-ms:2000}") - private int timeOutInMilliSeconds; - @Value("${notification.enabled:true}") - private boolean asyncEnabled; + private final NcmpDatastoreResourceRequestHandlerFactory ncmpDatastoreResourceRequestHandlerFactory; /** - * Get resource data from operational datastore. + * Get resource data from datastore. * - * @param cmHandle cm handle identifier - * @param resourceIdentifier resource identifier + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param resourceIdentifier resource identifier * @param optionsParamInQuery options query parameter - * @param topicParamInQuery topic query parameter + * @param topicParamInQuery topic query parameter + * @param includeDescendants whether include descendants * @return {@code ResponseEntity} response from dmi plugin */ + @Override - public ResponseEntity getResourceDataOperationalForCmHandle(final String cmHandle, - final @NotNull @Valid String resourceIdentifier, - final @Valid String optionsParamInQuery, - final @Valid String topicParamInQuery) { - if (asyncEnabled && isValidTopic(topicParamInQuery)) { - final String requestId = UUID.randomUUID().toString(); - log.info("Received Async passthrough-operational request with id {}", requestId); - cpsNcmpTaskExecutor.executeTask(() -> - networkCmProxyDataService.getResourceDataOperationalForCmHandle( - cmHandle, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId - ), timeOutInMilliSeconds - ); - return ResponseEntity.ok(Map.of("requestId", requestId)); - } else { - log.warn("Asynchronous messaging is currently disabled for passthrough-operational." - + " Will use synchronous operation."); - } + public ResponseEntity getResourceDataForCmHandle(final String datastoreName, + final String cmHandle, + final String resourceIdentifier, + final String optionsParamInQuery, + final String topicParamInQuery, + final Boolean includeDescendants) { - final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle( - cmHandle, resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID); + final NcmpDatastoreResourceRequestHandler ncmpDatastoreResourceRequestHandler = + ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler( + DatastoreType.fromDatastoreName(datastoreName)); - return ResponseEntity.ok(responseObject); + return ncmpDatastoreResourceRequestHandler.getResourceData(cmHandle, resourceIdentifier, + optionsParamInQuery, topicParamInQuery, includeDescendants); } /** - * Get resource data from pass-through running datastore. + * Patch resource data from passthrough-running. * - * @param cmHandle cm handle identifier * @param resourceIdentifier resource identifier - * @param optionsParamInQuery options query parameter - * @param topicParamInQuery topic query parameter + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param requestBody the request body + * @param contentType content type of body * @return {@code ResponseEntity} response from dmi plugin */ - @Override - public ResponseEntity getResourceDataRunningForCmHandle(final String cmHandle, - final @NotNull @Valid String resourceIdentifier, - final @Valid String optionsParamInQuery, - final @Valid String topicParamInQuery) { - if (asyncEnabled && isValidTopic(topicParamInQuery)) { - final String requestId = UUID.randomUUID().toString(); - log.info("Received Async passthrough-running request with id {}", requestId); - cpsNcmpTaskExecutor.executeTask(() -> - networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle( - cmHandle, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId - ), timeOutInMilliSeconds - ); - return ResponseEntity.ok(Map.of("requestId", requestId)); - } else { - log.warn("Asynchronous messaging is currently disabled for passthrough-running." - + " Will use synchronous operation."); - } - - final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle( - cmHandle, resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID); - - return ResponseEntity.ok(responseObject); - } @Override public ResponseEntity patchResourceDataRunningForCmHandle(final String resourceIdentifier, - final String cmHandle, - final Object requestBody, final String contentType) { - final Object responseObject = networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, PATCH, jsonObjectMapper.asJsonString(requestBody), contentType); + final String datastoreName, + final String cmHandle, + final Object requestBody, + final String contentType) { + + acceptPassthroughRunningOnly(datastoreName); + + final Object responseObject = networkCmProxyDataService + .writeResourceDataPassThroughRunningForCmHandle( + cmHandle, resourceIdentifier, PATCH, + jsonObjectMapper.asJsonString(requestBody), contentType); return ResponseEntity.ok(responseObject); } @@ -163,14 +130,21 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Create resource data in datastore pass-through running for given cm-handle. * * @param resourceIdentifier resource identifier - * @param cmHandle cm handle identifier - * @param requestBody the request body - * @param contentType content type of body + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param requestBody the request body + * @param contentType content type of body * @return {@code ResponseEntity} response from dmi plugin */ @Override public ResponseEntity createResourceDataRunningForCmHandle(final String resourceIdentifier, - final String cmHandle, final Object requestBody, final String contentType) { + final String datastoreName, + final String cmHandle, + final Object requestBody, + final String contentType) { + + acceptPassthroughRunningOnly(datastoreName); + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType); return new ResponseEntity<>(HttpStatus.CREATED); @@ -180,36 +154,45 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Update resource data in datastore pass-through running for given cm-handle. * * @param resourceIdentifier resource identifier - * @param cmHandle cm handle identifier - * @param requestBody the request body - * @param contentType content type of the body + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param requestBody the request body + * @param contentType content type of the body * @return response entity */ + @Override public ResponseEntity updateResourceDataRunningForCmHandle(final String resourceIdentifier, + final String datastoreName, final String cmHandle, final Object requestBody, final String contentType) { + acceptPassthroughRunningOnly(datastoreName); + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType); return new ResponseEntity<>(HttpStatus.OK); } - /** - * Delete resource data in datastore pass-through running for a given cm-handle. + * Delete resource data in datastore pass-through running for a given cm-handle. * + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier * @param resourceIdentifier resource identifier - * @param cmHandle cm handle identifier - * @param contentType content type of the body + * @param contentType content type of the body * @return response entity no content if request is successful */ @Override - public ResponseEntity deleteResourceDataRunningForCmHandle(final String cmHandle, + public ResponseEntity deleteResourceDataRunningForCmHandle(final String datastoreName, + final String cmHandle, final String resourceIdentifier, final String contentType) { + + acceptPassthroughRunningOnly(datastoreName); + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, DELETE, NO_BODY, contentType); + resourceIdentifier, DELETE, NO_BODY, contentType); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @@ -240,7 +223,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { */ @Override public ResponseEntity> searchCmHandleIds( - final CmHandleQueryParameters cmHandleQueryParameters) { + final CmHandleQueryParameters cmHandleQueryParameters) { final CmHandleQueryApiParameters cmHandleQueryApiParameters = jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class); final Set cmHandleIds = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters); @@ -249,6 +232,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { /** * Search for Cm Handle and Properties by Name. + * * @param cmHandleId cm-handle identifier * @return cm handle and its properties */ @@ -261,33 +245,35 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { /** * Get Cm Handle Properties by Cm Handle Id. + * * @param cmHandleId cm-handle identifier * @return cm handle properties */ @Override public ResponseEntity getCmHandlePublicPropertiesByCmHandleId( - final String cmHandleId) { + final String cmHandleId) { final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties(); cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId)); final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties = - new RestOutputCmHandlePublicProperties(); + new RestOutputCmHandlePublicProperties(); restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties); return ResponseEntity.ok(restOutputCmHandlePublicProperties); } /** * Get Cm Handle State by Cm Handle Id. + * * @param cmHandleId cm-handle identifier * @return cm handle state */ @Override public ResponseEntity getCmHandleStateByCmHandleId( - final String cmHandleId) { + final String cmHandleId) { final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId); final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState = - new RestOutputCmHandleCompositeState(); + new RestOutputCmHandleCompositeState(); restOutputCmHandleCompositeState.setState( - cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState)); + cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState)); return ResponseEntity.ok(restOutputCmHandleCompositeState); } @@ -314,12 +300,27 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { */ public ResponseEntity> getModuleReferencesByCmHandle(final String cmHandle) { final List restModuleReferences = - networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream() - .map(ncmpRestInputMapper::toRestModuleReference) - .collect(Collectors.toList()); + networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream() + .map(ncmpRestInputMapper::toRestModuleReference) + .collect(Collectors.toList()); return new ResponseEntity<>(restModuleReferences, HttpStatus.OK); } + /** + * Set the data sync enabled flag, along with the data sync state for the specified cm handle. + * + * @param cmHandleId cm handle id + * @param dataSyncEnabledFlag data sync enabled flag + * @return response entity ok if request is successful + */ + @Override + public ResponseEntity setDataSyncEnabledFlagForCmHandle(final String cmHandleId, + final Boolean dataSyncEnabledFlag) { + networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag); + return new ResponseEntity<>(HttpStatus.OK); + } + + private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) { final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle(); final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties(); @@ -331,15 +332,12 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { return restOutputCmHandle; } - private static boolean isValidTopic(final String topicName) { - if (topicName == null) { - return false; - } - if (CpsValidator.validateTopicName(topicName)) { - return true; + private void acceptPassthroughRunningOnly(final String datastoreName) { + final DatastoreType datastoreType = DatastoreType.fromDatastoreName(datastoreName); + + if (DatastoreType.PASSTHROUGH_RUNNING != datastoreType) { + throw new InvalidDatastoreException(datastoreName + " is not supported"); } - throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); } - }