Conditional cps change events
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / models / CmHandleRegistrationResponse.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada
4  *  Modifications Copyright (C) 2022-2024 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.api.models;
23
24 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNKNOWN_ERROR;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import lombok.Builder;
31 import lombok.Data;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.ncmp.api.NcmpResponseStatus;
34 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
35
36 @Data
37 @Builder
38 @Slf4j
39 public class CmHandleRegistrationResponse {
40
41     private final String cmHandle;
42     private final Status status;
43     private NcmpResponseStatus ncmpResponseStatus;
44     private String errorText;
45
46     /**
47      * Creates a failure response based on exception.
48      *
49      * @param cmHandleId  cmHandleId
50      * @param exception exception
51      * @return CmHandleRegistrationResponse
52      */
53     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
54                                                                      final Exception exception) {
55         return CmHandleRegistrationResponse.builder()
56             .cmHandle(cmHandleId)
57             .status(Status.FAILURE)
58             .ncmpResponseStatus(UNKNOWN_ERROR)
59             .errorText(exception.getMessage()).build();
60     }
61
62     /**
63      * Creates a failure response based on registration error.
64      *
65      * @param cmHandleId          cmHandleId
66      * @param ncmpResponseStatus registration error code and status
67      * @return CmHandleRegistrationResponse
68      */
69     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
70         final NcmpResponseStatus ncmpResponseStatus) {
71         return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
72             .status(Status.FAILURE)
73             .ncmpResponseStatus(ncmpResponseStatus)
74             .errorText(ncmpResponseStatus.getMessage())
75             .build();
76     }
77
78     /**
79      * Create a failure response of cm handle registration based on xpath and registration error.
80      * Conditions:
81      * - the xpath should be valid according to the cps path, otherwise xpath is not included in the response.
82      *
83      * @param failedXpaths the failed xpaths
84      * @param ncmpResponseStatus type of the registration error
85      * @return collection of cm handle registration response
86      */
87     public static List<CmHandleRegistrationResponse> createFailureResponsesFromXpaths(
88         final Collection<String> failedXpaths, final NcmpResponseStatus ncmpResponseStatus) {
89         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
90         for (final String xpath : failedXpaths) {
91             try {
92                 final String cmHandleId = YangDataConverter.extractCmHandleIdFromXpath(xpath);
93                 cmHandleRegistrationResponses
94                     .add(CmHandleRegistrationResponse.createFailureResponse(cmHandleId, ncmpResponseStatus));
95             } catch (IllegalArgumentException | IllegalStateException e) {
96                 log.warn("Unexpected xpath {}", xpath);
97             }
98         }
99         return cmHandleRegistrationResponses;
100     }
101
102     /**
103      * Create a failure response of cm handle registration based on cm handle id and registration error.
104      *
105      * @param failedCmHandleIds the failed cm handle ids
106      * @param ncmpResponseStatus type of the registration error
107      * @return collection of cm handle registration response
108      */
109     public static List<CmHandleRegistrationResponse> createFailureResponses(
110             final Collection<String> failedCmHandleIds, final NcmpResponseStatus ncmpResponseStatus) {
111         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
112             new ArrayList<>(failedCmHandleIds.size());
113         for (final String failedCmHandleId : failedCmHandleIds) {
114             cmHandleRegistrationResponses.add(
115                 CmHandleRegistrationResponse.createFailureResponse(failedCmHandleId, ncmpResponseStatus));
116         }
117         return cmHandleRegistrationResponses;
118     }
119
120     /**
121      * Creates a failure response based on other exception.
122      *
123      * @param cmHandleIds list of failed cmHandleIds
124      * @param exception   exception caught during the registration
125      * @return CmHandleRegistrationResponse
126      */
127     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
128             final Exception exception) {
129         return cmHandleIds.stream()
130                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
131                 .toList();
132     }
133
134     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
135         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
136             .status(Status.SUCCESS).build();
137     }
138
139     public static List<CmHandleRegistrationResponse> createSuccessResponses(final Collection<String> cmHandleIds) {
140         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse)
141                 .collect(Collectors.toList());
142     }
143
144     public enum Status {
145         SUCCESS, FAILURE;
146     }
147 }