Support for Patch across multiple data nodes
[cps.git] / cps-ncmp-rest-stub / src / main / java / org / onap / cps / ncmp / rest / stub / controller / NetworkCmProxyStubController.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada
4  *  Modifications Copyright (c) 2022-2023 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.rest.stub.controller;
23
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;
31 import java.util.Map;
32 import java.util.UUID;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.ncmp.rest.controller.handlers.DatastoreType;
35 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
36 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
37 import org.onap.cps.ncmp.rest.stub.handlers.NetworkCmProxyApiStubDefaultImpl;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.core.io.ClassPathResource;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
44
45 @Slf4j
46 @RestController
47 @RequestMapping("${rest.api.ncmp-stub-base-path}")
48 public class NetworkCmProxyStubController implements NetworkCmProxyApiStubDefaultImpl {
49
50     @Value("${stub.path}")
51     private String pathToResponseFiles;
52
53     @Override
54     public ResponseEntity<Object> getResourceDataForCmHandle(final String dataStoreName,
55                                                              final String cmHandle,
56                                                              final String resourceIdentifier,
57                                                              final String optionsParamInQuery,
58                                                              final String topicParamInQuery,
59                                                              final Boolean includeDescendants) {
60         if (DatastoreType.PASSTHROUGH_OPERATIONAL == DatastoreType.fromDatastoreName(dataStoreName)) {
61             final ResponseEntity<Map<String, Object>> asyncResponse = populateAsyncResponse(topicParamInQuery);
62             final Map<String, Object> asyncResponseData = asyncResponse.getBody();
63             Object responseObject = null;
64             // read JSON file and map/convert to java POJO
65             final ClassPathResource resource =
66                     new ClassPathResource(pathToResponseFiles + "passthrough-operational-example.json");
67             try (InputStream inputStream = resource.getInputStream()) {
68                 final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
69                 final ObjectMapper mapper = new ObjectMapper();
70                 responseObject = mapper.readValue(string, Object.class);
71             } catch (final IOException exception) {
72                 log.error("Error reading the file.", exception);
73                 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
74             }
75             if (asyncResponseData == null) {
76                 return ResponseEntity.ok(responseObject);
77             }
78             return ResponseEntity.ok(asyncResponse);
79         }
80         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
81     }
82
83     @Override
84     public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
85             final CmHandleQueryParameters cmHandleQueryParameters) {
86         List<RestOutputCmHandle> restOutputCmHandles = null;
87         // read JSON file and map/convert to java POJO
88         final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
89         try (InputStream inputStream = resource.getInputStream()) {
90             final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
91             final ObjectMapper mapper = new ObjectMapper();
92             restOutputCmHandles = Arrays.asList(mapper.readValue(string, RestOutputCmHandle[].class));
93         } catch (final IOException exception) {
94             log.error("Error reading the file.", exception);
95             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
96         }
97         return ResponseEntity.ok(restOutputCmHandles);
98     }
99
100     private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
101         final Map<String, Object> responseData;
102         if (topicParamInQuery == null) {
103             responseData = null;
104         } else {
105             responseData = getAsyncResponseData();
106         }
107         return ResponseEntity.ok().body(responseData);
108     }
109
110     private Map<String, Object> getAsyncResponseData() {
111         final Map<String, Object> asyncResponseData = new HashMap<>(1);
112         final String resourceDataRequestId = UUID.randomUUID().toString();
113         asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId);
114         return asyncResponseData;
115     }
116 }