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=b81378dd20017e11d1d6c308caf7a7c5d0f67f16;hb=7cd5edbfd38ec8a500cbaf74618178955f7a6dd2;hp=84fcd88a963eddd7f0f1f8fbeeee80647349900f;hpb=deac4777c1a245be1dc4c423658523b41071b110;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 84fcd88a9..b81378dd2 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 @@ -1,9 +1,9 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * Modifications Copyright (C) 2021-2022 Nordix Foundation - * Modification Copyright (C) 2021 highstreet technologies GmbH - * Modifications (C) 2021-2022 Bell Canada + * Modifications Copyright (C) 2021-2023 Nordix Foundation + * Modifications Copyright (C) 2021 highstreet technologies GmbH + * Modifications Copyright (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,41 +23,39 @@ package org.onap.cps.ncmp.rest.controller; -import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE; -import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE; -import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH; -import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE; +import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.OPERATIONAL; +import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING; +import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE; +import static org.onap.cps.ncmp.api.impl.operations.OperationType.DELETE; +import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH; +import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; 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.impl.exception.InvalidDatastoreException; +import org.onap.cps.ncmp.api.impl.operations.DatastoreType; +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.model.CmHandleProperties; -import org.onap.cps.ncmp.rest.model.CmHandleProperty; +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.NcmpPassthroughResourceRequestHandler; +import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper; +import org.onap.cps.ncmp.rest.mapper.DataOperationRequestMapper; import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties; -import org.onap.cps.ncmp.rest.model.CmHandleQueryRestParameters; -import org.onap.cps.ncmp.rest.model.CmHandles; -import org.onap.cps.ncmp.rest.model.ConditionProperties; -import org.onap.cps.ncmp.rest.model.Conditions; -import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject; -import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray; +import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters; +import org.onap.cps.ncmp.rest.model.DataOperationRequest; +import org.onap.cps.ncmp.rest.model.RestModuleDefinition; import org.onap.cps.ncmp.rest.model.RestModuleReference; import org.onap.cps.ncmp.rest.model.RestOutputCmHandle; -import org.onap.cps.utils.CpsValidator; +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.JsonObjectMapper; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -71,93 +69,118 @@ 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; - public static final String ASYNC_REQUEST_ID = "requestId"; - private final NetworkCmProxyDataService networkCmProxyDataService; private final JsonObjectMapper jsonObjectMapper; + private final DeprecationHelper deprecationHelper; private final NcmpRestInputMapper ncmpRestInputMapper; + private final CmHandleStateMapper cmHandleStateMapper; + private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler; + private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler; + private final DataOperationRequestMapper dataOperationRequestMapper; /** - * Get resource data from operational datastore. + * Get resource data from datastore. * - * @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 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 */ + @Override - public ResponseEntity getResourceDataOperationalForCmHandle(final String cmHandle, - final @NotNull @Valid String resourceIdentifier, - final @Valid String optionsParamInQuery, - final @Valid String topicParamInQuery) { - final ResponseEntity> asyncResponse = populateAsyncResponse(topicParamInQuery); - final Map asyncResponseData = asyncResponse.getBody(); - - final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle, - resourceIdentifier, - optionsParamInQuery, - asyncResponseData == null ? NO_TOPIC : topicParamInQuery, - asyncResponseData == null ? NO_REQUEST_ID : asyncResponseData.get(ASYNC_REQUEST_ID).toString()); - - if (asyncResponseData == null) { - return ResponseEntity.ok(responseObject); - } - return ResponseEntity.ok(asyncResponse); + public ResponseEntity getResourceDataForCmHandle(final String datastoreName, + final String cmHandle, + final String resourceIdentifier, + final String optionsParamInQuery, + final String topicParamInQuery, + final Boolean includeDescendants) { + + + final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName); + return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandle, resourceIdentifier, + optionsParamInQuery, topicParamInQuery, includeDescendants); + } + + @Override + public ResponseEntity executeDataOperationForCmHandles(final String topicParamInQuery, + final DataOperationRequest + dataOperationRequest) { + return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery, + dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest)); } /** - * Get resource data from pass-through running datastore. + * Query resource data from datastore. * - * @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 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 */ + @Override - public ResponseEntity getResourceDataRunningForCmHandle(final String cmHandle, - final @NotNull @Valid String resourceIdentifier, - final @Valid String optionsParamInQuery, - final @Valid String topicParamInQuery) { - final ResponseEntity> asyncResponse = populateAsyncResponse(topicParamInQuery); - final Map asyncResponseData = asyncResponse.getBody(); - - final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, - optionsParamInQuery, - asyncResponseData == null ? NO_TOPIC : topicParamInQuery, - asyncResponseData == null ? NO_REQUEST_ID : asyncResponseData.get(ASYNC_REQUEST_ID).toString()); - - if (asyncResponseData == null) { - return ResponseEntity.ok(responseObject); - } - return ResponseEntity.ok(asyncResponse); + public ResponseEntity queryResourceDataForCmHandle(final String datastoreName, + final String cmHandle, + final String cpsPath, + final String optionsParamInQuery, + final String topicParamInQuery, + final Boolean includeDescendants) { + validateDataStore(OPERATIONAL, datastoreName); + return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants); } + /** + * Patch resource data from passthrough-running. + * + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier + * @param resourceIdentifier resource identifier + * @param requestBody the request body + * @param contentType content type of body + * @return {@code ResponseEntity} response from dmi plugin + */ + @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); + public ResponseEntity patchResourceDataRunningForCmHandle(final String datastoreName, + final String cmHandle, + final String resourceIdentifier, + final Object requestBody, + final String contentType) { + + validateDataStore(PASSTHROUGH_RUNNING, datastoreName); + + final Object responseObject = networkCmProxyDataService + .writeResourceDataPassThroughRunningForCmHandle( + cmHandle, resourceIdentifier, PATCH, + jsonObjectMapper.asJsonString(requestBody), contentType); return ResponseEntity.ok(responseObject); } /** * Create resource data in datastore pass-through running for given cm-handle. * + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier * @param resourceIdentifier resource identifier - * @param cmHandle cm handle identifier - * @param requestBody the request body - * @param contentType content type of body + * @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) { + public ResponseEntity createResourceDataRunningForCmHandle(final String datastoreName, + final String cmHandle, + final String resourceIdentifier, + final Object requestBody, + final String contentType) { + + validateDataStore(PASSTHROUGH_RUNNING, datastoreName); + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType); return new ResponseEntity<>(HttpStatus.CREATED); @@ -166,70 +189,87 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { /** * Update resource data in datastore pass-through running for given cm-handle. * + * @param datastoreName name of the datastore + * @param cmHandle cm handle identifier * @param resourceIdentifier resource identifier - * @param cmHandle cm handle identifier - * @param requestBody the request body - * @param contentType content type of the body + * @param requestBody the request body + * @param contentType content type of the body * @return response entity */ + @Override - public ResponseEntity updateResourceDataRunningForCmHandle(final String resourceIdentifier, + public ResponseEntity updateResourceDataRunningForCmHandle(final String datastoreName, final String cmHandle, + final String resourceIdentifier, final Object requestBody, final String contentType) { + validateDataStore(PASSTHROUGH_RUNNING, 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) { + + validateDataStore(PASSTHROUGH_RUNNING, datastoreName); + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, - resourceIdentifier, DELETE, NO_BODY, contentType); + resourceIdentifier, DELETE, NO_BODY, contentType); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } /** - * Execute cm handle search. + * Query and return cm handles that match the given query parameters. * - * @param conditions the conditions - * @return cm handles returned from search. + * @param cmHandleQueryParameters the cm handle query parameters + * @return collection of cm handles */ @Override - public ResponseEntity executeCmHandleSearch(final Conditions conditions) { - final List conditionProperties = - conditions.getConditions().stream().collect(Collectors.toList()); - final CmHandles cmHandles = new CmHandles(); - cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties))); - return ResponseEntity.ok(cmHandles); + @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12 + public ResponseEntity> searchCmHandles( + final CmHandleQueryParameters cmHandleQueryParameters) { + final CmHandleQueryApiParameters cmHandleQueryApiParameters = + deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters); + final Collection cmHandles = networkCmProxyDataService + .executeCmHandleSearch(cmHandleQueryApiParameters); + final List outputCmHandles = + cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList()); + return ResponseEntity.ok(outputCmHandles); } /** - * Query and return cm handles that match the given query parameters. + * Query and return cm handle ids that match the given query parameters. * - * @param cmHandleQueryRestParameters the cm handle query parameters + * @param cmHandleQueryParameters the cm handle query parameters * @return collection of cm handle ids */ - public ResponseEntity> queryCmHandles( - final CmHandleQueryRestParameters cmHandleQueryRestParameters) { - final Set cmHandleIds = networkCmProxyDataService.queryCmHandles( - jsonObjectMapper.convertToValueType(cmHandleQueryRestParameters, CmHandleQueryApiParameters.class)); + @Override + public ResponseEntity> searchCmHandleIds( + final CmHandleQueryParameters cmHandleQueryParameters) { + final CmHandleQueryApiParameters cmHandleQueryApiParameters = + jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class); + final Collection cmHandleIds + = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters); return ResponseEntity.ok(List.copyOf(cmHandleIds)); } /** * Search for Cm Handle and Properties by Name. + * * @param cmHandleId cm-handle identifier * @return cm handle and its properties */ @@ -240,6 +280,55 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { return ResponseEntity.ok(restOutputCmHandle); } + /** + * 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 CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties(); + cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId)); + final RestOutputCmHandlePublicProperties 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 CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId); + final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState = + new RestOutputCmHandleCompositeState(); + restOutputCmHandleCompositeState.setState( + cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState)); + return ResponseEntity.ok(restOutputCmHandleCompositeState); + } + + /** + * Return module definitions for a cm handle. + * + * @param cmHandleId cm-handle identifier + * @return list of module definitions (module name, revision, yang resource content) + */ + @Override + public ResponseEntity> getModuleDefinitionsByCmHandleId(final String cmHandleId) { + final List restModuleDefinitions = + networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream() + .map(ncmpRestInputMapper::toRestModuleDefinition) + .collect(Collectors.toList()); + return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK); + } + /** * Return module references for a cm handle. * @@ -248,82 +337,53 @@ 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); } - private Collection processConditions(final List conditionProperties) { - for (final ConditionProperties conditionProperty : conditionProperties) { - if (conditionProperty.getName().equals("hasAllModules")) { - return executeCmHandleSearchesForModuleNames(conditionProperty); - } else { - log.warn("Unrecognized condition name {}.", conditionProperty.getName()); - } - } - log.warn("No valid conditions found {}.", conditionProperties); - return Collections.emptyList(); - } - - private Collection executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) { - return networkCmProxyDataService - .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters())); - } - - private Collection getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) { - final Collection moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size()); - for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) { - moduleNames.add(moduleNameAsJsonObject.getModuleName()); - } - return moduleNames; + /** + * 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 CmHandleProperties toCmHandleProperties(final Collection cmHandleIdentifiers) { - final CmHandleProperties cmHandleProperties = new CmHandleProperties(); - for (final String cmHandleIdentifier : cmHandleIdentifiers) { - final CmHandleProperty cmHandleProperty = new CmHandleProperty(); - cmHandleProperty.setCmHandleId(cmHandleIdentifier); - cmHandleProperties.add(cmHandleProperty); - } - return cmHandleProperties; - } private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) { final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle(); final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties(); - restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleID()); + restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId()); cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties()); restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties); + restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason( + ncmpServiceCmHandle.getCompositeState())); return restOutputCmHandle; } - private ResponseEntity> populateAsyncResponse(final String topicParamInQuery) { - final boolean processAsynchronously = hasTopicParameter(topicParamInQuery); - final Map responseData; - if (processAsynchronously) { - responseData = getAsyncResponseData(); - } else { - responseData = null; + private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) { + final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName); + + if (acceptableDataStoreType != datastoreType) { + throw new InvalidDatastoreException(requestedDatastoreName + " is not supported"); } - return ResponseEntity.ok().body(responseData); } - private static boolean hasTopicParameter(final String topicName) { - if (topicName == null) { - return false; - } - if (CpsValidator.validateTopicName(topicName)) { - return true; + private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) { + if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) { + return ncmpCachedResourceRequestHandler; } - throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); + return ncmpPassthroughResourceRequestHandler; } - private Map getAsyncResponseData() { - final Map asyncResponseData = new HashMap<>(1); - final String resourceDataRequestId = UUID.randomUUID().toString(); - asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId); - return asyncResponseData; - } } +