Uplift Spring boot 3.2.4
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / NetworkCmProxyController.java
index 4da251f..45c7c33 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Pantheon.tech
- *  Modifications Copyright (C) 2021-2023 Nordix Foundation
+ *  Modifications Copyright (C) 2021-2024 Nordix Foundation
  *  Modifications Copyright (C) 2021 highstreet technologies GmbH
  *  Modifications Copyright (C) 2021-2022 Bell Canada
  *  ================================================================================
 
 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 io.micrometer.core.annotation.Timed;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
-import org.onap.cps.ncmp.api.inventory.CompositeState;
+import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig;
+import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
+import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
+import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
+import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
+import org.onap.cps.ncmp.api.models.CmResourceAddress;
 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.exceptions.InvalidDatastoreException;
+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.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.ncmp.rest.model.RestOutputCmHandleCompositeState;
 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
+import org.onap.cps.spi.model.ModuleDefinition;
 import org.onap.cps.utils.JsonObjectMapper;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -69,45 +83,56 @@ 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;
+    private final DataOperationRequestMapper dataOperationRequestMapper;
+    @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_CM_HANDLE)
+    private final Map<String, TrustLevel> trustLevelPerCmHandle;
 
     /**
      * 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 includeDescendants  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
+     * @param authorization        contents of Authorization header, or null if not present
      * @return {@code ResponseEntity} response from dmi plugin
      */
-
     @Override
+    @Timed(value = "cps.ncmp.controller.get", description = "Time taken to get resource data from datastore")
     public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
                                                              final String cmHandle,
                                                              final String resourceIdentifier,
                                                              final String optionsParamInQuery,
                                                              final String topicParamInQuery,
-                                                             final Boolean includeDescendants) {
-
-        final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
-                ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
-                        DatastoreType.fromDatastoreName(datastoreName));
+                                                             final Boolean includeDescendants,
+                                                             final String authorization) {
+        final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName);
+        final CmResourceAddress cmResourceAddress = new CmResourceAddress(datastoreName, cmHandle, resourceIdentifier);
+        return ncmpDatastoreRequestHandler.executeRequest(cmResourceAddress, optionsParamInQuery, topicParamInQuery,
+            includeDescendants, authorization);
+    }
 
-        return ncmpDatastoreRequestHandler.executeRequest(cmHandle, resourceIdentifier,
-                optionsParamInQuery, topicParamInQuery, includeDescendants);
+    @Override
+    public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
+                                                                   final DataOperationRequest dataOperationRequest,
+                                                                   final String authorization) {
+        return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery,
+                dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest), authorization);
     }
 
     /**
      * 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 includeDescendants  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
      */
 
@@ -118,86 +143,87 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
                                                                final String optionsParamInQuery,
                                                                final String topicParamInQuery,
                                                                final Boolean includeDescendants) {
-        validateDataStore(DatastoreType.OPERATIONAL, datastoreName);
-        final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
-            ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceQueryHandler();
-
-        return ncmpDatastoreRequestHandler.executeRequest(cmHandle, cpsPath, optionsParamInQuery,
-            topicParamInQuery, includeDescendants);
+        validateDataStore(OPERATIONAL, datastoreName);
+        return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants);
     }
 
     /**
      * Patch resource data from passthrough-running.
      *
-     * @param resourceIdentifier resource identifier
      * @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
+     * @param authorization      contents of Authorization header, or null if not present
      * @return {@code ResponseEntity} response from dmi plugin
      */
 
     @Override
-    public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
-                                                                      final String datastoreName,
+    public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
                                                                       final String cmHandle,
+                                                                      final String resourceIdentifier,
                                                                       final Object requestBody,
-                                                                      final String contentType) {
+                                                                      final String contentType,
+                                                                      final String authorization) {
 
-        validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
+        validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
 
         final Object responseObject = networkCmProxyDataService
                 .writeResourceDataPassThroughRunningForCmHandle(
                         cmHandle, resourceIdentifier, PATCH,
-                        jsonObjectMapper.asJsonString(requestBody), contentType);
+                        jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
         return ResponseEntity.ok(responseObject);
     }
 
     /**
      * Create resource data in datastore pass-through running for given cm-handle.
      *
-     * @param resourceIdentifier resource identifier
      * @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
+     * @param authorization      contents of Authorization header, or null if not present
      * @return {@code ResponseEntity} response from dmi plugin
      */
     @Override
-    public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
-                                                                     final String datastoreName,
+    public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
                                                                      final String cmHandle,
+                                                                     final String resourceIdentifier,
                                                                      final Object requestBody,
-                                                                     final String contentType) {
-
-        validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
+                                                                     final String contentType,
+                                                                     final String authorization) {
+        validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
 
         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
-                resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
+                resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
 
     /**
      * Update resource data in datastore pass-through running for given cm-handle.
      *
-     * @param resourceIdentifier resource identifier
      * @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 the body
+     * @param authorization      contents of Authorization header, or null if not present
      * @return response entity
      */
 
     @Override
-    public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
-                                                                       final String datastoreName,
+    public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
                                                                        final String cmHandle,
+                                                                       final String resourceIdentifier,
                                                                        final Object requestBody,
-                                                                       final String contentType) {
-        validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
+                                                                       final String contentType,
+                                                                       final String authorization) {
+        validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
 
         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
-                resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
+                resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
         return new ResponseEntity<>(HttpStatus.OK);
     }
 
@@ -208,18 +234,20 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
      * @param cmHandle           cm handle identifier
      * @param resourceIdentifier resource identifier
      * @param contentType        content type of the body
+     * @param authorization      contents of Authorization header, or null if not present
      * @return response entity no content if request is successful
      */
     @Override
     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
                                                                      final String cmHandle,
                                                                      final String resourceIdentifier,
-                                                                     final String contentType) {
+                                                                     final String contentType,
+                                                                     final String authorization) {
 
-        validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
+        validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
 
         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
-                resourceIdentifier, DELETE, NO_BODY, contentType);
+                resourceIdentifier, DELETE, NO_BODY, contentType, authorization);
         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
     }
 
@@ -306,18 +334,32 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
     }
 
     /**
-     * Return module definitions for a cm handle.
+     * Return module definitions.
      *
-     * @param cmHandleId cm-handle identifier
+     * @param cmHandleId    cm-handle identifier
+     * @param moduleName    module name
+     * @param revision      the revision of the module
      * @return list of module definitions (module name, revision, yang resource content)
      */
     @Override
-    public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
-        final List<RestModuleDefinition> restModuleDefinitions =
-                networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
-                        .map(ncmpRestInputMapper::toRestModuleDefinition)
-                        .collect(Collectors.toList());
-        return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
+    public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitions(final String cmHandleId,
+                                                                           final String moduleName,
+                                                                           final String revision) {
+        final Collection<ModuleDefinition> moduleDefinitions;
+        if (StringUtils.hasText(moduleName)) {
+            moduleDefinitions =
+                networkCmProxyDataService.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, revision);
+        } else {
+            moduleDefinitions = networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId);
+            if (StringUtils.hasText(revision)) {
+                log.warn("Ignoring revision filter as no module name is provided");
+            }
+        }
+        final List<RestModuleDefinition> response = new ArrayList<>();
+        for (final ModuleDefinition moduleDefinition: moduleDefinitions) {
+            response.add(ncmpRestInputMapper.toRestModuleDefinition(moduleDefinition));
+        }
+        return new ResponseEntity<>(response, HttpStatus.OK);
     }
 
     /**
@@ -352,11 +394,18 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
     private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
         final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
         final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
+        final TrustLevel cmHandleCurrentTrustLevel = trustLevelPerCmHandle.get(ncmpServiceCmHandle.getCmHandleId());
         restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
         cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
         restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
         restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
                 ncmpServiceCmHandle.getCompositeState()));
+        if (cmHandleCurrentTrustLevel != null) {
+            restOutputCmHandle.setTrustLevel(cmHandleCurrentTrustLevel.toString());
+        }
+        restOutputCmHandle.setModuleSetTag(ncmpServiceCmHandle.getModuleSetTag());
+        restOutputCmHandle.setAlternateId(ncmpServiceCmHandle.getAlternateId());
+        restOutputCmHandle.setDataProducerIdentifier(ncmpServiceCmHandle.getDataProducerIdentifier());
         return restOutputCmHandle;
     }
 
@@ -367,5 +416,14 @@ public class NetworkCmProxyController implements NetworkCmProxyApi {
             throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
         }
     }
+
+    private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) {
+        if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) {
+            return ncmpCachedResourceRequestHandler;
+        }
+        return ncmpPassthroughResourceRequestHandler;
+    }
+
+
 }