Add withTrustLevel condition to CmHandle Query API
[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-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.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.regex.Matcher;
30 import java.util.regex.Pattern;
31 import lombok.Builder;
32 import lombok.Data;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.ncmp.api.NcmpResponseStatus;
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     private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
47
48     /**
49      * Creates a failure response based on exception.
50      *
51      * @param cmHandleId  cmHandleId
52      * @param exception exception
53      * @return CmHandleRegistrationResponse
54      */
55     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
56                                                                      final Exception exception) {
57         return CmHandleRegistrationResponse.builder()
58             .cmHandle(cmHandleId)
59             .status(Status.FAILURE)
60             .ncmpResponseStatus(UNKNOWN_ERROR)
61             .errorText(exception.getMessage()).build();
62     }
63
64     /**
65      * Creates a failure response based on registration error.
66      *
67      * @param cmHandleId          cmHandleId
68      * @param ncmpResponseStatus registration error code and status
69      * @return CmHandleRegistrationResponse
70      */
71     public static CmHandleRegistrationResponse createFailureResponse(final String cmHandleId,
72         final NcmpResponseStatus ncmpResponseStatus) {
73         return CmHandleRegistrationResponse.builder().cmHandle(cmHandleId)
74             .status(Status.FAILURE)
75             .ncmpResponseStatus(ncmpResponseStatus)
76             .errorText(ncmpResponseStatus.getMessage())
77             .build();
78     }
79
80     /**
81      * Creates a failure response based on registration error.
82      *
83      * @param failedXpaths       list of failed Xpaths
84      * @param ncmpResponseStatus enum describing the type of registration error
85      * @return CmHandleRegistrationResponse
86      */
87     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> failedXpaths,
88             final NcmpResponseStatus ncmpResponseStatus) {
89         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>(failedXpaths.size());
90         for (final String xpath : failedXpaths) {
91             final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
92             if (matcher.find()) {
93                 cmHandleRegistrationResponses.add(
94                     CmHandleRegistrationResponse.createFailureResponse(matcher.group(1), ncmpResponseStatus));
95             } else {
96                 log.warn("Unexpected xpath {}", xpath);
97             }
98         }
99         return cmHandleRegistrationResponses;
100     }
101
102     /**
103      * Creates a failure response based on other exception.
104      *
105      * @param cmHandleIds list of failed cmHandleIds
106      * @param exception   exception caught during the registration
107      * @return CmHandleRegistrationResponse
108      */
109     public static List<CmHandleRegistrationResponse> createFailureResponses(final Collection<String> cmHandleIds,
110             final Exception exception) {
111         return cmHandleIds.stream()
112                 .map(cmHandleId -> CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception))
113                 .toList();
114     }
115
116     public static CmHandleRegistrationResponse createSuccessResponse(final String cmHandle) {
117         return CmHandleRegistrationResponse.builder().cmHandle(cmHandle)
118             .status(Status.SUCCESS).build();
119     }
120
121     public static List<CmHandleRegistrationResponse> createSuccessResponses(final List<String> cmHandleIds) {
122         return cmHandleIds.stream().map(CmHandleRegistrationResponse::createSuccessResponse).toList();
123     }
124
125     public enum Status {
126         SUCCESS, FAILURE;
127     }
128 }