Set CPS Project Status to 'Mature'
[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-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.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.NcmpResponseStatus;
32 import org.onap.cps.ncmp.api.impl.events.NcmpEvent;
33 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperation;
34 import org.onap.cps.ncmp.events.async1_0_0.Data;
35 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent;
36 import org.onap.cps.ncmp.events.async1_0_0.Response;
37 import org.springframework.util.MultiValueMap;
38
39 @Slf4j
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public class DataOperationEventCreator {
42
43     /**
44      * Creates data operation event.
45      *
46      * @param clientTopic                              topic the client wants to use for responses
47      * @param requestId                                unique identifier per request
48      * @param cmHandleIdsPerResponseCodesPerOperation map of cm handles per operation response per response code
49      * @return Cloud Event
50      */
51     public static CloudEvent createDataOperationEvent(final String clientTopic,
52                                                       final String requestId,
53                                                       final MultiValueMap<DmiDataOperation,
54                                                               Map<NcmpResponseStatus, List<String>>>
55                                                               cmHandleIdsPerResponseCodesPerOperation) {
56         final DataOperationEvent dataOperationEvent = new DataOperationEvent();
57         final Data data = createPayloadFromDataOperationResponses(cmHandleIdsPerResponseCodesPerOperation);
58         dataOperationEvent.setData(data);
59         final Map<String, String> extensions = createDataOperationExtensions(requestId, clientTopic);
60         return NcmpEvent.builder().type(DataOperationEvent.class.getName())
61                 .data(dataOperationEvent).extensions(extensions).build().asCloudEvent();
62     }
63
64     private static Data createPayloadFromDataOperationResponses(final MultiValueMap<DmiDataOperation,
65             Map<NcmpResponseStatus, List<String>>> cmHandleIdsPerResponseCodesPerOperation) {
66         final Data data = new Data();
67         final List<org.onap.cps.ncmp.events.async1_0_0.Response> responses = new ArrayList<>();
68         cmHandleIdsPerResponseCodesPerOperation.forEach((dmiDataOperation, cmHandleIdsPerResponseCodes) ->
69                 cmHandleIdsPerResponseCodes.forEach(cmHandleIdsPerResponseCodeEntries ->
70                         responses.addAll(createResponseFromDataOperationResponses(
71                                 dmiDataOperation, cmHandleIdsPerResponseCodeEntries))));
72         data.setResponses(responses);
73         return data;
74     }
75
76     private static List<Response> createResponseFromDataOperationResponses(
77             final DmiDataOperation dmiDataOperation,
78             final Map<NcmpResponseStatus, List<String>> cmHandleIdsPerResponseCodeEntries) {
79         final List<org.onap.cps.ncmp.events.async1_0_0.Response> responses = new ArrayList<>();
80         cmHandleIdsPerResponseCodeEntries.forEach((ncmpEventResponseCode, cmHandleIds) -> {
81             final Response response = new Response();
82             response.setOperationId(dmiDataOperation.getOperationId());
83             response.setStatusCode(ncmpEventResponseCode.getCode());
84             response.setStatusMessage(ncmpEventResponseCode.getMessage());
85             response.setIds(cmHandleIds);
86             response.setResourceIdentifier(dmiDataOperation.getResourceIdentifier());
87             response.setOptions(dmiDataOperation.getOptions());
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 }