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 dataStoreType,
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(dataStoreType)) {
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 resourceIdentifier,
91 final String cmHandleId,
93 final String contentType) {
94 return new ResponseEntity<>(HttpStatus.CREATED);
98 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String cmHandleId,
99 final String resourceIdentifier,
100 final String contentType) {
101 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
105 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
106 final CmHandleQueryParameters cmHandleQueryParameters) {
107 List<RestOutputCmHandle> restOutputCmHandles = null;
108 // read JSON file and map/convert to java POJO
109 final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
110 try (InputStream inputStream = resource.getInputStream()) {
111 final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
112 final ObjectMapper mapper = new ObjectMapper();
113 restOutputCmHandles = Arrays.asList(mapper.readValue(string, RestOutputCmHandle[].class));
114 } catch (final IOException exception) {
115 log.error("Error reading the file.", exception);
116 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
118 return ResponseEntity.ok(restOutputCmHandles);
122 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
123 final Boolean dataSyncEnabled) {
124 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
128 public ResponseEntity<List<String>> searchCmHandleIds(
129 final CmHandleQueryParameters cmHandleQueryParameters) {
130 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
134 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
135 final String cmHandleId) {
136 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
140 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(final String cmHandle) {
141 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
145 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandle) {
146 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
150 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandleId) {
151 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
155 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
156 final String cmHandleId,
158 final String contentType) {
159 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
163 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
164 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
168 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
169 final String cmHandleId,
171 final String contentType) {
172 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
175 private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
176 final Map<String, Object> responseData;
177 if (topicParamInQuery != null) {
178 responseData = getAsyncResponseData();
182 return ResponseEntity.ok().body(responseData);
185 private Map<String, Object> getAsyncResponseData() {
186 final Map<String, Object> asyncResponseData = new HashMap<>(1);
187 final String resourceDataRequestId = UUID.randomUUID().toString();
188 asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId);
189 return asyncResponseData;