2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2022 Bell Canada
 
   4  *  Modifications Copyright (c) 2022 Nordix Foundation
 
   5  *  ================================================================================
 
   6  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   7  *  you may not use this file except in compliance with the License.
 
   8  *  You may obtain a copy of the License at
 
  10  *        http://www.apache.org/licenses/LICENSE-2.0
 
  12  *  Unless required by applicable law or agreed to in writing, software
 
  13  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  *  See the License for the specific language governing permissions and
 
  16  *  limitations under the License.
 
  18  *  SPDX-License-Identifier: Apache-2.0
 
  19  *  ============LICENSE_END=========================================================
 
  22 package org.onap.cps.ncmp.rest.stub.controller;
 
  24 import com.fasterxml.jackson.databind.ObjectMapper;
 
  25 import java.io.IOException;
 
  26 import java.io.InputStream;
 
  27 import java.nio.charset.StandardCharsets;
 
  28 import java.util.Arrays;
 
  29 import java.util.HashMap;
 
  30 import java.util.List;
 
  32 import java.util.UUID;
 
  33 import lombok.extern.slf4j.Slf4j;
 
  34 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
 
  35 import org.onap.cps.ncmp.rest.controller.handlers.DatastoreType;
 
  36 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
 
  37 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
 
  38 import org.onap.cps.ncmp.rest.model.RestModuleReference;
 
  39 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
 
  40 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
 
  41 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
 
  42 import org.springframework.beans.factory.annotation.Value;
 
  43 import org.springframework.core.io.ClassPathResource;
 
  44 import org.springframework.http.HttpStatus;
 
  45 import org.springframework.http.ResponseEntity;
 
  46 import org.springframework.web.bind.annotation.RequestMapping;
 
  47 import org.springframework.web.bind.annotation.RestController;
 
  51 @RequestMapping("${rest.api.ncmp-stub-base-path}")
 
  52 public class NetworkCmProxyStubController implements NetworkCmProxyApi {
 
  54     public static final String ASYNC_REQUEST_ID = "requestId";
 
  56     @Value("${stub.path}")
 
  57     private String pathToResponseFiles;
 
  60     public ResponseEntity<Object> getResourceDataForCmHandle(final String dataStoreName,
 
  61                                                              final String cmHandle,
 
  62                                                              final String resourceIdentifier,
 
  63                                                              final String optionsParamInQuery,
 
  64                                                              final String topicParamInQuery,
 
  65                                                              final Boolean includeDescendants) {
 
  66         if (DatastoreType.PASSTHROUGH_OPERATIONAL == DatastoreType.fromDatastoreName(dataStoreName)) {
 
  67             final ResponseEntity<Map<String, Object>> asyncResponse = populateAsyncResponse(topicParamInQuery);
 
  68             final Map<String, Object> asyncResponseData = asyncResponse.getBody();
 
  69             Object responseObject = null;
 
  70             // read JSON file and map/convert to java POJO
 
  71             final ClassPathResource resource =
 
  72                     new ClassPathResource(pathToResponseFiles + "passthrough-operational-example.json");
 
  73             try (InputStream inputStream = resource.getInputStream()) {
 
  74                 final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
 
  75                 final ObjectMapper mapper = new ObjectMapper();
 
  76                 responseObject = mapper.readValue(string, Object.class);
 
  77             } catch (final IOException exception) {
 
  78                 log.error("Error reading the file.", exception);
 
  79                 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
 
  81             if (asyncResponseData == null) {
 
  82                 return ResponseEntity.ok(responseObject);
 
  84             return ResponseEntity.ok(asyncResponse);
 
  86         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
  90     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
 
  91                                                                      final String resourceIdentifier,
 
  92                                                                      final String cmHandleId,
 
  94                                                                      final String contentType) {
 
  95         return new ResponseEntity<>(HttpStatus.CREATED);
 
  99     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
 
 100                                                                      final String cmHandleId,
 
 101                                                                      final String resourceIdentifier,
 
 102                                                                      final String contentType) {
 
 103         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
 
 107     public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
 
 108             final CmHandleQueryParameters cmHandleQueryParameters) {
 
 109         List<RestOutputCmHandle> restOutputCmHandles = null;
 
 110         // read JSON file and map/convert to java POJO
 
 111         final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
 
 112         try (InputStream inputStream = resource.getInputStream()) {
 
 113             final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
 
 114             final ObjectMapper mapper = new ObjectMapper();
 
 115             restOutputCmHandles = Arrays.asList(mapper.readValue(string, RestOutputCmHandle[].class));
 
 116         } catch (final IOException exception) {
 
 117             log.error("Error reading the file.", exception);
 
 118             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
 
 120         return ResponseEntity.ok(restOutputCmHandles);
 
 124     public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
 
 125                                                                     final Boolean dataSyncEnabled) {
 
 126         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 130     public ResponseEntity<List<String>> searchCmHandleIds(
 
 131             final CmHandleQueryParameters cmHandleQueryParameters) {
 
 132         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 136     public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
 
 137             final String cmHandleId) {
 
 138         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 142     public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(final String cmHandle) {
 
 143         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 147     public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandle) {
 
 148         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 152     public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandleId) {
 
 153         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 157     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
 
 158                                                                       final String resourceIdentifier,
 
 159                                                                       final String cmHandleId,
 
 161                                                                       final String contentType) {
 
 162         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 166     public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
 
 167         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 171     public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
 
 172                                                                        final String resourceIdentifier,
 
 173                                                                        final String cmHandleId,
 
 175                                                                        final String contentType) {
 
 176         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
 
 179     private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
 
 180         final Map<String, Object> responseData;
 
 181         if (topicParamInQuery != null) {
 
 182             responseData = getAsyncResponseData();
 
 186         return ResponseEntity.ok().body(responseData);
 
 189     private Map<String, Object> getAsyncResponseData() {
 
 190         final Map<String, Object> asyncResponseData = new HashMap<>(1);
 
 191         final String resourceDataRequestId = UUID.randomUUID().toString();
 
 192         asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId);
 
 193         return asyncResponseData;