Merge "Client to NCMP Subscription schema change"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / data / operation / DataOperationEventCreator.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.api.impl.utils.data.operation;
22
23 import io.cloudevents.CloudEvent;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import lombok.AccessLevel;
29 import lombok.NoArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.ncmp.api.NcmpEventResponseCode;
32 import org.onap.cps.ncmp.api.impl.events.NcmpCloudEventBuilder;
33 import org.onap.cps.ncmp.events.async1_0_0.Data;
34 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent;
35 import org.onap.cps.ncmp.events.async1_0_0.Response;
36 import org.springframework.util.MultiValueMap;
37
38 @Slf4j
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 public class DataOperationEventCreator {
41
42     /**
43      * Creates data operation event.
44      *
45      * @param clientTopic                              topic the client wants to use for responses
46      * @param requestId                                unique identifier per request
47      * @param cmHandleIdsPerResponseCodesPerOperationId map of cm handles per operation response per response code
48      * @return Cloud Event
49      */
50     public static CloudEvent createDataOperationEvent(final String clientTopic,
51                                                       final String requestId,
52                                                       final MultiValueMap<String,
53                                                               Map<NcmpEventResponseCode, List<String>>>
54                                                               cmHandleIdsPerResponseCodesPerOperationId) {
55         final DataOperationEvent dataOperationEvent = new DataOperationEvent();
56         final Data data = createPayloadFromDataOperationResponses(cmHandleIdsPerResponseCodesPerOperationId);
57         dataOperationEvent.setData(data);
58         final Map<String, String> extensions = createDataOperationExtensions(requestId, clientTopic);
59         return NcmpCloudEventBuilder.builder().type(DataOperationEvent.class.getName())
60                 .event(dataOperationEvent).extensions(extensions).setCloudEvent().build();
61     }
62
63     private static Data createPayloadFromDataOperationResponses(final MultiValueMap<String, Map<NcmpEventResponseCode,
64             List<String>>> cmHandleIdsPerOperationIdPerResponseCode) {
65         final Data data = new Data();
66         final List<org.onap.cps.ncmp.events.async1_0_0.Response> responses = new ArrayList<>();
67         cmHandleIdsPerOperationIdPerResponseCode.entrySet().forEach(cmHandleIdsPerOperationIdPerResponseCodeEntries ->
68                 cmHandleIdsPerOperationIdPerResponseCodeEntries.getValue().forEach(cmHandleIdsPerResponseCodeEntries ->
69                         responses.addAll(createResponseFromDataOperationResponses(
70                                 cmHandleIdsPerOperationIdPerResponseCodeEntries.getKey(),
71                                 cmHandleIdsPerResponseCodeEntries)
72                         )));
73         data.setResponses(responses);
74         return data;
75     }
76
77     private static List<Response> createResponseFromDataOperationResponses(
78             final String operationId,
79             final Map<NcmpEventResponseCode, List<String>> cmHandleIdsPerResponseCodeEntries) {
80         final List<org.onap.cps.ncmp.events.async1_0_0.Response> responses = new ArrayList<>();
81         cmHandleIdsPerResponseCodeEntries.entrySet()
82                 .forEach(cmHandleIdsPerResponseCodeEntry -> {
83                     final Response response = new Response();
84                     response.setOperationId(operationId);
85                     response.setStatusCode(cmHandleIdsPerResponseCodeEntry.getKey().getStatusCode());
86                     response.setStatusMessage(cmHandleIdsPerResponseCodeEntry.getKey().getStatusMessage());
87                     response.setIds(cmHandleIdsPerResponseCodeEntry.getValue());
88                     responses.add(response);
89                 });
90         return responses;
91     }
92
93     private static Map<String, String> createDataOperationExtensions(final String requestId, final String clientTopic) {
94         final Map<String, String> extensions = new HashMap<>();
95         extensions.put("correlationid", requestId);
96         extensions.put("destination", clientTopic);
97         return extensions;
98     }
99 }