Add moduleSetTag to the request towards dmi plugin
[cps.git] / dmi-plugin-demo-and-csit-stub / dmi-plugin-demo-and-csit-stub-service / src / main / java / org / onap / cps / ncmp / dmi / rest / stub / controller / DmiRestStubController.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 Nordix Foundation
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.dmi.rest.stub.controller;
22
23 import static org.onap.cps.ncmp.api.NcmpResponseStatus.SUCCESS;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import io.cloudevents.CloudEvent;
28 import io.cloudevents.core.builder.CloudEventBuilder;
29 import java.net.URI;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.json.simple.parser.JSONParser;
38 import org.json.simple.parser.ParseException;
39 import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter;
40 import org.onap.cps.ncmp.dmi.rest.stub.model.data.operational.CmHandle;
41 import org.onap.cps.ncmp.dmi.rest.stub.model.data.operational.DataOperationRequest;
42 import org.onap.cps.ncmp.dmi.rest.stub.model.data.operational.DmiDataOperationRequest;
43 import org.onap.cps.ncmp.dmi.rest.stub.utils.ResourceFileReaderUtil;
44 import org.onap.cps.ncmp.events.async1_0_0.Data;
45 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent;
46 import org.onap.cps.ncmp.events.async1_0_0.Response;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.context.ApplicationContext;
49 import org.springframework.core.io.Resource;
50 import org.springframework.core.io.ResourceLoader;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.kafka.core.KafkaTemplate;
54 import org.springframework.web.bind.annotation.DeleteMapping;
55 import org.springframework.web.bind.annotation.GetMapping;
56 import org.springframework.web.bind.annotation.PathVariable;
57 import org.springframework.web.bind.annotation.PostMapping;
58 import org.springframework.web.bind.annotation.PutMapping;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestHeader;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestParam;
63 import org.springframework.web.bind.annotation.RestController;
64
65 @RestController
66 @RequestMapping("${rest.api.dmi-stub-base-path}")
67 @RequiredArgsConstructor
68 @Slf4j
69 public class DmiRestStubController {
70
71     private static final String DEFAULT_TAG = "tagD";
72     private static final String dataOperationEventType = "org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent";
73     private static final Map<String, String> moduleSetTagPerCmHandleId = new HashMap<>();
74     private final KafkaTemplate<String, CloudEvent> cloudEventKafkaTemplate;
75     private final ObjectMapper objectMapper;
76     private final ApplicationContext applicationContext;
77     @Value("${app.ncmp.async-m2m.topic}")
78     private String ncmpAsyncM2mTopic;
79     @Value("${delay.module-references-delay-ms}")
80     private long moduleReferencesDelayMs;
81     @Value("${delay.module-resources-delay-ms}")
82     private long moduleResourcesDelayMs;
83     @Value("${delay.data-for-cm-handle-delay-ms}")
84     private long dataForCmHandleDelayMs;
85
86     /**
87      * This code defines a REST API endpoint for adding new the module set tag mapping. The endpoint receives the
88      * cmHandleId and moduleSetTag as request body and add into moduleSetTagPerCmHandleId map with the provided
89      * values.
90      *
91      * @param requestBody map of cmHandleId and moduleSetTag
92      * @return a ResponseEntity object containing the updated moduleSetTagPerCmHandleId map as the response body
93      */
94     @PostMapping("/v1/tagMapping")
95     public ResponseEntity<Map<String, String>> addTagForMapping(@RequestBody final Map<String, String> requestBody) {
96         moduleSetTagPerCmHandleId.putAll(requestBody);
97         return new ResponseEntity<>(requestBody, HttpStatus.CREATED);
98     }
99
100     /**
101      * This code defines a GET endpoint of  module set tag mapping.
102      *
103      * @return The map represents the module set tag mapping.
104      */
105     @GetMapping("/v1/tagMapping")
106     public ResponseEntity<Map<String, String>> getTagMapping() {
107         return ResponseEntity.ok(moduleSetTagPerCmHandleId);
108     }
109
110     /**
111      * This code defines a GET endpoint of  module set tag by cm handle ID.
112      *
113      * @return The map represents the module set tag mapping filtered by cm handle ID.
114      */
115     @GetMapping("/v1/tagMapping/ch/{cmHandleId}")
116     public ResponseEntity<String> getTagMappingByCmHandleId(@PathVariable final String cmHandleId) {
117         return ResponseEntity.ok(moduleSetTagPerCmHandleId.get(cmHandleId));
118     }
119
120     /**
121      * This code defines a REST API endpoint for updating the module set tag mapping. The endpoint receives the
122      * cmHandleId and moduleSetTag as request body and updates the moduleSetTagPerCmHandleId map with the provided
123      * values.
124      *
125      * @param requestBody map of cmHandleId and moduleSetTag
126      * @return a ResponseEntity object containing the updated moduleSetTagPerCmHandleId map as the response body
127      */
128
129     @PutMapping("/v1/tagMapping")
130     public ResponseEntity<Map<String, String>> updateTagMapping(@RequestBody final Map<String, String> requestBody) {
131         moduleSetTagPerCmHandleId.putAll(requestBody);
132         return ResponseEntity.noContent().build();
133     }
134
135     /**
136      * It contains a method to delete an entry from the moduleSetTagPerCmHandleId map.
137      * The method takes a cmHandleId as a parameter and removes the corresponding entry from the map.
138      *
139      * @return a ResponseEntity containing the updated map.
140      */
141     @DeleteMapping("/v1/tagMapping/ch/{cmHandleId}")
142     public ResponseEntity<String> deleteTagMappingByCmHandleId(@PathVariable final String cmHandleId) {
143         moduleSetTagPerCmHandleId.remove(cmHandleId);
144         return ResponseEntity.ok(String.format("Mapping of %s is deleted successfully", cmHandleId));
145     }
146
147     /**
148      * Get all modules for given cm handle.
149      *
150      * @param cmHandleId              The identifier for a network function, network element, subnetwork,
151      *                                or any other cm object by managed Network CM Proxy
152      * @param moduleReferencesRequest module references request body
153      * @return ResponseEntity response entity having module response as json string.
154      */
155     @PostMapping("/v1/ch/{cmHandleId}/modules")
156     public ResponseEntity<String> getModuleReferences(@PathVariable("cmHandleId") final String cmHandleId,
157                                                       @RequestBody final Object moduleReferencesRequest) {
158         delay(moduleReferencesDelayMs);
159         try {
160             log.info("Incoming DMI request body: {}",
161                     objectMapper.writeValueAsString(moduleReferencesRequest));
162         } catch (final JsonProcessingException jsonProcessingException) {
163             log.info("Unable to parse dmi data operation request to json string");
164         }
165         final String moduleResponseContent = getModuleResourceResponse(cmHandleId,
166                 "ModuleResponse.json");
167         log.info("cm handle: {} requested for modules", cmHandleId);
168         return ResponseEntity.ok(moduleResponseContent);
169     }
170
171     /**
172      * Retrieves module resources for a given cmHandleId.
173      *
174      * @param cmHandleId                 The identifier for a network function, network element, subnetwork,
175      *                                   or any other cm object by managed Network CM Proxy
176      * @param moduleResourcesReadRequest module resources read request body
177      * @return ResponseEntity response entity having module resources response as json string.
178      */
179     @PostMapping("/v1/ch/{cmHandleId}/moduleResources")
180     public ResponseEntity<String> retrieveModuleResources(
181             @PathVariable("cmHandleId") final String cmHandleId,
182             @RequestBody final Object moduleResourcesReadRequest) {
183         delay(moduleResourcesDelayMs);
184         final String moduleResourcesResponseContent = getModuleResourceResponse(cmHandleId,
185                 "ModuleResourcesResponse.json");
186         log.info("cm handle: {} requested for modules resources", cmHandleId);
187         return ResponseEntity.ok(moduleResourcesResponseContent);
188     }
189
190     /**
191      * Create resource data from passthrough operational or running for a cm handle.
192      *
193      * @param cmHandleId              The identifier for a network function, network element, subnetwork,
194      *                                or any other cm object by managed Network CM Proxy
195      * @param datastoreName           datastore name
196      * @param resourceIdentifier      resource identifier
197      * @param options                 options
198      * @param topic                   client given topic name
199      * @return (@ code ResponseEntity) response entity
200      */
201     @PostMapping("/v1/ch/{cmHandleId}/data/ds/{datastoreName}")
202     public ResponseEntity<String> getResourceDataForCmHandle(
203             @PathVariable("cmHandleId") final String cmHandleId,
204             @PathVariable("datastoreName") final String datastoreName,
205             @RequestParam(value = "resourceIdentifier") final String resourceIdentifier,
206             @RequestParam(value = "options", required = false) final String options,
207             @RequestParam(value = "topic", required = false) final String topic,
208             @RequestHeader(value = "Authorization", required = false) final String authorization) {
209         log.info("DMI AUTH HEADER: {}", authorization);
210         delay(dataForCmHandleDelayMs);
211         final String sampleJson = ResourceFileReaderUtil.getResourceFileContent(applicationContext.getResource(
212                 ResourceLoader.CLASSPATH_URL_PREFIX + "data/operational/ietf-network-topology-sample-rfc8345.json"));
213         return ResponseEntity.ok(sampleJson);
214     }
215
216     /**
217      * This method is not implemented for ONAP DMI plugin.
218      *
219      * @param topic                   client given topic name
220      * @param requestId               requestId generated by NCMP as an ack for client
221      * @param dmiDataOperationRequest list of operation details
222      * @return (@ code ResponseEntity) response entity
223      */
224     @PostMapping("/v1/data")
225     public ResponseEntity<Void> getResourceDataForCmHandleDataOperation(
226             @RequestParam(value = "topic") final String topic,
227             @RequestParam(value = "requestId") final String requestId,
228             @RequestBody final DmiDataOperationRequest dmiDataOperationRequest) {
229         delay(dataForCmHandleDelayMs);
230         try {
231             log.info("Request received from the NCMP to DMI Plugin: {}",
232                     objectMapper.writeValueAsString(dmiDataOperationRequest));
233         } catch (final JsonProcessingException jsonProcessingException) {
234             log.info("Unable to process dmi data operation request to json string");
235         }
236         dmiDataOperationRequest.getOperations().forEach(dmiDataOperation -> {
237             final DataOperationEvent dataOperationEvent = getDataOperationEvent(dmiDataOperation);
238             dmiDataOperation.getCmHandles().forEach(cmHandle -> {
239                 dataOperationEvent.getData().getResponses().get(0).setIds(List.of(cmHandle.getId()));
240                 final CloudEvent cloudEvent = buildAndGetCloudEvent(topic, requestId, dataOperationEvent);
241                 cloudEventKafkaTemplate.send(ncmpAsyncM2mTopic, UUID.randomUUID().toString(), cloudEvent);
242             });
243         });
244         return new ResponseEntity<>(HttpStatus.ACCEPTED);
245     }
246
247     private CloudEvent buildAndGetCloudEvent(final String topic, final String requestId,
248                                              final DataOperationEvent dataOperationEvent) {
249         CloudEvent cloudEvent = null;
250         try {
251             cloudEvent = CloudEventBuilder.v1()
252                     .withId(UUID.randomUUID().toString())
253                     .withSource(URI.create("DMI"))
254                     .withType(dataOperationEventType)
255                     .withDataSchema(URI.create("urn:cps:" + dataOperationEventType + ":1.0.0"))
256                     .withTime(EventDateTimeFormatter.toIsoOffsetDateTime(
257                             EventDateTimeFormatter.getCurrentIsoFormattedDateTime()))
258                     .withData(objectMapper.writeValueAsBytes(dataOperationEvent))
259                     .withExtension("destination", topic)
260                     .withExtension("correlationid", requestId)
261                     .build();
262         } catch (final JsonProcessingException jsonProcessingException) {
263             log.error("Unable to parse event into bytes. cause : {}", jsonProcessingException.getMessage());
264         }
265         return cloudEvent;
266     }
267
268     private DataOperationEvent getDataOperationEvent(final DataOperationRequest dataOperationRequest) {
269         final Response response = new Response();
270         response.setOperationId(dataOperationRequest.getOperationId());
271         response.setStatusCode(SUCCESS.getCode());
272         response.setStatusMessage(SUCCESS.getMessage());
273         response.setIds(dataOperationRequest.getCmHandles().stream().map(CmHandle::getId).toList());
274         response.setResourceIdentifier(dataOperationRequest.getResourceIdentifier());
275         response.setOptions(dataOperationRequest.getOptions());
276         final String ietfNetworkTopologySample = ResourceFileReaderUtil
277                 .getResourceFileContent(applicationContext.getResource(
278                         ResourceLoader.CLASSPATH_URL_PREFIX
279                                 + "data/operational/ietf-network-topology-sample-rfc8345.json"));
280         final JSONParser jsonParser = new JSONParser();
281         try {
282             response.setResult(jsonParser.parse(ietfNetworkTopologySample));
283         } catch (final ParseException parseException) {
284             log.error("Unable to parse event result as json object. cause : {}", parseException.getMessage());
285         }
286         final List<Response> responseList = new ArrayList<>(1);
287         responseList.add(response);
288         final Data data = new Data();
289         data.setResponses(responseList);
290         final DataOperationEvent dataOperationEvent = new DataOperationEvent();
291         dataOperationEvent.setData(data);
292         return dataOperationEvent;
293     }
294
295     private String getModuleResourceResponse(final String cmHandleId, final String moduleResponseType) {
296         if (moduleSetTagPerCmHandleId.isEmpty()) {
297             log.info("Using default module responses of type ietfYang");
298             return ResourceFileReaderUtil.getResourceFileContent(applicationContext.getResource(
299                     ResourceLoader.CLASSPATH_URL_PREFIX
300                             + String.format("module/ietfYang-%s", moduleResponseType)));
301         }
302         final String moduleSetTag = moduleSetTagPerCmHandleId.getOrDefault(cmHandleId, DEFAULT_TAG);
303         final String moduleResponseFilePath = String.format("module/%s-%s", moduleSetTag, moduleResponseType);
304         final Resource moduleResponseResource = applicationContext.getResource(
305                 ResourceLoader.CLASSPATH_URL_PREFIX + moduleResponseFilePath);
306         log.info("Using module responses from : {}", moduleResponseFilePath);
307         return ResourceFileReaderUtil.getResourceFileContent(moduleResponseResource);
308     }
309
310     private void delay(final long milliseconds) {
311         try {
312             Thread.sleep(milliseconds);
313         } catch (final InterruptedException e) {
314             log.error("Thread sleep interrupted: {}", e.getMessage());
315             Thread.currentThread().interrupt();
316         }
317     }
318 }