diff options
12 files changed, 183 insertions, 264 deletions
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 a8bc3aec76..324c1ae2d4 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 @@ -39,8 +39,9 @@ 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.controller.handlers.DatastoreType; +import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler; import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreRequestHandler; -import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory; +import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler; import org.onap.cps.ncmp.rest.exceptions.InvalidDatastoreException; import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper; import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties; @@ -69,17 +70,18 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { private final DeprecationHelper deprecationHelper; private final NcmpRestInputMapper ncmpRestInputMapper; private final CmHandleStateMapper cmHandleStateMapper; - private final NcmpDatastoreResourceRequestHandlerFactory ncmpDatastoreResourceRequestHandlerFactory; + private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler; + private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler; /** * Get resource data from datastore. * - * @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 includeDescendantsAsObject whether include descendants + * @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 includeDescendants whether to include descendants or not * @return {@code ResponseEntity} response from dmi plugin */ @@ -89,15 +91,11 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, - final Boolean includeDescendantsAsObject) { + final Boolean includeDescendants) { - final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = - ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - DatastoreType.fromDatastoreName(datastoreName)); - final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject); - - return ncmpDatastoreRequestHandler.executeRequest(cmHandle, resourceIdentifier, + final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName); + return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandle, resourceIdentifier, optionsParamInQuery, topicParamInQuery, includeDescendants); } @@ -107,30 +105,25 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { final String datastoreName, final Object requestBody, final String optionsParamInQuery, - final Boolean includeDescendantsAsObject) { + final Boolean includeDescendants) { - final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = - ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - DatastoreType.fromDatastoreName(datastoreName)); + final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName); final List<String> cmHandleIds = jsonObjectMapper.convertJsonString(jsonObjectMapper.asJsonString(requestBody), List.class); - - final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject); - - return ncmpDatastoreRequestHandler.executeRequest(cmHandleIds, resourceIdentifier, + return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandleIds, resourceIdentifier, optionsParamInQuery, topicParamInQuery, includeDescendants); } /** * Query resource data from datastore. * - * @param datastoreName name of the datastore - * @param cmHandle cm handle identifier - * @param cpsPath CPS Path - * @param optionsParamInQuery options query parameter - * @param topicParamInQuery topic query parameter - * @param includeDescendantsAsObject whether include descendants + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param cpsPath CPS Path + * @param optionsParamInQuery options query parameter + * @param topicParamInQuery topic query parameter + * @param includeDescendants whether to include descendants or not * @return {@code ResponseEntity} response from dmi plugin */ @@ -140,14 +133,8 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { final String cpsPath, final String optionsParamInQuery, final String topicParamInQuery, - final Boolean includeDescendantsAsObject) { + final Boolean includeDescendants) { validateDataStore(DatastoreType.OPERATIONAL, datastoreName); - final NcmpDatastoreRequestHandler ncmpCachedResourceRequestHandler = - ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - DatastoreType.fromDatastoreName(datastoreName)); - - final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject); - return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants); } @@ -393,11 +380,13 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { } } - private static boolean toPrimitiveFlag(final Boolean includeDescendantsAsObject) { - if (includeDescendantsAsObject == null) { - return false; + private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) { + if (DatastoreType.OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) { + return ncmpCachedResourceRequestHandler; } - return includeDescendantsAsObject.booleanValue(); + return ncmpPassthroughResourceRequestHandler; } + + } diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpCachedResourceRequestHandler.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpCachedResourceRequestHandler.java index 620f64782b..76946d3af2 100644 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpCachedResourceRequestHandler.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpCachedResourceRequestHandler.java @@ -21,22 +21,36 @@ package org.onap.cps.ncmp.rest.controller.handlers; import java.util.function.Supplier; -import lombok.RequiredArgsConstructor; -import lombok.Setter; +import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.api.NetworkCmProxyQueryService; +import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor; import org.onap.cps.spi.FetchDescendantsOption; import org.springframework.stereotype.Component; -@RequiredArgsConstructor @Component public class NcmpCachedResourceRequestHandler extends NcmpDatastoreRequestHandler { - @Setter - private String dataStoreName; + private final NetworkCmProxyDataService networkCmProxyDataService; private final NetworkCmProxyQueryService networkCmProxyQueryService; + /** + * Constructor. + * + * @param cpsNcmpTaskExecutor @see org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor + * @param networkCmProxyDataService @see org.onap.cps.ncmp.api.NetworkCmProxyDataService + * @param networkCmProxyQueryService @see org.onap.cps.ncmp.api.NetworkCmProxyQueryService + */ + public NcmpCachedResourceRequestHandler(final CpsNcmpTaskExecutor cpsNcmpTaskExecutor, + final NetworkCmProxyDataService networkCmProxyDataService, + final NetworkCmProxyQueryService networkCmProxyQueryService) { + super(cpsNcmpTaskExecutor); + this.networkCmProxyDataService = networkCmProxyDataService; + this.networkCmProxyQueryService = networkCmProxyQueryService; + } + @Override - public Supplier<Object> getTaskSupplierForGetRequest(final String cmHandleId, + public Supplier<Object> getTaskSupplierForGetRequest(final String datastoreName, + final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, @@ -46,7 +60,7 @@ public class NcmpCachedResourceRequestHandler extends NcmpDatastoreRequestHandle final FetchDescendantsOption fetchDescendantsOption = TaskManagementDefaultHandler.getFetchDescendantsOption(includeDescendants); - return () -> networkCmProxyDataService.getResourceDataForCmHandle(dataStoreName, cmHandleId, resourceIdentifier, + return () -> networkCmProxyDataService.getResourceDataForCmHandle(datastoreName, cmHandleId, resourceIdentifier, fetchDescendantsOption); } diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandler.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandler.java index 050e724b2c..a32c462e76 100644 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandler.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandler.java @@ -24,34 +24,32 @@ import java.util.List; import java.util.Map; import java.util.UUID; import java.util.function.Supplier; -import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor; import org.onap.cps.ncmp.rest.util.TopicValidator; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -@NoArgsConstructor @Slf4j @Service +@RequiredArgsConstructor public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler { @Value("${notification.async.executor.time-out-value-in-ms:2000}") protected int timeOutInMilliSeconds; + @Value("${notification.enabled:true}") protected boolean notificationFeatureEnabled; - @Autowired - protected NetworkCmProxyDataService networkCmProxyDataService; - @Autowired - protected CpsNcmpTaskExecutor cpsNcmpTaskExecutor; + + private final CpsNcmpTaskExecutor cpsNcmpTaskExecutor; /** * Executes synchronous/asynchronous request for given cm handle. * + * @param datastoreName the name of the datastore * @param cmHandleId the cm handle * @param resourceIdentifier the resource identifier * @param optionsParamInQuery the options param in query @@ -59,7 +57,8 @@ public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler * @param includeDescendants whether include descendants * @return the response entity */ - public ResponseEntity<Object> executeRequest(final String cmHandleId, + public ResponseEntity<Object> executeRequest(final String datastoreName, + final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, @@ -67,15 +66,15 @@ public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler final boolean asyncResponseRequested = topicParamInQuery != null; if (asyncResponseRequested && notificationFeatureEnabled) { - return executeAsyncTaskAndGetResponseEntity(cmHandleId, resourceIdentifier, optionsParamInQuery, - topicParamInQuery, includeDescendants, false); + return executeAsyncTaskAndGetResponseEntity(datastoreName, cmHandleId, resourceIdentifier, + optionsParamInQuery, topicParamInQuery, includeDescendants, false); } if (asyncResponseRequested) { log.warn("Asynchronous request is unavailable as notification feature is currently disabled, " + "will use synchronous operation."); } - final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(cmHandleId, + final Supplier<Object> taskSupplier = getTaskSupplierForGetRequest(datastoreName, cmHandleId, resourceIdentifier, optionsParamInQuery, NO_TOPIC, NO_REQUEST_ID, includeDescendants); return executeTaskSync(taskSupplier); } @@ -101,20 +100,22 @@ public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler /** * Executes synchronous/asynchronous request for batch of cm handles. * + * @param datastoreName the name of the datastore * @param cmHandleIds list of cm handles * @param resourceIdentifier the resource identifier * @param optionsParamInQuery the options param in query * @param topicParamInQuery the topic param in query - * @param includeDescendants whether include descendants + * @param includeDescendants whether to include descendants or not * @return the response entity */ - public ResponseEntity<Object> executeRequest(final List<String> cmHandleIds, + public ResponseEntity<Object> executeRequest(final String datastoreName, + final List<String> cmHandleIds, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, final boolean includeDescendants) { - return executeAsyncTaskAndGetResponseEntity(cmHandleIds, resourceIdentifier, optionsParamInQuery, + return executeAsyncTaskAndGetResponseEntity(datastoreName, cmHandleIds, resourceIdentifier, optionsParamInQuery, topicParamInQuery, includeDescendants, true); } @@ -134,7 +135,8 @@ public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler return ResponseEntity.ok(taskSupplier.get()); } - private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final Object targetObject, + private ResponseEntity<Object> executeAsyncTaskAndGetResponseEntity(final String datastoreName, + final Object targetObject, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, @@ -143,10 +145,10 @@ public class NcmpDatastoreRequestHandler implements TaskManagementDefaultHandler final String requestId = UUID.randomUUID().toString(); final Supplier<Object> taskSupplier; if (isBulkRequest) { - taskSupplier = getTaskSupplierForBulkRequest((List<String>) targetObject, + taskSupplier = getTaskSupplierForBulkRequest(datastoreName, (List<String>) targetObject, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants); } else { - taskSupplier = getTaskSupplierForGetRequest(targetObject.toString(), resourceIdentifier, + taskSupplier = getTaskSupplierForGetRequest(datastoreName, targetObject.toString(), resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId, includeDescendants); } if (taskSupplier == NO_OBJECT_SUPPLIER) { diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreResourceRequestHandlerFactory.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreResourceRequestHandlerFactory.java deleted file mode 100644 index 9a71798fa1..0000000000 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreResourceRequestHandlerFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.rest.controller.handlers; - -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Component; - -@Component -@RequiredArgsConstructor -public class NcmpDatastoreResourceRequestHandlerFactory { - private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler; - private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler; - - /** - * Gets ncmp datastore handler. - * - * @param datastoreType the datastore type - * @return the ncmp datastore handler - */ - public NcmpDatastoreRequestHandler getNcmpResourceRequestHandler(final DatastoreType datastoreType) { - - switch (datastoreType) { - case OPERATIONAL: - ncmpCachedResourceRequestHandler.setDataStoreName(datastoreType.getDatastoreName()); - return ncmpCachedResourceRequestHandler; - case PASSTHROUGH_RUNNING: - case PASSTHROUGH_OPERATIONAL: - default: - ncmpPassthroughResourceRequestHandler.setDataStoreName(datastoreType.getDatastoreName()); - return ncmpPassthroughResourceRequestHandler; - } - } -} diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpPassthroughResourceRequestHandler.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpPassthroughResourceRequestHandler.java index ab5d587e93..18e5a9f5ac 100644 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpPassthroughResourceRequestHandler.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/NcmpPassthroughResourceRequestHandler.java @@ -22,17 +22,30 @@ package org.onap.cps.ncmp.rest.controller.handlers; import java.util.List; import java.util.function.Supplier; -import lombok.Setter; +import org.onap.cps.ncmp.api.NetworkCmProxyDataService; +import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor; import org.springframework.stereotype.Component; @Component public class NcmpPassthroughResourceRequestHandler extends NcmpDatastoreRequestHandler { - @Setter - private String dataStoreName; + private final NetworkCmProxyDataService networkCmProxyDataService; + + /** + * Constructor. + * + * @param cpsNcmpTaskExecutor @see org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor + * @param networkCmProxyDataService @see org.onap.cps.ncmp.api.NetworkCmProxyDataService + */ + public NcmpPassthroughResourceRequestHandler(final CpsNcmpTaskExecutor cpsNcmpTaskExecutor, + final NetworkCmProxyDataService networkCmProxyDataService) { + super(cpsNcmpTaskExecutor); + this.networkCmProxyDataService = networkCmProxyDataService; + } @Override - public Supplier<Object> getTaskSupplierForGetRequest(final String cmHandleId, + public Supplier<Object> getTaskSupplierForGetRequest(final String datastoreName, + final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, @@ -40,19 +53,20 @@ public class NcmpPassthroughResourceRequestHandler extends NcmpDatastoreRequestH final boolean includeDescendants) { return () -> networkCmProxyDataService.getResourceDataForCmHandle( - dataStoreName, cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId); + datastoreName, cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId); } @Override - public Supplier<Object> getTaskSupplierForBulkRequest(final List<String> cmHandleIds, - final String resourceIdentifier, - final String optionsParamInQuery, - final String topicParamInQuery, - final String requestId, - final boolean includeDescendants) { + public Supplier<Object> getTaskSupplierForBulkRequest(final String datastoreName, + final List<String> cmHandleIds, + final String resourceIdentifier, + final String optionsParamInQuery, + final String topicParamInQuery, + final String requestId, + final boolean includeDescendants) { return () -> networkCmProxyDataService.getResourceDataForCmHandleBatch( - dataStoreName, cmHandleIds, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId); + datastoreName, cmHandleIds, resourceIdentifier, optionsParamInQuery, topicParamInQuery, requestId); } } diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/TaskManagementDefaultHandler.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/TaskManagementDefaultHandler.java index 08e8407c63..6d68f76802 100644 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/TaskManagementDefaultHandler.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/handlers/TaskManagementDefaultHandler.java @@ -30,24 +30,24 @@ public interface TaskManagementDefaultHandler { String NO_TOPIC = null; Supplier<Object> NO_OBJECT_SUPPLIER = null; - default Supplier<Object> getTaskSupplierForGetRequest(final String cmHandleId, + default Supplier<Object> getTaskSupplierForGetRequest(final String datastoreName, + final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, final String requestId, final boolean includeDescendant) { return NO_OBJECT_SUPPLIER; - } default Supplier<Object> getTaskSupplierForQueryRequest(final String cmHandleId, final String resourceIdentifier, final boolean includeDescendant) { return NO_OBJECT_SUPPLIER; - } - default Supplier<Object> getTaskSupplierForBulkRequest(final List<String> cmHandleIds, + default Supplier<Object> getTaskSupplierForBulkRequest(final String datastoreName, + final List<String> cmHandleIds, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutor.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutor.java index 5adbb252a0..0543c4fba3 100644 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutor.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/executor/CpsNcmpTaskExecutor.java @@ -32,7 +32,7 @@ import org.springframework.stereotype.Service; public class CpsNcmpTaskExecutor { /** - * Execute task asynchronously and publish response to supplied topic. + * Execute a task asynchronously. * * @param taskSupplier functional method is get() task need to executed asynchronously * @param timeOutInMillis the time out value in milliseconds diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy index 9531101e1d..9c22f7ce12 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/NetworkCmProxyControllerSpec.groovy @@ -35,7 +35,6 @@ import org.onap.cps.ncmp.api.inventory.LockReasonCategory import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler -import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper import org.onap.cps.ncmp.rest.util.DeprecationHelper @@ -70,13 +69,13 @@ import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.DELETE import static org.onap.cps.ncmp.rest.controller.handlers.DatastoreType.PASSTHROUGH_OPERATIONAL import static org.onap.cps.ncmp.rest.controller.handlers.DatastoreType.PASSTHROUGH_RUNNING import static org.onap.cps.ncmp.rest.controller.handlers.DatastoreType.OPERATIONAL +import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS; +import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; + @WebMvcTest(NetworkCmProxyController) class NetworkCmProxyControllerSpec extends Specification { - private static final int TIMEOUT_IN_MS = 2000 - private static final boolean NOTIFICATION_ENABLED = true - @Autowired MockMvc mvc @@ -105,7 +104,10 @@ class NetworkCmProxyControllerSpec extends Specification { DeprecationHelper stubbedDeprecationHelper = Stub() @SpringBean - NcmpDatastoreResourceRequestHandlerFactory stubbedNcmpDatastoreResourceRequestHandlerFactory = Stub() + NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler = new NcmpCachedResourceRequestHandler(spiedCpsTaskExecutor, mockNetworkCmProxyDataService, mockNetworkCmProxyQueryService) + + @SpringBean + NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler = new NcmpPassthroughResourceRequestHandler(spiedCpsTaskExecutor, mockNetworkCmProxyDataService) @Value('${rest.api.ncmp-base-path}/v1') def ncmpBasePathV1 @@ -113,23 +115,18 @@ class NetworkCmProxyControllerSpec extends Specification { def requestBody = '{"some-key":"some-value"}' def bulkRequestBody = '["testCmHandle"]' + def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC)) + @Shared def NO_TOPIC = null def NO_REQUEST_ID = null + def TIMOUT_FOR_TEST = 1234 - def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") - .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC)) - - void setup() { - stubbedNcmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - OPERATIONAL) >> getNcmpDatastoreRequestHandler(OPERATIONAL,new NcmpCachedResourceRequestHandler(mockNetworkCmProxyQueryService)) - - stubbedNcmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - PASSTHROUGH_OPERATIONAL) >> getNcmpDatastoreRequestHandler(PASSTHROUGH_OPERATIONAL,new NcmpPassthroughResourceRequestHandler()) - - stubbedNcmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler( - PASSTHROUGH_RUNNING) >> getNcmpDatastoreRequestHandler(PASSTHROUGH_RUNNING,new NcmpPassthroughResourceRequestHandler()) - + def setup() { + ncmpCachedResourceRequestHandler.notificationFeatureEnabled = true + ncmpCachedResourceRequestHandler.timeOutInMilliSeconds = TIMOUT_FOR_TEST + ncmpPassthroughResourceRequestHandler.notificationFeatureEnabled = true + ncmpPassthroughResourceRequestHandler.timeOutInMilliSeconds = TIMOUT_FOR_TEST } def 'Get Resource Data from pass-through operational.'() { @@ -148,32 +145,49 @@ class NetworkCmProxyControllerSpec extends Specification { response.status == HttpStatus.OK.value() } - def 'Get Resource Data from #datastoreInUrl with #scenario.'() { + def 'Get Resource Data Async Topic Handling with #scenario.'() { given: 'resource data url' - def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:${datastoreInUrl}" + - "?resourceIdentifier=parent/child&options=(a=1,b=2)${topicQueryParam}" + def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:passthrough-operational?resourceIdentifier=parent/child&${topicQueryParam}" when: 'get data resource request is performed' def response = mvc.perform( get(getUrl).contentType(MediaType.APPLICATION_JSON)).andReturn().response then: 'task executor is called appropriate number of times' - expectedNumberOfExecutorExecutions * spiedCpsTaskExecutor.executeTask(_, TIMEOUT_IN_MS) - and: 'response status is expected' + expectedNumberOfTaskExecutions * spiedCpsTaskExecutor.executeTask(_, TIMOUT_FOR_TEST) + and: 'response status is OK' response.status == HttpStatus.OK.value() where: 'the following parameters are used' - scenario | datastoreInUrl | topicQueryParam || expectedTopicName | expectedNumberOfExecutorExecutions - 'url with valid topic' | 'passthrough-operational' | '&topic=my-topic-name' || 'my-topic-name' | 1 - 'no topic in url' | 'passthrough-operational' | '' || NO_TOPIC | 0 - 'null topic in url' | 'passthrough-operational' | '&topic=null' || 'null' | 1 - 'url with valid topic' | 'passthrough-running' | '&topic=my-topic-name' || 'my-topic-name' | 1 - 'no topic in url' | 'passthrough-running' | '' || NO_TOPIC | 0 - 'null topic in url' | 'passthrough-running' | '&topic=null' || 'null' | 1 + scenario | datastoreInUrl | topicQueryParam || expectedNumberOfTaskExecutions + 'url with valid topic' | 'passthrough-operational' | '&topic=my-topic-name' || 1 + 'no topic in url' | 'passthrough-operational' | '' || 0 + 'null topic in url' | 'passthrough-operational' | '&topic=null' || 1 } - def 'Fail to get Resource Data from #datastoreInUrl when #scenario.'() { + def 'Get Resource Data from ncmp-datastore:operational (cached) parameters handling with #scenario.'() { + given: 'resource data url' + def getUrl = "$ncmpBasePathV1/ch/h123/data/ds/ncmp-datastore:operational" + + "?resourceIdentifier=parent/child${additionalUrlParam}" + when: 'get data resource request is performed' + def response = mvc.perform( + get(getUrl).contentType(MediaType.APPLICATION_JSON)).andReturn().response + then: 'task executor is called appropriate number of times' + 1 * mockNetworkCmProxyDataService.getResourceDataForCmHandle('ncmp-datastore:operational', 'h123', 'parent/child', expectedIncludeDescendants) + and: 'response status is OK' + response.status == HttpStatus.OK.value() + where: 'the following parameters are used' + scenario | additionalUrlParam || expectedIncludeDescendants + 'no additional param' | '' || OMIT_DESCENDANTS + 'include descendants true' | '&include-descendants=true' || INCLUDE_ALL_DESCENDANTS + 'include descendants TRUE' | '&include-descendants=true' || INCLUDE_ALL_DESCENDANTS + 'include descendants false' | '&include-descendants=false' || OMIT_DESCENDANTS + 'include descendants FALSE' | '&include-descendants=FALSE' || OMIT_DESCENDANTS + 'options (ignored)' | '&options=(a-=1)' || OMIT_DESCENDANTS + } + + def 'Get Resource Data with invalid topic parameter: #scenario.'() { given: 'resource data url' def getUrl = "$ncmpBasePathV1/ch/testCmHandle/data/ds/ncmp-datastore:${datastoreInUrl}" + "?resourceIdentifier=parent/child&options=(a=1,b=2)${topicQueryParam}" - when: 'get data resource request is performed' + when: 'get data resource (async) request is performed' def response = mvc.perform( get(getUrl).contentType(MediaType.APPLICATION_JSON)).andReturn().response then: 'abad request is returned' @@ -184,15 +198,11 @@ class NetworkCmProxyControllerSpec extends Specification { 'missing topic in url' | 'passthrough-operational' | '&topic=' 'blank topic value in url' | 'passthrough-operational' | '&topic=\" \"' 'invalid non-empty topic value in url' | 'passthrough-operational' | '&topic=1_5_*_#' - 'empty topic in url' | 'passthrough-running' | '&topic=\"\"' - 'missing topic in url' | 'passthrough-running' | '&topic=' - 'blank topic value in url' | 'passthrough-running' | '&topic=\" \"' - 'invalid non-empty topic value in url' | 'passthrough-running' | '&topic=1_5_*_#' } - def 'Get bulk resource data for #datastoreName from dmi service.'() { + def 'Get (async) bulk resource data from dmi service.'() { given: 'bulk resource data url' - def getUrl = "$ncmpBasePathV1/batch/data/ds/${datastoreName}" + + def getUrl = "$ncmpBasePathV1/batch/data/ds/${datastore.datastoreName}" + "?resourceIdentifier=parent/child&options=(a=1,b=2)&topic=myTopic" when: 'post data resource request is performed' def response = mvc.perform( @@ -202,18 +212,18 @@ class NetworkCmProxyControllerSpec extends Specification { ).andReturn().response then: 'response status is Ok' response.status == HttpStatus.OK.value() - // TODO Need to be un-commented as it's failing into onap CICD pipeline - // but passed into nordix and local build. - //and: 'the NCMP data service is called with getResourceDataForCmHandleBatch' - // 1 * mockNetworkCmProxyDataService.getResourceDataForCmHandleBatch(datastoreName, ['testCmHandle'], - // 'parent/child', - // '(a=1,b=2)', - // 'myTopic', - // _) and: 'async request id is generated' assert response.contentAsString.contains("requestId") + then: 'wait a little to allow execution of service method by task executor (on separate thread)' + Thread.sleep(100); + then: 'the service has been invoked with the correct parameters ' + 1 * mockNetworkCmProxyDataService.getResourceDataForCmHandleBatch(datastore.datastoreName, ['testCmHandle'], + 'parent/child', + '(a=1,b=2)', + 'myTopic', + _) where: 'the following data stores are used' - datastoreName << [PASSTHROUGH_RUNNING.datastoreName, PASSTHROUGH_OPERATIONAL.datastoreName] + datastore << [PASSTHROUGH_RUNNING, PASSTHROUGH_OPERATIONAL] } def 'Get bulk resource data for non-supported #datastoreName from dmi service.'() { @@ -228,8 +238,6 @@ class NetworkCmProxyControllerSpec extends Specification { ).andReturn().response then: 'response status code is 501 not implemented' response.status == HttpStatus.NOT_IMPLEMENTED.value() - where: 'the following data store is un-supported' - datastoreName << [OPERATIONAL.datastoreName] } def 'Query Resource Data from operational.'() { @@ -648,19 +656,5 @@ class NetworkCmProxyControllerSpec extends Specification { return assertContainsAll(response, expectedContent) } - def getNcmpDatastoreRequestHandler(dataStoreType, ncmpDatastoreRequestHandler) { - if (ncmpDatastoreRequestHandler instanceof NcmpCachedResourceRequestHandler) { - NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler = (NcmpCachedResourceRequestHandler) ncmpDatastoreRequestHandler - ncmpCachedResourceRequestHandler.dataStoreName = dataStoreType.datastoreName - } else { - NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler = (NcmpPassthroughResourceRequestHandler) ncmpDatastoreRequestHandler - ncmpPassthroughResourceRequestHandler.dataStoreName = dataStoreType.datastoreName - } - ncmpDatastoreRequestHandler.networkCmProxyDataService = mockNetworkCmProxyDataService - ncmpDatastoreRequestHandler.cpsNcmpTaskExecutor = spiedCpsTaskExecutor - ncmpDatastoreRequestHandler.notificationFeatureEnabled = NOTIFICATION_ENABLED - ncmpDatastoreRequestHandler.timeOutInMilliSeconds = TIMEOUT_IN_MS - return ncmpDatastoreRequestHandler - } } diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandlerFactorySpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandlerFactorySpec.groovy deleted file mode 100644 index 15b3ee6c1e..0000000000 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/controller/handlers/NcmpDatastoreRequestHandlerFactorySpec.groovy +++ /dev/null @@ -1,47 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ncmp.rest.controller.handlers - -import org.spockframework.spring.SpringBean -import spock.lang.Specification - -class NcmpDatastoreRequestHandlerFactorySpec extends Specification { - - @SpringBean - NcmpCachedResourceRequestHandler mockNcmpCachedResourceRequestHandler = new NcmpCachedResourceRequestHandler(null) - - @SpringBean - NcmpPassthroughResourceRequestHandler mockNcmpPassthroughResourceRequestHandler = new NcmpPassthroughResourceRequestHandler() - - def objectUnderTest = new NcmpDatastoreResourceRequestHandlerFactory(mockNcmpCachedResourceRequestHandler, mockNcmpPassthroughResourceRequestHandler) - - def 'Creating ncmp datastore request handlers.'() { - when: 'a ncmp datastore request handler is created for #datastoreType' - def result = objectUnderTest.getNcmpResourceRequestHandler(datastoreType) - then: 'the result is of the expected class' - result.class == expectedClass - where: 'the following type of datastore is used' - datastoreType || expectedClass - DatastoreType.OPERATIONAL || NcmpCachedResourceRequestHandler - DatastoreType.PASSTHROUGH_OPERATIONAL || NcmpPassthroughResourceRequestHandler - DatastoreType.PASSTHROUGH_RUNNING || NcmpPassthroughResourceRequestHandler - } -} diff --git a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy index 9d1077fd2f..f44d6c9907 100644 --- a/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy +++ b/cps-ncmp-rest/src/test/groovy/org/onap/cps/ncmp/rest/exceptions/NetworkCmProxyRestExceptionHandlerSpec.groovy @@ -29,7 +29,8 @@ import org.onap.cps.ncmp.api.impl.exception.DmiRequestException import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException import org.onap.cps.ncmp.rest.controller.NcmpRestInputMapper -import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory +import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler +import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler import org.onap.cps.ncmp.rest.executor.CpsNcmpTaskExecutor import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper import org.onap.cps.ncmp.rest.util.DeprecationHelper @@ -80,7 +81,10 @@ class NetworkCmProxyRestExceptionHandlerSpec extends Specification { DeprecationHelper stubbedDeprecationHelper = Stub() @SpringBean - NcmpDatastoreResourceRequestHandlerFactory mockedNcmpDatastoreResourceRequestHandlerFactory = Mock() + NcmpCachedResourceRequestHandler stubbedNcmpCachedResourceRequestHandler = Stub() + + @SpringBean + NcmpPassthroughResourceRequestHandler StubbedNcmpPassthroughResourceRequestHandler = Stub() @Value('${rest.api.ncmp-base-path}') def basePathNcmp diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index 03737bc51b..05490d8fc9 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -53,7 +53,7 @@ public interface NetworkCmProxyDataService { /** * Get resource data for given data store using dmi. * - * @param dataStoreName data store name + * @param datastoreName datastore name * @param cmHandleId cm handle identifier * @param resourceIdentifier resource identifier * @param optionsParamInQuery options query @@ -61,7 +61,7 @@ public interface NetworkCmProxyDataService { * @param requestId unique requestId for async request * @return {@code Object} resource data */ - Object getResourceDataForCmHandle(String dataStoreName, + Object getResourceDataForCmHandle(String datastoreName, String cmHandleId, String resourceIdentifier, String optionsParamInQuery, @@ -71,13 +71,13 @@ public interface NetworkCmProxyDataService { /** * Get resource data for operational. * - * @param dataStoreName data store name - * @param cmHandleId cm handle identifier + * @param datastoreName datastore name + * @param cmHandleId cm handle identifier * @param resourceIdentifier resource identifier * @Link FetchDescendantsOption fetch descendants option * @return {@code Object} resource data */ - Object getResourceDataForCmHandle(String dataStoreName, + Object getResourceDataForCmHandle(String datastoreName, String cmHandleId, String resourceIdentifier, FetchDescendantsOption fetchDescendantsOption); @@ -85,7 +85,7 @@ public interface NetworkCmProxyDataService { /** * Get resource data for given batch of cm handles using dmi. * - * @param dataStoreName data store name + * @param datastoreName datastore name * @param cmHandleIds cm handle identifiers * @param resourceIdentifier resource identifier * @param optionsParamInQuery options query @@ -93,7 +93,7 @@ public interface NetworkCmProxyDataService { * @param requestId unique requestId for async request * @return {@code Object} resource data */ - Object getResourceDataForCmHandleBatch(String dataStoreName, + Object getResourceDataForCmHandleBatch(String datastoreName, List<String> cmHandleIds, String resourceIdentifier, String optionsParamInQuery, @@ -102,13 +102,13 @@ public interface NetworkCmProxyDataService { /** - * Write resource data for data store pass-through running - * using dmi for given cm-handle. - * @param cmHandleId cm handle identifier + * Write resource data for data store pass-through running using dmi for given cm-handle. + * + * @param cmHandleId cm handle identifier * @param resourceIdentifier resource identifier - * @param operation required operation - * @param requestBody request body to create resource - * @param contentType content type in body + * @param operation required operation + * @param requestBody request body to create resource + * @param contentType content type in body * @return {@code Object} return data */ Object writeResourceDataPassThroughRunningForCmHandle(String cmHandleId, diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 1b1997f23a..e478b0053b 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -114,13 +114,13 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } @Override - public Object getResourceDataForCmHandle(final String dataStoreName, + public Object getResourceDataForCmHandle(final String datastoreName, final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, final String requestId) { - final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(dataStoreName, cmHandleId, + final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId, resourceIdentifier, optionsParamInQuery, topicParamInQuery, @@ -129,22 +129,22 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } @Override - public Object getResourceDataForCmHandle(final String dataStoreName, + public Object getResourceDataForCmHandle(final String datastoreName, final String cmHandleId, final String resourceIdentifier, final FetchDescendantsOption fetchDescendantsOption) { - return cpsDataService.getDataNodes(dataStoreName, cmHandleId, resourceIdentifier, + return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier, fetchDescendantsOption).iterator().next(); } @Override - public Object getResourceDataForCmHandleBatch(final String dataStoreName, + public Object getResourceDataForCmHandleBatch(final String datastoreName, final List<String> cmHandleIds, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery, final String requestId) { - final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(dataStoreName, cmHandleIds, + final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleIds, resourceIdentifier, optionsParamInQuery, topicParamInQuery, |