2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.rest.stub.controller;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.UUID;
32 import javax.validation.Valid;
33 import javax.validation.constraints.NotNull;
34 import lombok.extern.slf4j.Slf4j;
35 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
36 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
37 import org.onap.cps.ncmp.rest.model.RestModuleReference;
38 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
39 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.core.io.ClassPathResource;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RestController;
49 @RequestMapping("${rest.api.ncmp-stub-base-path}")
50 public class NetworkCmProxyStubController implements NetworkCmProxyApi {
52 public static final String ASYNC_REQUEST_ID = "requestId";
54 @Value("${stub.path}")
55 private String pathToResponseFiles;
58 public ResponseEntity<Void> createResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier,
59 final String cmHandleId, @Valid final Object body, final String contentType) {
60 return new ResponseEntity<>(HttpStatus.CREATED);
64 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String cmHandleId,
65 @NotNull @Valid final String resourceIdentifier, final String contentType) {
66 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
70 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
71 final CmHandleQueryParameters cmHandleQueryParameters) {
72 List<RestOutputCmHandle> restOutputCmHandles = null;
73 // read JSON file and map/convert to java POJO
74 final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
75 try (InputStream inputStream = resource.getInputStream()) {
76 final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
77 final ObjectMapper mapper = new ObjectMapper();
78 restOutputCmHandles = Arrays.asList(mapper.readValue(string, RestOutputCmHandle[].class));
79 } catch (final IOException exception) {
80 log.error("Error reading the file.", exception);
81 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
83 return ResponseEntity.ok(restOutputCmHandles);
87 public ResponseEntity<List<String>> searchCmHandleIds(
88 final CmHandleQueryParameters cmHandleQueryParameters) {
89 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
93 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
94 final String cmHandleId) {
95 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
99 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandleId) {
100 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
104 * Get resource data from operational datastore.
106 * @param cmHandleId cm handle identifier
107 * @param resourceIdentifier resource identifier
108 * @param optionsParamInQuery options query parameter
109 * @param topicParamInQuery topic query parameter
110 * @return {@code ResponseEntity} response from dmi plugin
113 public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandleId,
114 final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery) {
115 final ResponseEntity<Map<String, Object>> asyncResponse = populateAsyncResponse(topicParamInQuery);
116 final Map<String, Object> asyncResponseData = asyncResponse.getBody();
117 Object responseObject = null;
118 // read JSON file and map/convert to java POJO
119 final ClassPathResource resource = new ClassPathResource(pathToResponseFiles
120 + "passthrough-operational-example.json");
121 try (InputStream inputStream = resource.getInputStream()) {
122 final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
123 final ObjectMapper mapper = new ObjectMapper();
124 responseObject = mapper.readValue(string, Object.class);
125 } catch (final IOException exception) {
126 log.error("Error reading the file.", exception);
127 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
129 if (asyncResponseData == null) {
130 return ResponseEntity.ok(responseObject);
132 return ResponseEntity.ok(asyncResponse);
137 public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandleId,
138 final String resourceIdentifier, final String options, final String topic) {
139 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
143 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
144 final String cmHandleId, final Object body, final String contentType) {
145 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
149 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
150 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
154 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier,
155 final String cmHandleId, @Valid final Object body, final String contentType) {
156 return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
159 private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
160 final Map<String, Object> responseData;
161 if (topicParamInQuery != null) {
162 responseData = getAsyncResponseData();
166 return ResponseEntity.ok().body(responseData);
169 private Map<String, Object> getAsyncResponseData() {
170 final Map<String, Object> asyncResponseData = new HashMap<>(1);
171 final String resourceDataRequestId = UUID.randomUUID().toString();
172 asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId);
173 return asyncResponseData;